我有在 Delphi 上编写的 DLL。我只有没有头文件的DLL,所以我动态加载它。 (到 C++ 项目)
HMODULE hLib = LoadLibrary(L"LibName.dll");
if (!hLib) {
//Throw error
}
DLL提供的功能:
function DataToFile(AddressName: PChar; Request: PChar;
RequestSize: integer; ResultFile: PChar;
ErrorBuf: PChar; ErrorBufSize: integer):BOOL;stdcall;
function DataToStream(AddressName: PChar; Request: PChar;
RequestSize: integer; ResultStream: IStream;
ErrorBuf: PChar; ErrorBufSize: integer):BOOL;stdcall;
我的 Visual Studio 代码(C++):
typedef bool(__stdcall* f_DataToFile)(
char*, //AddressName: PChar
char*, //Request: PChar
int, //RequestSize: integer
char*, //FileName: PChar
char*, //ErrorBuf: PChar
int); //ErrorBufSize: integer);
typedef bool(__stdcall* f_DataToStream)(
char*, //AddressName: PChar
char*, //Request: PChar
int, //RequestSize: integer
std::istream &, //ResultStream: IStream
char*, //ErrorBuf: PChar
int); //ErrorBufSize: integer);
... 获取功能。地址:
//load DataToFile
f_DataToFile DataToFile = (f_DataToFile) GetProcAddress(hLib, "DataToFile");
if (!DataToFile) { //throw error
}
//load DataToStream
f_DataToStream DataToStream = (f_DataToStream) GetProcAddress(hLib, "DataToStream");
if (!DataToStream) { //throw error
}
... 设置数据:
char* AddressName = _strdup("127.0.0.1:1234"); //AddressName: PChar
char* Request = _strdup("<?xml request... >"); //Request: PChar
int RequestSize = strlen(Request); //RequestSize: integer
char* ResultFile = _strdup("exportpath\\output.xml"); //FileName: PChar
char* ErrorBuf = new char[255]; //ErrorBuf: PChar
int ErrorBufSize = 255; //ErrorBufSize: integer);
std::filebuf(buffer);
std::istream ResultStream(&buffer);
... 第一个功能正常工作
bool reesult1= (DataToFile)(AddressName, Request, RequestSize, ResultFile, ErrorBuf, ErrorBufSize);
... 我在执行第二个函数时遇到问题 -
bool reesult2= (DataToStream)(AddressName, Request, RequestSize, ResultStream, ErrorBuf, ErrorBufSize);
正在编译,但在运行时出现访问冲突。
有人可以帮助我了解如何正确使用 (Delphi) 中的 IStream 数据类型吗?
当我将 ResultStream 声明为 nullptr 指针并使用不正确的连接地址调用 DataToStream 函数时,函数返回“连接错误”- 因此它被正确导入,主要问题是从函数返回 IStream。
最佳答案
您对 IStream
的翻译不正确。 DLL 正在使用 COM IStream
interface .您不能将其替换为 C++ 的 std::istream
类。您需要使用实现 IStream
接口(interface)的 COM 对象。要以 IStream
的形式访问文件,您可以使用 Win32 SHCreateStreamOnFileEx()
功能,例如。
关于c++ - 从 C++ 调用 Delphi DLL IStream 参数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55894229/