【问题标题】:How to emulate a 64 bit pointer in a 32 bit application如何在 32 位应用程序中模拟 64 位指针
【发布时间】:2020-09-24 20:39:08
【问题描述】:

我有一个用 C# 编写的 32 位用户模式应用程序,我需要模拟 64 位指针的行为。我知道我可以编译为 64 位,但我使用的是 directx9 dll,一旦我更改目标架构,它就会变得不兼容。

我的问题是 IntPtr 类类型太小,只有 4 个字节,在与 64 位内核模式驱动程序通信时会导致问题。我需要一个 64 位指针来匹配驱动程序上的结构。

我的问题是是否有任何方法可以在 c# 应用程序上创建 64 位指针?是否有任何包装类可以为我做到这一点?

ideally:

IntPtr32 pnt32; //would 4 bytes large
IntPtr64 pnt64; //and this would be 8 bytes large

【问题讨论】:

  • 使用long 强制64位。
  • 你想用这个指针做什么?您不能使用 64 位地址从 32 位应用程序访问内存。

标签: c# windows pointers x86-64


【解决方案1】:

在尝试了 Raymond 的建议后,使用 long 强制 64 位变量,我可以确认虽然 hacky 它确实有效。

这个指针需要做什么的一点背景。驱动程序和用户模式应用程序都有一个用于相互发送请求的结构。该结构有一个由驱动程序写入并由用户模式应用程序发送的 void*。它充当用户在请求完成后读取信息的缓冲区。

//NOTE: alot of stuff ommitted for simplicity

T buffer = default;
IntPtr pBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(buffer));
Marshal.StructureToPtr(buffer, pBuffer, false);

//construct struct for the read request
KERNEL_READ_REQUEST ReadRequest = new KERNEL_READ_REQUEST();
ReadRequest.pBuff = pBuffer.ToInt64(); //long type variable in struct

if(DeviceIoControl(&ReadRequest)){ 
    buffer = (T)Marshal.PtrToStructure(pBuffer, typeof(T));

    //return buffer and free memory
    Marshal.FreeHGlobal(pBuffer); //free the memory

    return buffer;
}

我不会将此标记为答案,因为使用 long 来存储 64 位地址确实感觉不对,但我会把对我有用的东西留在这里。

【讨论】:

  • 关键字longSystem.Int64 的别名。随意使用类型名称而不是别名。
猜你喜欢
  • 2012-04-22
  • 2014-09-22
  • 2013-01-11
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 2013-12-05
  • 1970-01-01
  • 2013-04-11
相关资源
最近更新 更多