Search code examples
c#pinvokeunmanagedmanaged

P/Invoke : converting this signature to Managed c#


HRESULT WINAPI RegisterDeviceWithManagement(
  _In_ LPCWSTR ppszMDMServiceUri,
  _In_ LPCWSTR pszUPN,
  _In_ LPCWSTR ppzsAccessToken
);

I want to convert this to Dllimport c# signature. Any help will be greatly appreciated


Solution

  • This is as simple as it comes:

    • The HRESULT type is an unsigned 32 bit integer, so uint. You could be justified in using int since signed types tend to be easier to work with in managed code. However, since you are unlikely to be performing arithmetic on an HRESULT you may as well use uint in my opinion.
    • The WINAPI macro expands to the stdcall calling convention which happens to be the default, so we can omit the calling convention. If you prefer to be explicit, include CallingConvention = CallingConvention.Stdcall.
    • The strings are all Unicode string, so we use CharSet.Unicode.

    So the translation is:

    [DllImport(dllname, CharSet = CharSet.Unicode)]
    static extern uint RegisterDeviceWithManagement(
        string ppszMDMServiceUri,
        string pszUPN,
        string ppzsAccessToken
    );
    

    Obviously you need to fill in the name of the DLL.