开发者

Convert C++ code to C# [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, 开发者_运维技巧visit the help center. Closed 10 years ago.

Im having very tough challenge in converting the following code C#, can anybody help me on how do we do that.

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

and how do i use this union in 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;

Edit Answer:

So including all your answers, here by i'm writing the converted code. Please edit it if there is anything wrong, it might be useful for someone..

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

and for the second one,

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


char FAR * is just a pointer to a string. Back in the bad old days when we had to worry about different memory models, FAR was a 32-bit pointer and NEAR was a 16-bit pointer. Treat that char FAR * as you would any other character pointer.


You can simulate C++ unions in C# by using StructLayout:

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

Basically, just declare a series of variables with the same offset, which is really what a union is.

Edit: Just a copy/paste from the link to illustrate:

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


So including all your answers, here by i'm writing the converted code. Please edit it if there is anything wrong, it might be useful for someone..

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

and for the second one,

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