关于VC++到Delphi的一个小问题,关于union,顶者有分!!:)

时间:2020-12-12 17:37:34
翻译一个SDK时遇到了union,不知道怎么办了,希望高手帮忙看看能用delphi搞定不!先谢过了!:)
typedef struct {
DWORD dwSize;
char sDecoderIP[16];
WORD wDecoderPort;
WORD wLoadMode;
union
{
BYTE byFile[100];
struct 
{
DWORD dwChannel;
BYTE sUserName[NAME_LEN];
BYTE sPassword[PASSWD_LEN];
NET_DVR_TIME struStartTime;
NET_DVR_TIME struStopTime;
}bytime;
}mode_size;
}NET_DVR_PLAYREMOTEFILE,*LPNET_DVR_PLAYREMOTEFILE;

typedef struct {
DWORD dwWorkType;
char sDVRIP[16];
WORD wDVRPort;
BYTE byChannel;
BYTE byLinkMode;
DWORD dwLinkType;
union
{
struct 
{
BYTE sUserName[NAME_LEN];
BYTE sPassword[PASSWD_LEN];
char cReserve[52];
}userInfo;
struct 
{
BYTE   fileName[100];
}fileInfo;
struct 
{
DWORD dwChannel;
BYTE sUserName[NAME_LEN];
BYTE sPassword[PASSWD_LEN];
NET_DVR_TIME struStartTime;
NET_DVR_TIME struStopTime;
}timeInfo;
}objectInfo;
}NET_DVR_DECCHANSTATUS, *LPNET_DVR_DECCHANSTATUS;


7 个解决方案

#1



type
  NET_DVR_PLAYREMOTEFILE = record
    dwSize : dword;
    sDecoderIP: array[0..15] of char;
    wDecoderPort: word;
    wLoadMode: word;
    mode_size = record
      case integer of
      0:
      byFile: array[0..99] of byte;
      1:
      bytime = record
        dwChannel: dword;
        sUserName: array[0..NAME_LEN - 1] of byte;
        sPassword: array[0..PASSWD_LEN - 1] of byte;
        struStartTime: NET_DVR_TIME;
        struStopTime: NET_DVR_TIME;
      end;
    end;
  end;
  LPNET_DVR_PLAYREMOTEFILE = ^NET_DVR_PLAYREMOTEFILE;

  NET_DVR_DECCHANSTATUS = record
    dwWorkType: dword;
    sDVRIP: array[0..15] of char;
    wDVRPort: word;
    byChannel: byte;
    byLinkMode: byte;
    dwLinkType: dword;
    objectInfo = record
      case integr of
      0:
        userInfo = record
          sUserName: array[0..NAME_LEN - 1] of byte;
          sPassword: array[0..PASSWD_LEN - 1] of byte;
          cReserve: array[0..51] of char;
        end;
      1:
        fileInfo = record
          fileName: array[0..99] of byte;
        end;
      2:
        timeInfo = record
          dwChannel: dword;
          sUserName: array[0..NAME_LEN - 1] of byte;
          sPassword: array[0..PASSWD_LEN - 1] of byte;
          struStartTime: NET_DVR_TIME;
          struStopTime: NET_DVR_TIME;
        end;
    end;
  end;
  LPNET_DVR_DECCHANSTATUS = ^NET_DVR_DECCHANSTATUS;

还有,NAME_LEN、PASSWD_LEN、NET_DVR_TIME这些定义没有,你自己翻译吧。
象sUserName之类的array of byte也可以定义为array of char,具体要看你的程序中是怎么使用的。

#2


同意楼上

#3


同意

JF

#4


