c# - Marshalling LPWSTR * from a C function -


i have sample function native code

hresult getsamplefunctionvalue(_out_ lpwstr * argument) 

this function outputs value in argument. need call managed code

[dllimport("mydll.dll", entrypoint = "getsamplefunctionvalue", charset = charset.unicode)] static extern uint getsamplefunctionvalue([marshalasattribute(unmanagedtype.lpwstr)] stringbuilder  argument); 

this returns garbage value. afaik original c function not create string using cotaskmemalloc. correct call?

any appreciated.

you need c# code receive pointer. this:

[dllimport("mydll.dll")] static extern uint getsamplefunctionvalue(out intptr argument); 

call this:

intptr argument; uint retval = getsamplefunctionvalue(out argument); // add check of retval here string argstr = marshal.ptrtostringuni(argument); 

and you'll presumably need call native function deallocates memory allocated. can after call marshal.ptrtostringuni because @ point no longer need pointer. or perhaps string returned statically allocated, can't sure. in case, docs native library explain what's needed.

you may need specify calling convention. written, native function looks use __cdecl. however, perhaps didn't include specification of __stdcall in question. again, consult native header file sure.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -