p\invoke once again...

G

gh

Hello,
I have not problems to p\invoke some blittable types (so
as int, long), but String stops me :(
Could anyone explain me, what's wrong here:

(native.dll, eVC++4.0 SP2, default call convention -
__cdecl)
extern "C" __declspec(dllexport) int getString(WCHAR*&)
{ /*some code here*/ }

(C# code, trying invoke getString)
class A {
[DllImport("native.dll")]
public static extern int getString(ref String s);

void Invoker() {
String str_ = "Terrible news that:";
getString (ref str_); // <-- here I caught exception
}
}

The similar sample code placed in MSDN, why it does not
working??

Best regards.
 
P

Paul G. Tobey [eMVP]

Your declaration doesn't match the declaration in C++. Take a close look at
the parameter. What is it? It's a reference to a pointer, right? Well,
what is that, exactly? It's a pointer to a pointer, effectively. So, your
C# declaration would also need to be a pointer to a pointer. I'm not aware
of any way to do this except with unsafe code. If you control the C++ DLL,
you should rethink the design. A better way to do it would be to accept a
string and a length, and have the DLL function write the text of the string,
up to the length, directly into the passed string.

Paul T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top