UP`~~~

#5


顶~顶~

#6


UP

#7


对不起啊,嵌套的记录可能有问题。
这样就好了:

  tbytime = record
    dwChannel: dword;
    sUserName: array[0..NAME_LEN - 1] of byte;
    sPassword: array[0..PASSWD_LEN - 1] of byte;
    struStartTime: NET_DVR_TIME;
    struStopTime: NET_DVR_TIME;
  end;

  tmode_size = record
    case integer of
    0:
      (byFile: array[0..99] of byte);
    1:
      (bytime: tbytime);
  end;

  NET_DVR_PLAYREMOTEFILE = record
    dwSize : dword;
    sDecoderIP: array[0..15] of char;
    wDecoderPort: word;
    wLoadMode: word;
    mode_size: tmode_size;
  end;
  LPNET_DVR_PLAYREMOTEFILE = ^NET_DVR_PLAYREMOTEFILE;

  tuserInfo = record
    sUserName: array[0..NAME_LEN - 1] of byte;
    sPassword: array[0..PASSWD_LEN - 1] of byte;
    cReserve: array[0..51] of char;
  end;

  tfileInfo = record
    fileName: array[0..99] of byte;
  end;

  ttimeInfo = record
    dwChannel: dword;
    sUserName: array[0..NAME_LEN - 1] of byte;
    sPassword: array[0..PASSWD_LEN - 1] of byte;
    struStartTime: NET_DVR_TIME;
    struStopTime: NET_DVR_TIME;
  end;

  tobjectInfo = record
  case integer of
  0:
    (userinfo: tuserinfo);
  1:
    (fileinfo: tfileinfo);
  2:
    (timeinfo: ttimeinfo);
  end;

  NET_DVR_DECCHANSTATUS = record
    dwWorkType: dword;
    sDVRIP: array[0..15] of char;
    wDVRPort: word;
    byChannel: byte;
    byLinkMode: byte;
    dwLinkType: dword;
    objectInfo: tobjectInfo;
  end;
  LPNET_DVR_DECCHANSTATUS = ^NET_DVR_DECCHANSTATUS;

#1



type
  NET_DVR_PLAYREMOTEFILE = record
    dwSize : dword;
    sDecoderIP: array[0..15] of char;
    wDecoderPort: word;
    wLoadMode: word;
    mode_size = record
      case integer of
      0:
      byFile: array[0..99] of byte;
      1:
      bytime = record
        dwChannel: dword;
        sUserName: array[0..NAME_LEN - 1] of byte;
        sPassword: array[0..PASSWD_LEN - 1] of byte;
        struStartTime: NET_DVR_TIME;
        struStopTime: NET_DVR_TIME;
      end;
    end;
  end;
  LPNET_DVR_PLAYREMOTEFILE = ^NET_DVR_PLAYREMOTEFILE;

  NET_DVR_DECCHANSTATUS = record
    dwWorkType: dword;
    sDVRIP: array[0..15] of char;
    wDVRPort: word;
    byChannel: byte;
    byLinkMode: byte;
    dwLinkType: dword;
    objectInfo = record
      case integr of
      0:
        userInfo = record
          sUserName: array[0..NAME_LEN - 1] of byte;
          sPassword: array[0..PASSWD_LEN - 1] of byte;
          cReserve: array[0..51] of char;
        end;
      1:
        fileInfo = record
          fileName: array[0..99] of byte;
        end;
      2:
        timeInfo = record
          dwChannel: dword;
          sUserName: array[0..NAME_LEN - 1] of byte;
          sPassword: array[0..PASSWD_LEN - 1] of byte;
          struStartTime: NET_DVR_TIME;
          struStopTime: NET_DVR_TIME;
        end;
    end;
  end;
  LPNET_DVR_DECCHANSTATUS = ^NET_DVR_DECCHANSTATUS;

还有,NAME_LEN、PASSWD_LEN、NET_DVR_TIME这些定义没有,你自己翻译吧。
象sUserName之类的array of byte也可以定义为array of char,具体要看你的程序中是怎么使用的。

#2


同意楼上

#3


同意

JF

#4


UP`~~~

#5


顶~顶~

#6


UP

#7


对不起啊,嵌套的记录可能有问题。
这样就好了:

  tbytime = record
    dwChannel: dword;
    sUserName: array[0..NAME_LEN - 1] of byte;
    sPassword: array[0..PASSWD_LEN - 1] of byte;
    struStartTime: NET_DVR_TIME;
    struStopTime: NET_DVR_TIME;
  end;

  tmode_size = record
    case integer of
    0:
      (byFile: array[0..99] of byte);
    1:
      (bytime: tbytime);
  end;

  NET_DVR_PLAYREMOTEFILE = record
    dwSize : dword;
    sDecoderIP: array[0..15] of char;
    wDecoderPort: word;
    wLoadMode: word;
    mode_size: tmode_size;
  end;
  LPNET_DVR_PLAYREMOTEFILE = ^NET_DVR_PLAYREMOTEFILE;

  tuserInfo = record
    sUserName: array[0..NAME_LEN - 1] of byte;
    sPassword: array[0..PASSWD_LEN - 1] of byte;
    cReserve: array[0..51] of char;
  end;

  tfileInfo = record
    fileName: array[0..99] of byte;
  end;

  ttimeInfo = record
    dwChannel: dword;
    sUserName: array[0..NAME_LEN - 1] of byte;
    sPassword: array[0..PASSWD_LEN - 1] of byte;
    struStartTime: NET_DVR_TIME;
    struStopTime: NET_DVR_TIME;
  end;

  tobjectInfo = record
  case integer of
  0:
    (userinfo: tuserinfo);
  1:
    (fileinfo: tfileinfo);
  2:
    (timeinfo: ttimeinfo);
  end;

  NET_DVR_DECCHANSTATUS = record
    dwWorkType: dword;
    sDVRIP: array[0..15] of char;
    wDVRPort: word;
    byChannel: byte;
    byLinkMode: byte;
    dwLinkType: dword;
    objectInfo: tobjectInfo;
  end;
  LPNET_DVR_DECCHANSTATUS = ^NET_DVR_DECCHANSTATUS;