什么是C#等价的C ++ DWORD?
搜索之后,我听说UInt32是C#等价的C ++ DWORD。 我通过执行算法测试结果
*(DWORD*)(1 + 0x2C) //C++ (UInt32)(1 + 0x2C) //C#
他们产生完全不同的结果。 有人可以告诉我C#中DWORD的正确匹配吗?
您的示例使用DWORD作为指针,该指针很可能是无效指针。 我假设你自己的意思是DWORD。
DWORD定义为无符号长整数,最终为32位无符号整数。
uint(System.UInt32)应该是匹配的。
#import // I'm on macOS right now, so I'm defining DWORD // the way that Win32 defines it. typedef unsigned long DWORD; int main() { DWORD d = (DWORD)(1 + 0x2C); int i = (int)d; printf("value: %d\n", i); return 0; }
产量:45
public class Program { public static void Main() { uint d = (uint)(1 + 0x2C); System.Console.WriteLine("Value: {0}", d); } }
产量:45
微软的DWord定义:
typedef unsigned long DWORD,* PDWORD,* LPDWORD; https://msdn.microsoft.com/en-us/library/cc230318.aspx
微软的Uint32定义
typedef unsigned int UINT32; https://msdn.microsoft.com/en-us/library/cc230386.aspx
现在你可以看到差异….一个是unsigned long,另一个是unsigned int
你的两个片段完全不同。 在您的C ++代码中,由于某种奇怪的原因,您将值(1 + 0x2C)
(写入45的奇怪方式)转换为DWORD*
,然后解除引用,就好像该地址实际上是有效的内存位置一样。 使用C#,您只需在整数类型之间进行转换。