开发者

different charset for multiple params with dllimport

开发者 https://www.devze.com 2023-02-03 07:49 出处:网络
Is it possible to dec开发者_C百科lare different charset options for seperate params? heres what i mean:

Is it possible to dec开发者_C百科lare different charset options for seperate params?

heres what i mean:

[dllimport("my.dll", charset = charset.Ansi)]
void myfunc(string CharPtrInCPP, StringBuilder WCharPtrInCPP);

the problem is the c++ function takes a char* for the filename and a wchar* for the data recieved back...

in c++:

void myfuncImpl(char *filename, WCHAR *buffer, int len); 
//another method, myfunc, wraps this


As already pointed out, you should be able to specify MarshalAs for each parameter. Another way would be to specify a default character set type and then specify the marshalling for the odd one out. For example,

  [DllImport("my.dll", CharSet=CharSet.Unicode)]
  void myfunc( [MarshalAs( UnmanagedType.LPStr )] String filename, 
               StringBuilder buffer, int len );


You should specify [MarshalAs] for each parameter.

Try the following:

[DllImport("my.dll")]
void myfunc(
     [MarshalAs(UnmanagedType.LPStr)] string CharPtrInCPP, 
     [MarshalAs(UnmanagedType.LPWStr)] StringBuilder WCharPtrInCPP,
     int len
   );
0

精彩评论

暂无评论...
验证码 换一张
取 消