如何在 C# 中声明 LARGE_INTEGER

发布于 2024-07-15 23:46:03 字数 2277 浏览 5 评论 0原文

下面的代码(C++ 中)是我尝试转换为 C# 的代码。

DWORD Func_X_4(DWORD arg1, DWORD arg2, DWORD arg3)
{
LARGE_INTEGER result = {1, 0};
LARGE_INTEGER temp1 = {0};
LARGE_INTEGER temp2 = {0};
LARGE_INTEGER temp3 = {0};
LARGE_INTEGER temp4 = {0};
for(int x = 0; x < 32; ++x)
{
    if(arg2 & 1)
    {
        temp1.LowPart = arg3;
        temp1.HighPart = 0;
        temp2.QuadPart = temp1.QuadPart * result.QuadPart;
        temp3.LowPart = arg1;
        temp3.HighPart = 0;
        temp4.QuadPart = temp2.QuadPart % temp3.QuadPart;
        result.QuadPart = temp4.QuadPart;
    }
    arg2 >>= 1;
    temp1.LowPart = arg3;
    temp1.HighPart = 0;
    temp1.QuadPart *= temp1.QuadPart;
    temp2.LowPart = arg1;
    temp2.HighPart = 0;
    temp3.QuadPart = temp1.QuadPart % temp2.QuadPart;
    arg3 = temp3.LowPart;
    if(!arg2)
        break;
}
return result.LowPart;
}

这里我尝试翻译代码,但它太混乱了,而且我以前从未使用过大整数。

结构:

public struct LARGE_INTEGER
{
    UInt32 LowPart;
    Int32 HighPart;
    Int32 QuadPart;
}

翻译后的功能:

    public Int32 Func_X_4(Int32 arg1, Int32 arg2, Int32 arg3)
    {
    LARGE_INTEGER result = {1, 0}; //this and the four below,are they correct?
    LARGE_INTEGER temp1 = {0, 0};
    LARGE_INTEGER temp2 = {0, 0};
    LARGE_INTEGER temp3 = {0, 0};
    LARGE_INTEGER temp4 = {0, 0};
    for(int x = 0; x < 32; ++x)
    {
        if(arg2 & 1==0) //correct?
        {
            temp1.LowPart = arg3;
            temp1.HighPart = 0;
            temp2.QuadPart = temp1.QuadPart * result.QuadPart;
            temp3.LowPart = arg1;
            temp3.HighPart = 0;
            temp4.QuadPart = temp2.QuadPart % temp3.QuadPart;
            result.QuadPart = temp4.QuadPart;
        }
        arg2 >>= 1;
        temp1.LowPart = arg3;
        temp1.HighPart = 0;
        temp1.QuadPart *= temp1.QuadPart;
        temp2.LowPart = arg1;
        temp2.HighPart = 0;
        temp3.QuadPart = temp1.QuadPart % temp2.QuadPart;
        arg3 = temp3.LowPart;
        if(arg2 != 0) //correct?
        break;
    }
    return result.LowPart;
}

问题: 第一个问题是我在C#中没有找到LARGE_INTEGER类型变量,所以我创建了一个结构,我想知道是否确实存在。 至于第二个问题,功能不对,没有任何作用。

对于该特定问题的任何帮助将不胜感激! 先感谢您。

the code below(in C++) is what I am trying the convert into C#

DWORD Func_X_4(DWORD arg1, DWORD arg2, DWORD arg3)
{
LARGE_INTEGER result = {1, 0};
LARGE_INTEGER temp1 = {0};
LARGE_INTEGER temp2 = {0};
LARGE_INTEGER temp3 = {0};
LARGE_INTEGER temp4 = {0};
for(int x = 0; x < 32; ++x)
{
    if(arg2 & 1)
    {
        temp1.LowPart = arg3;
        temp1.HighPart = 0;
        temp2.QuadPart = temp1.QuadPart * result.QuadPart;
        temp3.LowPart = arg1;
        temp3.HighPart = 0;
        temp4.QuadPart = temp2.QuadPart % temp3.QuadPart;
        result.QuadPart = temp4.QuadPart;
    }
    arg2 >>= 1;
    temp1.LowPart = arg3;
    temp1.HighPart = 0;
    temp1.QuadPart *= temp1.QuadPart;
    temp2.LowPart = arg1;
    temp2.HighPart = 0;
    temp3.QuadPart = temp1.QuadPart % temp2.QuadPart;
    arg3 = temp3.LowPart;
    if(!arg2)
        break;
}
return result.LowPart;
}

