Solved

How to Call Delphi DLL Method from C++  with Var WideString parameter

Posted on 2013-06-24
4
2,573 Views
Last Modified: 2013-06-30
greetings,

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:widestring);stdcall;

On the C++ side I have tried:

[DllImport( "ArgumentLab.dll", CallingConvention = CallingConvention.StdCall, CharSet=CharSet.WideString )];
 extern void ProduceOutput(WideString request, WideString inputs, WideString ResultString);


WideString arequest = WideString(ComboBox1->Text);
WideString ainput = "<xml> Input Text Goes Here </XML>";
WideString  aresultstring;
WideString &aresultstringpointer = aresultstring;
aresultstring = " ";
ProduceOutput(arequest, ainput, &aresultstringpointer);

Memo1->Lines->Text = &aresultstringpointer;

My console error reads:

  Unit1.cpp(13): candidate function not viable: no known conversion from 'BSTR *' (aka 'wchar_t **') to 'System::WideString' for 3rd argument;

I have built the DLL and the c++ test app using Rad Studio XE4 - it is a 64 bit DLL and APP

How should I have gone about doing this?  

Best regards,

garry
0
Comment
Question by:garrymott
  • 3
4 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 39273826
I am not using C++, so i am just suggesting here.
Would it be the pointer that makes the road bumpy?
Have you tried to use:
...
ProduceOutput(arequest, ainput, aresultstring);

Memo1->Lines->Text = aresultstring;
...

Open in new window

Or use function instead to return the ResultString:
function ProduceOutput(request,inputs:widestring; ResultString:widestring):widestring; stdcall;

Open in new window

So your call would be:
Memo1->Lines->Text = ProduceOutput(arequest, ainput, aresultstring);

Open in new window

0
 

Author Comment

by:garrymott
ID: 39274809
JimyX - Thank you for your response - no I believe that C++ cannot simply pass a widestring as (an in/out) parameter as can be done from Delphi.  Also I do not have the option to use a string as a result.

The function was originally provided in a DLL to be used with a COM server. The callers used Visual Basic and BSTRs to operate the DLL.  Now the same peeps want to skip using COM and handle the DLL directly from C++ or C#.  

I need to give them some guidance on how to proceed.

again thanks for your response
0
 

Accepted Solution

by:
garrymott earned 0 total points
ID: 39276069
Ok - solved it myself.  Here is the code for anybody who needs to walk this path. - It is also embedded here:       CPPDllLabMain.cpp


typedef void* (*VoidReturnFunc)(WideString request, WideString inputs, WideString &ResultString);

extern "C" __declspec(dllimport) void* __stdcall GetClassInstance();

 HINSTANCE load = LoadLibrary(library);

WideString arequest = WideString("EchoInputs");//(ComboBox1->Text);
WideString ainput = WideString("<xml> Input Text Goes Here </XML>");
WideString aresultstring = WideString( "hello") ;


 if (load) {
                        VoidReturnFunc myFunc;
                        myFunc = (VoidReturnFunc)GetProcAddress(load, "ProduceOutput");
                        myFunc(arequest, ainput, aresultstring);
                        Memo1->Lines->Text = aresultstring;

        }
            else {
                        ShowMessage("Library not loaded!");
                        Memo1->Lines->Clear();
            }
0
 

Author Closing Comment

by:garrymott
ID: 39287918
The use of Widestrings as var parameters provides a safe method of returning large string data from DLL procedures.  We do this in our COM servers but the same process is suitable for direct calls from C++.  There is a lot of info out there but the specifics on implementation on the C++ side are rare or non-existent.  I tested this before I posted it and trust the info is good - enjoy - Peace and Good to you
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I could not build boost code, 10 87
drawing animated level bar based on numbers 3 97
Issues with C++ Class 19 92
can i put an image from resource into image tag ? 6 25
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question