Solved

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

Posted on 2013-06-24
4
2,713 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

691 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