Here I tried to translate the code,but it's too messy and I never worked with Large integers before.

Structure:

public struct LARGE_INTEGER
{
    UInt32 LowPart;
    Int32 HighPart;
    Int32 QuadPart;
}

Translated function:

    public Int32 Func_X_4(Int32 arg1, Int32 arg2, Int32 arg3)
    {
    LARGE_INTEGER result = {1, 0}; //this and the four below,are they correct?
    LARGE_INTEGER temp1 = {0, 0};
    LARGE_INTEGER temp2 = {0, 0};
    LARGE_INTEGER temp3 = {0, 0};
    LARGE_INTEGER temp4 = {0, 0};
    for(int x = 0; x < 32; ++x)
    {
        if(arg2 & 1==0) //correct?
        {
            temp1.LowPart = arg3;
            temp1.HighPart = 0;
            temp2.QuadPart = temp1.QuadPart * result.QuadPart;
            temp3.LowPart = arg1;
            temp3.HighPart = 0;
            temp4.QuadPart = temp2.QuadPart % temp3.QuadPart;
            result.QuadPart = temp4.QuadPart;
        }
        arg2 >>= 1;
        temp1.LowPart = arg3;
        temp1.HighPart = 0;
        temp1.QuadPart *= temp1.QuadPart;
        temp2.LowPart = arg1;
        temp2.HighPart = 0;
        temp3.QuadPart = temp1.QuadPart % temp2.QuadPart;
        arg3 = temp3.LowPart;
        if(arg2 != 0) //correct?
        break;
    }
    return result.LowPart;
}

Problems:
The first problem is that I haven't found a LARGE_INTEGER type variable in C#,so I created a strucure,I'd like to know if there is actually.
As for the second problem,the function is not right,nothing worked.

Any help on that particular question will be greatfuly appreciated!
Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

当爱已成负担 2024-07-22 23:46:03

LARGE_INTEGER 结构的直接翻译是:

[StructLayout(LayoutKind.Explicit, Size=8)]
struct LARGE_INTEGER
{
    [FieldOffset(0)]public Int64 QuadPart;
    [FieldOffset(0)]public UInt32 LowPart;
    [FieldOffset(4)]public Int32 HighPart;
}

它就像 C 中的联合,其中 QuadPart 是一个 8 字节值,LowPart 占用前 4 个字节和 HighPart 占据高 4 个字节。

A direct translation of the LARGE_INTEGER structure would be:

[StructLayout(LayoutKind.Explicit, Size=8)]
struct LARGE_INTEGER
{
    [FieldOffset(0)]public Int64 QuadPart;
    [FieldOffset(0)]public UInt32 LowPart;
    [FieldOffset(4)]public Int32 HighPart;
}

It's like a union in C, where QuadPart is an 8-byte value, with LowPart occupying the first 4 bytes and HighPart occupying the high 4 bytes.

蝶…霜飞 2024-07-22 23:46:03

它是 Int64。 基于 http://msdn.microsoft.com/en-us/library/ aa383713.aspx,它是一个 64 位有符号整数。

Its Int64. Based on http://msdn.microsoft.com/en-us/library/aa383713.aspx, its a 64 bit-signed integer.

最偏执的依靠 2024-07-22 23:46:03

基于之前的答案,我发现扩展 LARGE_INTEGER 的定义以包括高位和低位部分的 int 和 uint 版本很有用:

[StructLayout(LayoutKind.Explicit, Size=8)]
struct LARGE_INTEGER
    {
    [FieldOffset(0)]public long QuadPart;

    [FieldOffset(0)]public uint LowPart;
    [FieldOffset(4)]public int HighPart;

    [FieldOffset(0)]public int LowPartAsInt;
    [FieldOffset(0)]public uint LowPartAsUInt;

    [FieldOffset(4)]public int HighPartAsInt;
    [FieldOffset(4)]public uint HighPartAsUInt;
    }

Building on a previous answer I found that it was useful to extend the definition of LARGE_INTEGER to include int and uint versions of the high and low parts:

[StructLayout(LayoutKind.Explicit, Size=8)]
struct LARGE_INTEGER
    {
    [FieldOffset(0)]public long QuadPart;

    [FieldOffset(0)]public uint LowPart;
    [FieldOffset(4)]public int HighPart;

    [FieldOffset(0)]public int LowPartAsInt;
    [FieldOffset(0)]public uint LowPartAsUInt;

    [FieldOffset(4)]public int HighPartAsInt;
    [FieldOffset(4)]public uint HighPartAsUInt;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文