Solved

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

Posted on 2013-06-24
4
2,497 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now