Hello,
I have a C++ dll that needs to return a string to a C# program. What's the best way to do this? I've tried several things, but I can't seem to get it to work.
I really appreciate any help.
Thanks,
Nick
Printable View
Hello,
I have a C++ dll that needs to return a string to a C# program. What's the best way to do this? I've tried several things, but I can't seem to get it to work.
I really appreciate any help.
Thanks,
Nick
In C++
You can define this function when it is imported into the C# as following:Code:/* Dll export specifier*/ LONG SomeFuncName(BSTR* bstrRetString)
{
// some stuff
// Fill *bstrRetString with appropriate string
_bstr_t bs("this is some string which I want to return to the c# prog");
*bstrRetString = bs.copy();
return 0L;
}
than you can use it:Code:class DllWrapper
{
[DllImport("YourDll.dll")]
public static extern long SomeFuncName(out string str);
}
MartinCode:void SomeOtherFunc()
{
string str;
long lRet = DllWrapper.SomeFuncName(out str);
MessageBox.Show("Returned string: " + str);
}
Martin,
Thank you very much for your help. I've got everything running, but for some reason I only seem to be getting the first letter of the string that I am returning. For testing I used your test string below and I always get "t" returned. What does OL need to be that I return? Anything specific?
Thanks again for your help.
Regards,
Nick
try this one out,
CString s( "abcd" );
LPTSTR p = s.GetBuffer( 10 ); // LPTSTR will be ur ref string
strcpy( p, "Hello" ); // directly access CString buffer
s.ReleaseBuffer( );
<edit>
this will be in your C++ code. if CString is not allowed (forexample in case of embeded environment) then just use std::string of STL
</edit>
Paresh
No paresh... That is not the thing...
I just trying it... I wrote the answer down and didn't test it...
I wrote win32 dll which exports just one function. Function returns int and takes BSTR* as parameter...
Then, I wrote C# client (console app) as I post in my answer. It really gets just the first character of the string I want to pass to the caller of the dll's function...
Then, I wrote C prog for testing that dll. It works ok...
I think, the problem is in using unicode string. However I still didn't find solution. I used the same mechanism like that in my answer in several COM objects I wrote. When I used those COM objects from the C#, they worked well... I really don't know why I can't get unicode string from the DLL into this C# app...
I attached the project to this mail...
ConsApp is C# app for testing dll
TryDll is Win32 DLL
TryCpp is C prog for testing dll
solution file is in TryDll dir...
Martin
Hello Paresh,
Thank you very much for your help. I am using CStrings. It works if I set the CString to a constant, but I am calling a function that returns a CString in my dll.
Here is some more detail. I've got an open CDatabase object and I want to return the GetConnect() value back to the C# program. Here is what I've got:
CString getConnectString;
getConnectString= myDB.GetConnect();
LPTSTR p = getConnectString.GetBuffer(getConnectString.GetLength());
strcpy( p, connectStr );
connectStr.ReleaseBuffer( );
My definitions still look basically like what Martin suggested, but now I'm using LPTSTR. Would that be correct?
Thanks again for your help.
Regards,
Nick
ya, you can use LPTSTR
in you C# program pass string as a ref
and take LPTSTR (actually a pointer to char) and then by allocating the size and releasing the buffer is fine.
check out Martin's sample also.
tnx
Paresh
Paresh,
When I run Martin's sample I only get the first character of the string that is returned (when using the C# client). I think he said the same thing. When I run the C++ program that calls the dll it returns the whole string.
Thanks,
Nick
Hi guys!
Try this!
It should be work!
// Dll C++
extern "C" DllExport int GetStringFromDLL(WCHAR** ppString)
{
LPTSTR p = getConnectString.GetBuffer(getConnectString.GetLength()); // or something else!
*ppString = (LPTSTR)p;
return 1;
}
// C#
[DllImport("Your.dll")]
public static extern int GetStringFromDLL(ref IntPtr p);
void SomeFunction()
{
IntPtr ptr = IntPtr.Zero;
int res = GetStringFromDLL(ref ptr);
string MyString = Marshal.PtrToStringUni(ptr);
}
Good luck!
Max
and yes marshalling is requred here. i forgot to mention. thanks GMV
Paresh
You all are geniuses! Thank you very much for your help.
Yea... I was working too long yesterday... :) And even made more mistakes... :)
Martin,
i don't think so you made mistakes
;)
Paresh
Hi there,
I have a 'similar' problem - except I want to return a string from a C# DLL to a C/C++ application as an output parameter. I've just about given up. Help!
Sarah
After a lot of trial and error (and much hair pulling) I have finally managed to get a string out of a C# DLL into a C/C++ application. So unless anyone has a better suggestion: use unsafe pointers in the C# code. I used the C# sbyte* type which corresponds to the C/C++ char* type. Note that the C# code has to be compiled with /unsafe.