将一个delphi代码行转换为c ++

时间:2012-04-13 23:24:19

标签: c++ delphi

如何在c ++中编写该行?

使用delphi代码(不是这里发布的代码)我会写一些信息填充一些信息到exe文件和c ++代码我会读取这条记录并将信息传递给结构

这是delphi中的代码

type
 TSettings = record
    sFileName: String[50]; 
    siInstallFolder: Byte; 
    bRunFile: Boolean;
   ...
  end;

..
var
i: dword;
sZdData: PChar;
Settings :Tsettings;
begin
....
     ZeroMemory(@Settings, sizeof(Tsettings));
      settings := Tsettings(Pointer(@sZdData[i])^); // this code to c++

c ++代码(希望其余的都可以)

struct TSettings{
    char sFileName[50]; 
    byte siInstallFolder; 
    bool bRunFile;
   ...
}  Settings;

...
DWORD i;
LPBYTE sZdData;

         ZeroMemory(&Settings, sizeof(TSettings));
      Settings = ????? // im failing here i dunno what to do // i need same as in delphi code above

和我的坏英语的故事.. :(

这是delphi代码

function GetInfoSettings(FileName: String; // filename from where to get data
                        var lpData: PChar; // where to write data
                        var dwSettingsLen: DWORD // returns the length of all bound files
                        ): Boolean;
var
  hFile: THandle;
  DosHeader: TImageDosHeader;
  NtHeaders: TImageNtHeaders;
  SectionHeader: TImageSectionHeader;
  dwReadBytes, dwOrginalFileSize, dwFileSize, dwSettingsLength: DWORD;
begin
  Result := False;
  hFile :=  Createfile(PChar(FileName), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
  SetFilePointer(hFile, 0, nil, FILE_BEGIN);
  ReadFile(hFile, DosHeader, sizeof(DosHeader), dwReadBytes, nil);
  if dwReadBytes = sizeof(DosHeader) then
  begin
    SetFilePointer(hFile, DosHeader._lfanew, nil, FILE_BEGIN);
    ReadFile(hFile, NtHeaders, sizeof(NtHeaders), dwReadBytes, nil);
    if dwReadBytes = sizeof(NtHeaders) then
    begin
      SetFilePointer(hFile, sizeof(SectionHeader) * (NtHeaders.FileHeader.NumberOfSections -1), nil, FILE_CURRENT);
      ReadFile(hFile, SectionHeader, sizeof(SectionHeader), dwReadBytes, nil);
      dwOrginalFileSize := SectionHeader.PointerToRawData + SectionHeader.SizeOfRawData;
      dwFileSize := GetFileSize(hFile, nil);
      dwSettingsLength := dwFileSize - dwOrginalFileSize;
      if dwSettingsLength > 0 then
      begin
        SetFilePointer(hFile, dwOrginalFileSize, nil, FILE_BEGIN);
        GetMem(lpData, dwSettingsLength);
        ReadFile(hFile, lpData^, dwSettingsLength, dwReadBytes, nil);
        if dwReadBytes = dwSettingsLength then
        begin
          Result := True;
          dwSettingsLen := dwSettingsLength;
        end;
      end;
    end;
  end;
  CloseHandle(hFile);
end;

在这里我得到信息或绑定文件......

无功   i,//这里将保存已完成的字节数。   dwDaSize:DWORD; //所有部分后的数据长度..   dwFilenaam,sFileName:string;   sZdData:PChar; //这里将是“EOF”之后的所有数据(所有部分数据结束)  //将提取文件的文件名..

  Settings: Tsettings;


// writting
  hFile: THandle;
  lpNumberOfBytesWritten: DWORD;
begin
  GetMem(dwFilenaam, MAX_PATH);
  GetModuleFileName(GetModuleHandle(nil), dwFilenaam, MAX_PATH);
  if GetInfos(dwFilenaam, sZdData, dwDaSize) then
  begin
    i := 0;
    repeat

      ZeroMemory(@Settings, sizeof(Tsettings));
      settings := Tsettings(Pointer(@sZdData[i])^);

我的尝试(我知道这段代码看起来垃圾LOL)也许不是吗?

bool getSettingsInfo(LPSTR FileName, LPBYTE lpdata, DWORD dwSettingsLen)
{
    HANDLE HandleFile ;
    DWORD  dwReadBytes;
    DWORD dwOrginalFileSize;
    DWORD dwFileSize;
    DWORD dwSettingsLength; 
    PIMAGE_DOS_HEADER pidh ;
    PIMAGE_NT_HEADERS pinh ;
    PIMAGE_SECTION_HEADER pish;

    return  false;
    HandleFile =  CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, 0,OPEN_EXISTING,0, NULL); 
    SetFilePointer(HandleFile, 0,0, FILE_BEGIN);
        ReadFile(HandleFile, pidh, sizeof(pidh), &dwReadBytes,NULL); 
    if (dwReadBytes == sizeof(pidh))
     {
     SetFilePointer(HandleFile, pidh->e_lfanew , NULL, FILE_BEGIN);
     ReadFile(HandleFile, pidh,sizeof(pinh), &dwReadBytes, NULL); 
    if (dwReadBytes == sizeof(pinh)) 
    {
      SetFilePointer(HandleFile, sizeof(pish) * (pinh->FileHeader.NumberOfSections -1), NULL, FILE_CURRENT);
      ReadFile(HandleFile, pish, sizeof(pinh), &dwReadBytes, NULL);
      dwOrginalFileSize = pish->PointerToRawData + pish->SizeOfRawData;
      dwFileSize = GetFileSize(HandleFile, NULL);
      dwSettingsLength = dwFileSize - dwOrginalFileSize;
      if (dwSettingsLength > 0) 
      {

        SetFilePointer(HandleFile, dwOrginalFileSize, NULL, FILE_BEGIN);
        realloc(lpdata, dwSettingsLength);
        ReadFile(HandleFile, lpdata, dwSettingsLength, &dwReadBytes, NULL);
        if (dwReadBytes == dwSettingsLength) 
        {
         return true;
          dwSettingsLen = dwSettingsLength;
        }
      }
    }
  }
  CloseHandle(HandleFile);
} 

提取信息的代码......

  DWORD i; // here will be saved how much bytes are already done ..
  DWORD dwDaSize; // length of data after all sections ..

  LPSTR dwFilenaam; 
  LPBYTE sZdData; // here will be all data after "EOF" (End of all sections data)
  LPSTR sFileName;// the filename where the file will be extracted ..

  char * Installpath;
  char * buffer;

  HFILE hFile;
  DWORD lpNumberOfBytesWritten;

  memset(dwFilenaam,0, MAX_PATH);
  GetModuleFileName(GetModuleHandle(NULL), dwFilenaam, MAX_PATH);
  if (getSettingsInfo(dwFilenaam, sZdData, dwDaSize) == true)
  {
      i = 0;
    //  REPEAT


      ZeroMemory(&Settings, sizeof(TSettings));

Settings = ???

2 个答案:

答案 0 :(得分:0)

  struct TSettings{
     uchar length ; // shortstrings have an implicit length byte
     char sFileName[50]; 
     byte siInstallFolder; 
     bool bRunFile;  // one byte, 1=true, 0=false  any other value undefined
     ...
   }  Settings;


  DWORD i;
  LPBYTE sZdData;

  ZeroMemory(&Settings, sizeof(TSettings));   

  literally it is something like
    Settings = (struct Settings)  (*((void *)&sZDData[i])

  essentially it does

  memcpy (&sZDData[i],&settings,sizeof(TSettings));

它的作用是获取sZSdata(@sZDDATA [i])的第i个元素的地址并将其转换为无类型指针。 (相当于C ++(void *)(& sZDData [i])))

然后是时髦的部分,derefence它(^作为pascal中的后缀运算符,*作为C中的前缀运算符)(基本上是(void)ptr指向的地址的结果,没有长度数据的内存块附在上面)。

当分配给设置时,这意味着将从该地址的左值(= sizeof(设置))字节大小的字节移动到左值(设置)。

这是一个非常时髦的结构,我不确定我以前是否看过它。我检查了Delphi和FPC中的asm级别,并且它们的行为相同。

答案 1 :(得分:0)

在C语言中应该比Delphi更容易:

typedef struct 
{
     uchar length ; // shortstrings have an implicit length byte
     char sFileName[50]; 
     byte siInstallFolder; 
     bool bRunFile;  // one byte, 1=true, 0=false  any other value undefined
     ...
} TSettings;

TSettings settings;
DWORD i;
LPBYTE sZdData;

settings = *(TSettings*)(&sZdData[i]);

这将调用memcpy,而不是因为转换,因为分配了TSettings结构。如果使用指向TSettings的指针,则无需复制即可访问:

TSettings* pSettings;
DWORD i; 
LPBYTE sZdData;

pSettings = (TSettings*)(&sZdData[i]);
printf("%d\n", pSettings->siInstallFolder); // example

顺便说一句,小心对齐。由于项目之间存在差距,总结构规模可能会超出您的预期。