从C++调用 Delphi DLL IStream 参数的问题
Problems with Delphi DLL IStream parameter calling from C++
我在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 Code (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
接口。你不能用C++的std::istream
类代替它。需要使用实现IStream
接口的 COM 对象。例如,要以IStream
访问文件,您可以使用Win32 SHCreateStreamOnFileEx()
功能。
感谢雷米·勒博!
HRESULT GetFileStream(
_In_ LPCWSTR fullFileName,
_Outptr_ IStream** stream)
{
HRESULT hr = S_OK;
// Create stream for writing the file
if (SUCCEEDED(hr))
{
hr = SHCreateStreamOnFileEx(
fullFileName,
STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE,
0, // default file attributes
TRUE, // create file if it does not exist
NULL, // no template
stream);
}
return hr;
}
在类型定义中:
IStream *, //ResultStream: IStream
..
LPCWSTR filename = L"path\to\file.txt";
IStream* ResultStream = NULL;
HRESULT hr = GetFileStream(filename, &ResultStream);
..
bool result = (CallRK7XMLRPCToStream)(AddressName, Request, RequestSize, ResultStream, ErrorBuf, ErrorBufSize);
以下是使用 SHCreateStreamOnFileEx 的一个很好的例子:
https://github.com/Microsoft/Windows-classic-samples/blob/master/Samples/AppxPackingCreateBundle/cpp/CreateBundle.cpp
相关文章:
- C++复杂情况的比较器通过参数问题
- C++17 中的歧义错误(模板模板参数和默认参数问题)
- C 缺少模板参数问题
- C++ pcap_loop() 参数问题
- 带有指针作为参数问题的 C++ 函数
- C++ 奇怪的字符*参数问题(不兼容的类型)
- JNA 参数问题:内存访问无效
- Qt样式表和"一个参数"问题
- 默认参数问题 (c++)
- 具有可变模板的递归继承和继承参数问题
- 命令行参数问题
- OpenCV 决策树参数问题
- winapi GetProcessMemoryInfo 无效参数问题
- 参数问题?至少我认为是这样
- 构造函数参数问题C++
- c++教程参考参数问题
- c++多线程参数问题
- Clang Format多行函数声明参数问题
- 提升Python - 具有默认参数问题的重载函数
- VBA Excel DLL参数问题-第6个参数