different charset for multiple params with dllimport

a 夏天 提交于 2020-01-21 15:24:48

问题


Is it possible to declare 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

回答1:


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 );



回答2:


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
   );


来源:https://stackoverflow.com/questions/4631267/different-charset-for-multiple-params-with-dllimport

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!