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.
/* 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;
}
You can define this function when it is imported into the C# as following:
Code:
class DllWrapper
{
[DllImport("YourDll.dll")]
public static extern long SomeFuncName(out string str);
}
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?
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
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:
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.
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.
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!
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.