【问题标题】:P/Invoke : converting this signature to Managed c#P/Invoke : 将此签名转换为托管 c#
【发布时间】:2015-12-02 15:20:33
【问题描述】:
HRESULT WINAPI RegisterDeviceWithManagement(
_In_ LPCWSTR ppszMDMServiceUri,
_In_ LPCWSTR pszUPN,
_In_ LPCWSTR ppzsAccessToken
);
我想将此转换为 Dllimport c# 签名。
任何帮助将不胜感激
【问题讨论】:
标签:
c#
pinvoke
unmanaged
managed
【解决方案1】:
这很简单:
-
HRESULT 类型是一个无符号的 32 位整数,所以uint。您可能有理由使用int,因为签名类型往往更容易在托管代码中使用。但是,由于您不太可能在 HRESULT 上执行算术运算,因此我认为您最好使用 uint。
-
WINAPI 宏扩展为 stdcall 调用约定,这恰好是默认值,因此我们可以省略调用约定。如果您喜欢明确,请包括CallingConvention = CallingConvention.Stdcall。
- 字符串都是Unicode字符串,所以我们使用
CharSet.Unicode。
所以翻译是:
[DllImport(dllname, CharSet = CharSet.Unicode)]
static extern uint RegisterDeviceWithManagement(
string ppszMDMServiceUri,
string pszUPN,
string ppzsAccessToken
);
显然你需要填写DLL的名称。