Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Hilfe beim Übersetzen von C zu Delphi (https://www.delphipraxis.net/177643-hilfe-beim-uebersetzen-von-c-zu-delphi.html)

SlpLow 18. Nov 2013 17:47

Hilfe beim Übersetzen von C zu Delphi
 
Hallo liebe! Benötige Hilfe übersetzen bei VC++ zu Delphi!

VC++
....

Delphi-Quellcode:
void EditWIMFile(HANDLE hImg, int imgNum, LPWSTR pName, LPWSTR pDesc)
{
//Get the info and divide it into two strings
WCHAR *ImgInfoBuf, TempString[100];
DWORD ImgInfoBufSize;
WIMGetImageInformation(hImg, (LPVOID *)&ImgInfoBuf, &ImgInfoBufSize);
wsprintf(TempString, TEXT("<IMAGE INDEX=\"%d\">"), imgNum);
WCHAR *StringPointer = wcsstr(ImgInfoBuf, TempString);
WCHAR *SecondString = wcsstr(++StringPointer, TEXT("<"));
SecondString[-1] = 0; //end the first string

//Copy the first string to a new buffer and add our extra info
WCHAR *NewInfoBuf = (WCHAR *)LocalAlloc(LPTR, ImgInfoBufSize + 200 * sizeof(WCHAR));
lstrcpy(NewInfoBuf, ImgInfoBuf);
if (*pName) {
wsprintf(TempString, TEXT(" <NAME>%s</NAME>\n "), pName);
lstrcat(NewInfoBuf, TempString);
}
if (*pDesc) {
if (*pName)
wsprintf(TempString, TEXT("<DESCRIPTION>%s</DESCRIPTION>\n "), pDesc);
else
wsprintf(TempString, TEXT(" <DESCRIPTION>%s</DESCRIPTION>\n "), pDesc);
lstrcat(NewInfoBuf, TempString);
}

//Add the second string to the new buffer and reset the info in the file
lstrcat(NewInfoBuf, SecondString);
WIMSetImageInformation(hImg, NewInfoBuf, wcslen(NewInfoBuf) * sizeof(WCHAR));

//Free the storage
LocalFree(ImgInfoBuf);
LocalFree(NewInfoBuf);
}
....

Der schöne Günther 18. Nov 2013 21:04

AW: Hilfe beim Übersetzen von C zu Delphi
 
And which lines are you having problems with?

Also, please consider properly formatting and wrapping your code in <code></code>-tags.

Code:
void EditWIMFile(HANDLE hImg, int imgNum, LPWSTR pName, LPWSTR pDesc) {
   
   //Get the info and divide it into two strings
   WCHAR *ImgInfoBuf, TempString[100];
   DWORD ImgInfoBufSize;
   
   WIMGetImageInformation(
      hImg,
      (LPVOID *)&ImgInfoBuf,
      &ImgInfoBufSize
   );
   
   wsprintf(TempString, TEXT("<IMAGE INDEX=\"%d\">"), imgNum);
   
   WCHAR *StringPointer = wcsstr(ImgInfoBuf, TempString);
   WCHAR *SecondString = wcsstr(++StringPointer, TEXT("<"));
   SecondString[-1] = 0; //end the first string

   //Copy the first string to a new buffer and add our extra info
   WCHAR *NewInfoBuf = (WCHAR *)LocalAlloc(
      LPTR,
      ImgInfoBufSize + 200 * sizeof(WCHAR)
   );
   lstrcpy(NewInfoBuf, ImgInfoBuf);
   
   if (*pName) {
      wsprintf(TempString, TEXT(" <NAME>%s</NAME>\n "), pName);
      lstrcat(NewInfoBuf, TempString);
   }
   
   if (*pDesc) {
      if (*pName)
         wsprintf(TempString, TEXT("<DESCRIPTION>%s</DESCRIPTION>\n "), pDesc);
      else
         wsprintf(TempString, TEXT(" <DESCRIPTION>%s</DESCRIPTION>\n "), pDesc);
      
      lstrcat(NewInfoBuf, TempString);
   }

   //Add the second string to the new buffer and reset the info in the file
   lstrcat(NewInfoBuf, SecondString);
   WIMSetImageInformation(hImg, NewInfoBuf, wcslen(NewInfoBuf) * sizeof(WCHAR));

   //Free the storage
   LocalFree(ImgInfoBuf);
   LocalFree(NewInfoBuf);
}

himitsu 18. Nov 2013 21:21

AW: Hilfe beim Übersetzen von C zu Delphi
 
Über die Suchfunktion kann man auch schon einige fertig übersetzte Delphi-Übersetzungen finden :zwinker:

z.B.
Hier im Forum suchenWIMGetImageInformation

http://www.delphipraxis.net/137479-w...er-delphi.html
http://www.delphipraxis.net/177393-w...is-delphi.html

SlpLow 19. Nov 2013 00:02

AW: Hilfe beim Übersetzen von C zu Delphi
 
Versuchte...

Versuchte...
Speichert keine!

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
const
  CR = #13#10;
var
  WimHandle, ImageHandle: THandle;
  Created, ImageIdx: DWORD;
  WimName, MyTemp, pName, pDesc, aall, aa, bb, dd, cc, ns, ds, ErrCode: string;
  ImgInfoBuf, NewInfoBuf: PWideChar;
  pre: Integer;
begin
  InitWIMGAPI;

  WimName := 'd:\Project\sources\install.wim';
  ImageIdx := 1;
  MyTemp := 'd:\Project\Temp';
  pName := 'Windows 7 HOMEBASIC (x64)';
  pDesc := 'Windows 7 HOMEBASIC (x64) AllDrivers';

  // Open file
  WimHandle := WIMCreateFile(PWideChar(WimName), WIM_GENERIC_READ,
    WIM_OPEN_EXISTING, WIM_FLAG_SHARE_WRITE, WIM_COMPRESS_XPRESS, @Created);

  if WimHandle = 0 then
  begin
    ShowMessage('Error Open ' + IntToStr(GetLastError));
    Exit;
  end
  else
  begin
    // Set temp
    if (not WIMSetTemporaryPath(WimHandle, PWideChar(MyTemp))) then
    begin
      ShowMessage('Error set temp ' + IntToStr(GetLastError));
      Exit;
    end
    else
    begin
      // Load image
      ImageHandle := WIMLoadImage(WimHandle, ImageIdx);

      if ImageHandle = 0 then
      begin
        ShowMessage('Error load image ' + IntToStr(GetLastError));
        Exit;
      end
      else
      begin
        if (not WIMGetImageInformation(ImageHandle, @ImgInfoBuf, @pre)) then
        begin
          ShowMessage('Error get info ' + IntToStr(GetLastError));
          Exit;
        end
        else
        begin
          aall := ImgInfoBuf;
          Memo1.Lines.Clear;
          // Parsing
          bb := '<IMAGE INDEX="' + IntToStr(ImageIdx) + '">';
          aa := Copy(aall, 1, Pos(bb, aall) - 1);
          bb := '<NAME>';
          aa := Copy(aall, 1, Pos(bb, aall) - 1);
          bb := '<FLAGS>';
          cc := ' ' + Copy(aall, Pos(bb, aall), Length(aall));
          ns := '<NAME>' + pName + '</NAME>' + CR;
          ds := ' <DESCRIPTION>' + pDesc + '</DESCRIPTION>' + CR;
          aall := aa + ns + ds + cc;
          NewInfoBuf := PWideChar(aall);
          // if (not WIMGetImageInformation(ImageHandle, @ImgInfoBuf, @pre)) then
          if (not WIMSetImageInformation(ImageHandle, ImgInfoBuf, pre)) then
          begin
            ShowMessage('Error set info ' + IntToStr(GetLastError));
            Exit;
          end
          else
            Memo1.Lines.Add(aall);
        end;
      end;
    end;
  end;
  WIMCloseHandle(ImageHandle);
  WIMCloseHandle(WimHandle);
end;

SlpLow 19. Nov 2013 07:07

AW: Hilfe beim Übersetzen von C zu Delphi
 
Delphi-Quellcode:
function SysErrorMessageEx(ErrorCode: Longint): string;
var
  Len: Integer;
  Buffer: array [0 .. 4095] of char;
begin
  Len := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or
    FORMAT_MESSAGE_IGNORE_INSERTS, nil, ErrorCode, 0, Buffer,
    SizeOf(Buffer), nil);
  SetString(result, Buffer, Len);
  if (Len = 0) then
    result := Format('Code: ' + IntToStr(GetLastError) + ' - ' +
      SysErrorMessage(GetLastError), [SysErrorMessage(GetLastError)]);
  result := Format('Code: ' + IntToStr(GetLastError) + ' - ' +
    SysErrorMessage(GetLastError), [ErrorCode, result]);
