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
Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

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…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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