2009-03-25 4 views
2

le code ci-dessous (en C++) est ce que je suis en train de la convertir en C#Comment declarate LARGE_INTEGER en 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; 
} 

Ici, j'essayé de traduire le code, mais il est trop compliqué et je ne ai jamais travaillé avec grand entiers avant.

Structure:

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

fonction Traduction:

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; 
} 

Problèmes: Le premier problème est que je ne l'ai pas trouvé une variable de type LARGE_INTEGER en C#, donc je créé une strucure, je J'aimerais savoir s'il y a réellement. Quant au deuxième problème, la fonction n'est pas bonne, rien n'a fonctionné.

Toute aide sur cette question sera grandement appréciée! Merci d'avance.

Répondre

11

une traduction directe de la structure de LARGE_INTEGER serait:

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

Il est comme une union en C, où QuadPart est une valeur de 8 octets, avec LowPart occupant les 4 premiers octets et HighPart occupant le haut 4 octets.

+2

Utilisez LayoutKind.Explicit au lieu de LayoutKind.Absolute – Rado

1

bâtiment sur une réponse précédente, j'ai trouvé qu'il était utile d'étendre la définition de LARGE_INTEGER pour inclure les versions int et uint des parties hautes et basses:

[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; 
    }