从C ++调用Delphi DLL IStream参数的问题

问题描述 投票:1回答:2

我有在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。

c++ delphi dll istream loadlibrary
2个回答
4
投票

您对IStream的翻译不正确。 DLL正在使用COM IStream interface。你无法用C ++的std::istream类替换它。您需要使用实现IStream接口的COM对象。要以IStream身份访问文件,可以使用Win32 SHCreateStreamOnFileEx()函数。


0
投票

感谢Remy Lebeau!

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;
}

在typedef中:

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

© www.soinside.com 2019 - 2024. All rights reserved.