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

时间:2019-04-28 20:16:41

标签: c++ delphi dll istream loadlibrary

我在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);

它正在编译,但是在运行时提供Access Violoation。

有人可以帮助我-如何正确使用(Delphi)中的IStream数据类型吗?

当我将ResultStream声明为nullptr指针并使用错误的连接地址调用DataToStream函数时,函数返回“连接错误”-因此它已正确导入,主要问题是从函数返回IStream。

2 个答案:

答案 0 :(得分:4)

您对const cors = (req, res, next) => { // Website you wish to allow to connect res.setHeader("Access-Control-Allow-Origin", "*"); // Request methods you wish to allow res.setHeader( "Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE" ); // Request headers you wish to allow res.setHeader( "Access-Control-Allow-Headers", "Origin,X-Requested-With,content-type" ); // Pass to next layer of middleware next(); }; //export default cors; //export {cors as default} //export {cors}; //module.exports = cors; //module.exports = { cors } module.exports = { cors: cors } 的翻译不正确。 DLL正在使用COM IStream interface。您不能将其替换为C ++的IStream类。您需要使用实现std::istream接口的COM对象。例如,要以IStream访问文件,可以使用Win32 SHCreateStreamOnFileEx()函数。

答案 1 :(得分:0)

感谢雷米·勒博!

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