1.设置 CharSet = CharSet.Unicode
[DllImport("test.dll", EntryPoint = "sum()", CharSet = CharSet.Unicode)]
public static extern int sum(wchar_t* param);
1>test.dll是载入的dll
2>sum()是dll中的函数
3>param是函数中的参数。
2.把wchar_t* 转为stringbuilder/string
[DllImport("test.dll", EntryPoint = "sum()", CharSet = CharSet.Unicode)]
public static extern int sum(StringBuilder param);
调用时候:
sum("hello world !")
总结:设置charset 就可以用stringbuilder代替wchar_t*
看到的另一种方法:
[DllImport("Netapi32.dll", EntryPoint = "NetUserChangePassword", CharSet = CharSet.Unicode)]
public static extern int NetUserChangePassword(
[MarshalAs(UnmanagedType.LPWStr)] string domainname,
[MarshalAs(UnmanagedType.LPWStr)] string username,
[MarshalAs(UnmanagedType.LPWStr)] string oldpassword,
[MarshalAs(UnmanagedType.LPWStr)] string newpassword);