什么是C#等价的C ++ DWORD?

时间:2016-10-10 04:51:05

标签: c# c++

搜索之后,我听说UInt32是C#等价的C ++ DWORD。 我通过执行算术

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

他们产生完全不同的结果。有人可以告诉我C#中DWORD的正确匹配吗?

3 个答案:

答案 0 :(得分:5)

您的示例使用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: %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

答案 1 :(得分:0)

你的两个片段完全不同。在您的C ++代码中,出于某些奇怪的原因,您将值(1 + 0x2C) (一种奇怪的方式写入45)转换为DWORD*,然后将其解除引用,如如果该地址实际上是有效的内存位置。使用C#,您只需在整数类型之间进行转换。

答案 2 :(得分:0)

来自microsoft的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

现在你可以看到差异......一个是无符号长整数,另一个是无符号整数