different charset for multiple params with dllimport

后端 未结 2 1835
不思量自难忘°
不思量自难忘° 2021-01-21 19:28

Is it possible to declare different charset options for seperate params?

heres what i mean:

[dllimport(\"my.dll\", charset = charset.Ansi)]
void myfunc(s         


        
相关标签:
2条回答
  • 2021-01-21 19:33

    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 讨论(0)
  • 2021-01-21 19:54

    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 );
    
    0 讨论(0)
提交回复
热议问题