Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-06-24
4
Medium Priority
?
2,832 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

618 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