我怎样才能用 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
对不起我的英语不好.. :(
这是德尔福代码
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;
在这里我得到信息或绑定文件......
var 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])^);
我的尝试(我知道这段代码看起来很垃圾哈哈)也许不是?
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 = ???