如何将IntPtr转换为UIntPtr?

Ian*_*Ian 5 c#

我尝试像这样投射它:

UIntPtr x = (UIntPtr)intPtr;
Run Code Online (Sandbox Code Playgroud)

...但编译器对它不满意并返回编译错误.

我需要进行转换,因为RegOpenKeyEx的P/Invoke签名需要一个UIntPtr:

  [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  public static extern int RegOpenKeyEx(
    UIntPtr hKey,
    string subKey,
    int ulOptions,
    int samDesired,
    out UIntPtr hkResult);
Run Code Online (Sandbox Code Playgroud)

为了获得句柄,我使用SafeHandle.DangerousHandle()返回一个IntPtr:

   /// <summary>
    /// Get a pointer to a registry key.
    /// </summary>
    /// <param name="registryKey">Registry key to obtain the pointer of.</param>
    /// <returns>Pointer to the given registry key.</returns>
    IntPtr _getRegistryKeyHandle(RegistryKey registryKey)
    {
        //Get the type of the RegistryKey
        Type registryKeyType = typeof(RegistryKey);

        //Get the FieldInfo of the 'hkey' member of RegistryKey
        System.Reflection.FieldInfo fieldInfo =
            registryKeyType.GetField("hkey", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

        //Get the handle held by hkey
        SafeHandle handle = (SafeHandle)fieldInfo.GetValue(registryKey);

        //Get the unsafe handle
        IntPtr dangerousHandle = handle.DangerousGetHandle();
        return dangerousHandle;
    }
Run Code Online (Sandbox Code Playgroud)

Fel*_* K. 2

查看 msdn 后,我注意到 和UIntPtr都有IntPtr指针void*转换。

IntPtr a = ....;
UIntPtr b = (UIntPtr)a.ToPointer();
Run Code Online (Sandbox Code Playgroud)

此时你需要不安全的代码。避免这种情况的唯一方法是使用unchecked关键字并使用非指针类型进行转换(此处也使用)。

两个指针之间没有转换的原因可能是UIntPtrlikeIntPtr没有固定的大小,因为它们是特定于平台的(请参阅 参考资料)。