C#中与C++中的 LPWSTR(wchar_t *) 对应的类型

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

原文地址:https://www.cnblogs.com/uftwkb24/p/9712175.html