How to call Delphi DLL WideString Parameters from C++ (including var parameters)

前端 未结 1 667
执笔经年
执笔经年 2021-01-26 20:42

I have a Delphi DLL that works when called by delphi apps and exports a method declared as:

Procedure ProduceOutput(request,inputs:widestring; var ResultString:w         


        
1条回答
  •  失恋的感觉
    2021-01-26 21:21

    There is no DllImport in C++. That is for .NET PInvoke instead. So remove that.

    The remainder of your C++ function declaration does not match the Delphi function declaration. The correct C++ declaration is as follows:

    void __stdcall ProduceOutput(WideString request, WideString inputs, WideString &ResultString);
    

    Don't forget to statically link to the DLL's import .LIB file (which you can create using C++Builder's command-line IMPLIB.EXE tool, if needed).

    Then, in the app's code, you can call the DLL function like this:

    WideString arequest = ComboBox1->Text;
    WideString ainput = " Input Text Goes Here ";
    WideString aresultstring;
    ProduceOutput(arequest, ainput, aresultstring);
    
    Memo1->Lines->Text = aresultstring;
    

    The reason you are getting the conversion error is because the WideString class overrides the & operator to return a pointer to its internal BSTR member. The reason for this is to allow WideString to act like a smart wrapper class for ActiveX/COM strings, eg:

    HRESULT __stdcall SomeFuncThatReturnsABStr(BSTR** Output);
    

    WideString output;
    SomeFuncThatReturnsABStr(&output);
    

    As such, it is not possible to obtain a pointer to a WideString itself using the & operator. Because of that, the only way (that I know of) to get a real WideString pointer is to dynamically allocate the WideString, eg:

    WideString *pStr = new WideString;
    ...
    delete pStr;
    

    0 讨论(0)
提交回复
热议问题