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
Post a Comment