将C ++代码转换为C#

时间:2011-06-01 16:43:12

标签: c# .net c++

我在转换以下代码C#时遇到了非常严峻的挑战,任何人都可以帮助我解决这个问题。

typedef struct ACSNameAddr_t {
    char FAR *serverName;    // How do i use FAR in C#
    struct {
        short length;
        unsigned char FAR *value;
    } serverAddr;
} ACSNameAddr_t;

如何在C#

中使用此联合
typedef struct
{
    union
    {
        CSTARouteRegisterAbortEvent_t   registerAbort;
        CSTARouteUsedEvent_t            routeUsed;
        CSTARouteUsedExtEvent_t         routeUsedExt;
        CSTARouteEndEvent_t             routeEnd;
        CSTAPrivateEvent_t              privateEvent;
        CSTASysStatEvent_t              sysStat;
        CSTASysStatEndedEvent_t         sysStatEnded;
    }u;
} CSTAEventReport;

编辑答案:

所以包括你所有的答案,我在这里编写转换后的代码。如果有任何错误,请编辑它,它可能对某人有用..

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct ACSNameAddr_t
{
    string serverName;
    [StructLayout(LayoutKind.Sequential, Pack = 4)]
    public struct serverAddr
    {
        public short length;
        string value;
    };
};

和第二个,

[StructLayout(LayoutKind.Explicit, Pack = 4)]
    public struct CSTAEventReport{
        [StructLayout(LayoutKind.Explicit, Pack = 4)]
        public struct u{
            [FieldOffset(0)]
            public CSTARouteRegisterAbortEvent_t registerAbort;
            [FieldOffset(0)]
            public CSTARouteUsedEvent_t routeUsed;
            [FieldOffset(0)]
            public CSTARouteUsedExtEvent_t routeUsedExt;
            [FieldOffset(0)]
            public CSTARouteEndEvent_t routeEnd;
            [FieldOffset(0)]
            public CSTAPrivateEvent_t privateEvent;
            [FieldOffset(0)]
            public CSTASysStatEvent_t sysStat;
            [FieldOffset(0)]
            public CSTASysStatEndedEvent_t sysStatEnded;
        };
};

3 个答案:

答案 0 :(得分:4)

char FAR *只是指向字符串的指针。回到过去,当我们不得不担心不同的内存模型时,FAR是一个32位指针而NEAR是一个16位指针。像对待任何其他字符指针一样对待char FAR *

答案 1 :(得分:3)

您可以使用StructLayout在C#中模拟C ++联合:

http://msdn.microsoft.com/en-us/library/acxa5b99(v=vs.80).aspx

基本上,只需声明一系列具有相同偏移量的变量,这实际上就是一个联合。

编辑:只需从链接中复制/粘贴即可:

[System.Runtime.InteropServices.StructLayout(LayoutKind.Explicit)]
struct TestUnion
{
    [System.Runtime.InteropServices.FieldOffset(0)]
    public int i;

    [System.Runtime.InteropServices.FieldOffset(0)]
    public double d;

    [System.Runtime.InteropServices.FieldOffset(0)]
    public char c;

    [System.Runtime.InteropServices.FieldOffset(0)]
    public byte b;
}

答案 2 :(得分:0)

所以包括你所有的答案,我在这里编写转换后的代码。如果有任何错误,请编辑它,它可能对某人有用..

[StructLayout(LayoutKind.Sequential, Pack = 4)] 
public struct ACSNameAddr_t 
{ 
    string serverName; 
    [StructLayout(LayoutKind.Sequential, Pack = 4)] 
    public struct serverAddr 
    { 
        public short length; 
        string value; 
    }; 
}; 

和第二个,

[StructLayout(LayoutKind.Explicit, Pack = 4)] 
    public struct CSTAEventReport{ 
        [StructLayout(LayoutKind.Explicit, Pack = 4)] 
        public struct u{ 
            [FieldOffset(0)] 
            public CSTARouteRegisterAbortEvent_t registerAbort; 
            [FieldOffset(0)] 
            public CSTARouteUsedEvent_t routeUsed; 
            [FieldOffset(0)] 
            public CSTARouteUsedExtEvent_t routeUsedExt; 
            [FieldOffset(0)] 
            public CSTARouteEndEvent_t routeEnd; 
            [FieldOffset(0)] 
            public CSTAPrivateEvent_t privateEvent; 
            [FieldOffset(0)] 
            public CSTASysStatEvent_t sysStat; 
            [FieldOffset(0)] 
            public CSTASysStatEndedEvent_t sysStatEnded; 
        }; 
};