什么是c++ DWORD的c#等价物?

What is the C# equivalent of C++ DWORD?

本文关键字:等价物 DWORD c++ 什么      更新时间:2023-10-16

经过搜索,我听说UInt32是c#中的c++ DWORD。我通过执行算术

来测试结果
*(DWORD*)(1 + 0x2C) //C++
(UInt32)(1 + 0x2C) //C#

它们产生完全不同的结果。有人能告诉我c#中DWORD的正确匹配吗?

您的示例使用DWORD作为指针,这很可能是一个无效的指针。我假设你指的是DWORD本身。

DWORD定义为unsigned long,最终为32位无符号整数。

uint (System.UInt32)应该是匹配的。

#import <stdio.h>
// 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: %dn", i);
    return 0;
}

输出:45

public class Program
{
    public static void Main()
    {
        uint d = (uint)(1 + 0x2C);
        System.Console.WriteLine("Value: {0}", d);
    }
}

输出:45

来自microsoft的DWord定义:

typepedef unsigned long DWORD, *PDWORD, *LPDWORD;https://msdn.microsoft.com/en-us/library/cc230318.aspx

Uint32 definition from microsoft

typedef unsigned int UINT32;https://msdn.microsoft.com/en-us/library/cc230386.aspx

现在您可以看到差异....一个是unsigned long,另一个是unsigned int

这两个代码段做的事情完全不同。在c++代码中,由于一些奇怪的原因,您将值(1 + 0x2C) (一种奇怪的写45的方式)转换为DWORD*,然后对其解引用,就好像该地址实际上是一个有效的内存位置一样。使用c#,您只需在整数类型之间进行转换。