end;

.....
NewInfoBuf := PWideChar(aall);
if (not WIMSetImageInformation(ImageHandle, NewInfoBuf, pre)) then
begin
  ErrCode:=SysErrorMessageEx(GetLastError);
  ShowMessage('Error set info ' + ErrCode);
  Exit;
end
  else....
Failed to parse the requested XML data!

Was mache ich hier falsch?

SlpLow 19. Nov 2013 22:32

AW: Hilfe beim Übersetzen von C zu Delphi
 
Windows was unable to parse the requested XML data!

Der schöne Günther 19. Nov 2013 23:04

AW: Hilfe beim Übersetzen von C zu Delphi
 
Altough I personally find your text processing extremely difficult to understand, it produces perfectly valid XML output. At least on some random WIM xml file I found on the interwebs.

I bet your error hails from your 'pre' parameter. When you call
Delphi-Quellcode:
WIMGetImageInformation
, you're storing the buffer size of the returned XML data. When later writing your modified data by
Delphi-Quellcode:
WIMSetImageInformation
, you're passing the exact same size. This cannot be right. According to MSDN, you'll have to pass the size (in Bytes) of your new xml block. I'm not sure if the trailing '\0' for termination is counted or not.

SlpLow 20. Nov 2013 01:37

AW: Hilfe beim Übersetzen von C zu Delphi
 
Danke! Ich gab ein beispiel aus VC++, um es elegant.
Die Frage war, indem PChar variablen - pName := PChar('Windows 7 HOMEBASIC (x64)').
Und entfernen CR (#13#10) gelöst.


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:26 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz