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 Function (WIMGetMountedImageInfo) (https://www.delphipraxis.net/178081-function-wimgetmountedimageinfo.html)

SlpLow 16. Dez 2013 17:38

Function (WIMGetMountedImageInfo)
 
Hallo! Hilfe bitte!
Warum function (WIMGetMountedImageInfo) nicht an zwei montierten Bildern zu arbeiten?
Delphi-Quellcode:
procedure TForm1.btn2Click(Sender: TObject);
var
  Created, Count: DWORD;
  Buffer: WIM_MOUNT_INFO_LEVEL0;
  size: Integer;
  wPath, wFile, wIndex, wWrite: string;
begin
  if not (WIMGetMountedImageInfo(0, @Count, @Buffer, size, @created)) then
  begin
    ShowMessage('Error Open ' + IntToStr(GetLastError));
    Exit;
  end
  else
  begin
    if Count > 0 then
    begin
//      for i := 1 to Count - 1 do
      begin
        wPath := Buffer.MountPath;
        wFile := Buffer.WimPath;
        wIndex := IntToStr(Buffer.ImageIndex);
        // Memo1.Lines.Add(BoolToStr(NewInfoBuf.MountedForRW, true));
        if Buffer.MountedForRW then
          wWrite := 'RW'
        else
          wWrite := 'R';
      end;
      Memo1.Lines.Add(wPath);
      Memo1.Lines.Add(wFile);
      Memo1.Lines.Add(wIndex);
      Memo1.Lines.Add(wWrite);
    end;
end;

Union 16. Dez 2013 20:11

AW: Function (WIMGetMountedImageInfo)
 
See the documentation

SlpLow 16. Dez 2013 21:56

AW: Function (WIMGetMountedImageInfo)
 
Zitat:

Zitat von Union (Beitrag 1240017)

Thank you! I studied it for three days.
When one image, everything works. If two simultaneously in different folders is not running. It is clear that there are many records but how to handle it I do not know.

himitsu 16. Dez 2013 22:05

AW: Function (WIMGetMountedImageInfo)
 
Then you did not read properly.

Zitat:

pMountInfo

[out opt] Pointer to a variable that receives the array of mounted image structures. The size of the information written varies depending on the type of structured defined by the fInfoLevelId parameter.

cbMountInfoLength

[in] The size of the buffer pointed to by the pMountInfo parameter, in bytes.

pcbReturnLength ( @created ? )

[out] A pointer to a variable in which the function returns the size of the requested information. If ...

SlpLow 17. Dez 2013 00:19

AW: Function (WIMGetMountedImageInfo)
 
I can tell you one thing! I do not know how to do, how to announce in the variables and how to apply.
Is all that is understood.
Delphi-Quellcode:
 if not (WIMGetMountedImageInfo([COLOR="Red"]MountedImageInfoLevel0[/COLOR], @Count, @Buffer, size, @created)) then
Yours!

SlpLow 17. Dez 2013 00:21

AW: Function (WIMGetMountedImageInfo)
 
I can tell you one thing! I do not know how to do, how to announce in the variables and how to apply.
Is all that is understood - MountedImageInfoLevel0.
Delphi-Quellcode:
 if not (WIMGetMountedImageInfo(MountedImageInfoLevel0, @Count, @Buffer, size, @created)) then
Yours!

Zacherl 17. Dez 2013 01:44

AW: Function (WIMGetMountedImageInfo)
 
Zitat:

Zitat von SlpLow (Beitrag 1240032)
When one image, everything works

I doubt that ... :roll:

You were just "lucky". The uninitialized size variable contained a sufficient size for the returned data struct. The api call in it's current form destroys a random memory region (directly behind the output buffer) in your program (buffer overflow), if there is more than one mounted image.

I cant really test it, because i have no mounted images, but it should work like this. Happy copy and paste:
Delphi-Quellcode:
type
  _WIM_MOUNT_LIST = packed record
    WimPath: array[0..MAX_PATH - 1] of WideChar;
    MountPath: array[0..MAX_PATH - 1] of WideChar;
    ImageIndex: DWord;
    MountedForRW: BOOL;
  end;
  WIM_MOUNT_LIST = _WIM_MOUNT_LIST;
  PWIM_MOUNT_LIST = ^WIM_MOUNT_LIST;
type
  WIM_MOUNT_INFO_LEVEL0 = _WIM_MOUNT_LIST;
  PWIM_MOUNT_INFO_LEVEL0 = ^WIM_MOUNT_LIST;
type
  MOUNTED_IMAGE_INFO_LEVELS = DWord;
const
  MountedImageInfoLevel0 = 1;
var
  WIMGetMountedImageInfo: function(fInfoLevelId: MOUNTED_IMAGE_INFO_LEVELS;
    var dwImageCount: DWord; pMountInfo: Pointer; cbMountInfoLength: DWord;
    var cbReturnLength: DWord): BOOL; stdcall;
type
  PWIM_MOUNT_INFO_LEVEL0_LIST = ^WIM_MOUNT_INFO_LEVEL0_LIST;
  WIM_MOUNT_INFO_LEVEL0_LIST = array[0..0] of WIM_MOUNT_INFO_LEVEL0;
var
  Success: Boolean;
  Buffer: PWIM_MOUNT_INFO_LEVEL0_LIST;
  BufferLength,
  ReturnLength,
  ImageCount: DWord;
  I: Integer;
begin
  @WIMGetMountedImageInfo := GetProcAddress(LoadLibrary('wimgapi.dll'), 'WIMGetMountedImageInfo');
  BufferLength := SizeOf(WIM_MOUNT_INFO_LEVEL0) * 8;
  GetMem(Buffer, BufferLength);
  try
    repeat
      Success := WIMGetMountedImageInfo(MountedImageInfoLevel0, ImageCount, Buffer, BufferLength,
        ReturnLength);
      if (Success) then
      begin
        for I := 0 to ImageCount - 1 do
        begin
          ShowMessage(StrPas(PChar(@Buffer^[I].WimPath[0])));
        end;
      end else if (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
      begin
        ReallocMem(Buffer, ReturnLength);
      end;
    until (Success) or (GetLastError <> ERROR_INSUFFICIENT_BUFFER);
  finally
    FreeMem(Buffer);
  end;
end;

SlpLow 17. Dez 2013 02:35

AW: Function (WIMGetMountedImageInfo)
 
Zacherl! Vielen, vielen Dank! Ich werde versuchen, zu verstehen.
Adel liegt (sitzt) im Gemüte, nicht im Geblüte.

himitsu! Danke! Ich habe wenig Erfahrung, aber ich werde lernen!

SlpLow 17. Dez 2013 05:27

AW: Function (WIMGetMountedImageInfo)
 
Thank you again!
Strings and integer okay!
A logical always returns a -1 (RW, R) instead of 0 and 1
Delphi-Quellcode:
Idx: = BoolToStr (Buffer ^ [I]. MountedForRW, False);
:
or integer 257 (RW) or 1 (R) instead of 0 and 1
Delphi-Quellcode:
idx: = IntToStr (Integer (Buffer ^ [I]. MountedForRW));
:
Because it LongBool? Is there a solution?

I do not know, correct or not, but the problem disappeared when
Delphi-Quellcode:
const
MountedImageInfoLevel0 = 0;
Is it right decision?

Zacherl 17. Dez 2013 22:43

AW: Function (WIMGetMountedImageInfo)
 
Thats because of the C style boolean definition:
FALSE = 0
TRUE = everything else

You can just check the flag like this:
Delphi-Quellcode:
if (Buffer^[I].MountedForRW) then
or
Delphi-Quellcode:
if (not Buffer^[I].MountedForRW) then
If your problem is just the bool to string conversion you can go for this:
Delphi-Quellcode:
BoolToStr(Buffer^[I].MountedForRW, true)
or just write you an own function, if you dont like the -1 or the default bool strings.

Sir Rufo 17. Dez 2013 23:39

AW: Function (WIMGetMountedImageInfo)
 
@Zacherl

Folgendes würde ich ändern
  • Wenn Delphi-Referenz durchsuchenLoadLibrary dann auch Delphi-Referenz durchsuchenFreeLibrary inkl. Fehlerbehandlung (Exception)
  • Delphi-Referenz durchsuchenGetLastError nur einmal aufrufen
    Zitat:

    Der Aufruf dieser Funktion setzt in der Regel den Fehlerstatus des Betriebssystems zurück.
  • Da alle anderen Fehlermeldungen in diesem Kontext eine Ausnahme sind, würde ich auch eine Exception werfen
  • Statt
    Delphi-Quellcode:
    PChar
    auf jeden Fall
    Delphi-Quellcode:
    PWideChar
    nehmen, denn hier gibt es nur
    Delphi-Quellcode:
    WCHAR
    (und
    Delphi-Quellcode:
    PChar
    ist abhängig vom Compiler -> bei der nächsten Umstellung des Standard-String-Typs sind wieder alle am Weinen :mrgreen: ).
    Wenn sich die API ändert, dann ändert die sich eben ... nachführen
Delphi-Quellcode:
var
  Lwimgapi                                 : HMODULE;
  LSuccess                                 : Boolean;
  LBuffer                                  : PWIM_MOUNT_INFO_LEVEL0_LIST;
  LBufferLength, LReturnLength, LImageCount : DWord;
  LIdx                                     : Integer;
  LLastError                               : Cardinal;

begin
  Lwimgapi := LoadLibrary( 'wimgapi.dll' );
  if Lwimgapi = 0
  then
    RaiseLastOSError;

  try
    @WIMGetMountedImageInfo := GetProcAddress( Lwimgapi, 'WIMGetMountedImageInfo' );
    LBufferLength          := SizeOf( WIM_MOUNT_INFO_LEVEL0 ) * 8;
    GetMem( LBuffer, LBufferLength );
    try
      repeat
        LSuccess := WIMGetMountedImageInfo( MountedImageInfoLevel0, LImageCount, LBuffer, LBufferLength, LReturnLength );
        if LSuccess
        then
          begin
            for LIdx := 0 to LImageCount - 1 do
              begin
                ShowMessage( StrPas( PWideChar( @LBuffer^[LIdx].WimPath[0] ) ) );
              end;
          end
        else
          begin
            LLastError := GetLastError;
            case LLastError of
              ERROR_INSUFFICIENT_BUFFER :
                begin
                  ReallocMem( LBuffer, LReturnLength );
                end;
            else
              RaiseLastOSError( LLastError );
            end;
          end;
      until LSuccess;
    finally
      FreeMem( LBuffer );
    end;
  finally
    FreeLibrary( Lwimgapi );
  end;
end;
EDIT
Delphi-Quellcode:
GetProcAddress
ist auch noch ein Kandidat zum Prüfen ... aber jetzt nicht mehr :)

Zacherl 18. Dez 2013 00:07

AW: Function (WIMGetMountedImageInfo)
 
Dein Code sieht auf jeden Fall besser aus, als mein nächtlicher Versuch :), aber der Hinweis zu GetLastError in der Delphi Hilfe ist definitiv falsch :D Habe es unter verschiedenen Windows Versionen getestet und der Error Code wird auf jeden Fall nicht resetted. Im MSDN ist unter MSDN-Library durchsuchenGetLastError ebenfalls kein Hinweis darauf zu finden.

Sir Rufo 18. Dez 2013 00:25

AW: Function (WIMGetMountedImageInfo)
 
Zitat:

Zitat von Zacherl (Beitrag 1240158)
Dein Code sieht auf jeden Fall besser aus, als mein nächtlicher Versuch :), aber der Hinweis zu GetLastError in der Delphi Hilfe ist definitiv falsch :D Habe es unter verschiedenen Windows Versionen getestet und der Error Code wird auf jeden Fall nicht resetted. Im MSDN ist unter MSDN-Library durchsuchenGetLastError ebenfalls kein Hinweis darauf zu finden.

Nun ja irgendwie schon
Zitat:

You should call the GetLastError function immediately ...
Also immer direkt nach dem API Aufruf ist gewährleistet, dass da der passende Fehlercode zu finden ist.
Danach kann schon wieder sonst was drin stehen (you never know).

Potentiell handelt man sich so diese unliebsamen SideEffects ein ;)

Zacherl 18. Dez 2013 01:40

AW: Function (WIMGetMountedImageInfo)
 
Der Vollständigkeit halber:
Zitat:

You should call the GetLastError function immediately when a function's return value indicates that such a call will return useful data. That is because some functions call SetLastError with a zero when they succeed, wiping out the error code set by the most recently failed function.
Solange man also nicht bewusst eine andere API / Funktion aufruft, sollte der Wert von GetLastError demnach unverändert bleiben. Empfinde den Text in der Delphi Hilfe deshalb nach wie vor als unzutreffend, da GetLastError selbst nichts resetted.

Sir Rufo 18. Dez 2013 02:08

AW: Function (WIMGetMountedImageInfo)
 
Problematisch ist aber eben der unbewusste Aufruf von API Funktionen (weil die mit einem harmlosen Befehl irgendwo versteckt aufgerufen werden).

Bei MS wird es erklärt und Emba schürt Angst ;)

SlpLow 18. Dez 2013 20:34

AW: Function (WIMGetMountedImageInfo)
 
Zitat:

Zitat von Zacherl (Beitrag 1240154)
Thats because of the C style boolean definition:
You can just check the flag like this:
Delphi-Quellcode:
if (Buffer^[I].MountedForRW) then

Genau. Aber richtig identifiziert das nur, wenn Sie die Konstanten ändern
MountedImageInfoLevel0 = 0;
Warum?

Sir Rufo Vielen Dank!

Zacherl 19. Dez 2013 00:03

AW: Function (WIMGetMountedImageInfo)
 
Zitat:

Zitat von SlpLow (Beitrag 1240292)
Genau. Aber richtig identifiziert das nur, wenn Sie die Konstanten ändern
MountedImageInfoLevel0 = 0;
Warum?

I dont know the correct value for that constant. I could not found it in the MSDN, but i googled it and it seems to be 1.

SlpLow 19. Dez 2013 01:00

AW: Function (WIMGetMountedImageInfo)
 
Zitat:

Zitat von Zacherl (Beitrag 1240302)
I dont know the correct value for that constant. I could not found it in the MSDN, but i googled it and it seems to be 1.

In den Header-Dateien = 1 auch.

// MOUNTED_IMAGE_INFO_LEVELS
MountedImageInfoLevel0 = 1;
MountedImageInfoLevel1 = 2;
MountedImageInfoLevelInvalid = 3;


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:04 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