Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi GetModuleName wirft Exception (https://www.delphipraxis.net/169329-getmodulename-wirft-exception.html)

Jelen 12. Jul 2012 14:22

GetModuleName wirft Exception
 
Hallo,
hat jemand schon mal versucht, GetModuleName mit einem Handle für die 'TMSSmoothControlsPackPkgD2009.bpl' aufzurufen?
Folgender Code läuft bei mir auf eine Exception, die ich mir nicht erklären kann:
Delphi-Quellcode:
procedure BplInfo (ts: TStrings);
 {+++++++ BplInfo ++++++++++++++++++++++++++++++}
 function BplPath (const BplName: String): AnsiString;
  var MHnd: Cardinal;
      j,k: integer;
      ModuleBuffer: array [0..63] of AnsiChar;
      PathBuffer: array [0..255] of Char;
  begin
   StrPCopy(@ModuleBuffer,BplName);
   MHnd := GetModuleHandle(@ModuleBuffer);
   if MHnd = 0
   then Result := 'nicht geladen'
   else begin
         Result := GetModuleName(MHnd);
        end;
   Result := BplName+': "'+Result+'"';
  end;
 {+++++++ BplInfo ++++++++++++++++++++++++++++++}
 begin
  try
   ts.Append(BplPath('bdertl120.bpl'));
   ts.Append(BplPath('dbrtl120.bpl'));
   ts.Append(BplPath('designide120.bpl'));
   ts.Append(BplPath('rtl120.bpl'));
   ts.Append(BplPath('tee8120.bpl'));
   ts.Append(BplPath('TMSSmoothControlsPackPkgD2009.bpl'));
   ts.Append(BplPath('vcl120.bpl'));
   ts.Append(BplPath('vclactnband120.bpl'));
   ts.Append(BplPath('vcldb120.bpl'));
   ts.Append(BplPath('vclimg120.bpl'));
   ts.Append(BplPath('vclsmp120.bpl'));
   ts.Append(BplPath('vclx120.bpl'));
   ts.Append(BplPath('VirtualTreesD12.bpl'));
   ts.Append(BplPath('xmlrtl120.bpl'));
  except on e: Exception
  do ts.Append('BplInfo: "'+e.Message+'"');
  end;
 end;
Das Ergebnis sieht dann so aus:
bdertl120.bpl: "C:\Windows\system32\bdertl120.bpl"
dbrtl120.bpl: "C:\Windows\system32\dbrtl120.bpl"
designide120.bpl: "C:\Program Files (x86)\CodeGear\RAD Studio\6.0\bin\designide120.bpl"
rtl120.bpl: "C:\Windows\system32\rtl120.bpl"
tee8120.bpl: "C:\Windows\system32\Tee8120.bpl"
BplInfo: "Zugriffsverletzung bei Adresse 003C4761 in Modul 'test.bpl'. Lesen von Adresse 00000038"
Die ersten 5 Aufrufe gehen also sauber durch, und dann kommt die TMS...bpl dran und es kracht.
Mit GetModuleFileName hab' ich es auch probiert, dasselbe Problem.

Hat jemand 'ne Idee?

Bernhard Geyer 12. Jul 2012 16:25

AW: GetModuleName wirft Exception
 
Schau mal wie viel Zeichen "TMSSmoothControlsPackPkgD2009.bpl" und wie groß dein Buffer "ModuleBuffer".
Und jetzt bedenke das 1 Zeichen in D2009 2 Byte lang ist und du fälschlicherweis AnsiChar verwendest.

Wenn mich nicht alles täusch müsstest du auch ein paar Compilerwarnungen bekommen.

shmia 12. Jul 2012 16:31

AW: GetModuleName wirft Exception
 
Wozu diese Zwischenpuffer?
Delphi-Quellcode:
function BplPath(const BplName: String): AnsiString;
  var MHnd: Cardinal;
begin
   MHnd := GetModuleHandleW(PWideChar(BplName));
   if MHnd = 0
   then Result := 'nicht geladen'
   else begin
         Result := GetModuleName(MHnd);
        end;
   Result := BplName+': "'+Result+'"';
end;

himitsu 12. Jul 2012 16:57

AW: GetModuleName wirft Exception
 
Delphi-Quellcode:
function BplPath(const BplName: String): AnsiString;
  var MHnd: Cardinal;
begin
   MHnd := GetModuleHandleW(PWideChar(BplName));
Wenn schon hart GetModuleHandleW und PWideChar verwendet wird, dann auch hart WideString/UnicodeString verwenden.


@Jelen:
Der Compiler hat nicht zufällig von unsicherem String-Zeugs gefaselt?


Ich sag's gerne nochmals (immer und immer wieder :wall: ) :

xxxW + WideChar + PWideChar + WideString/UnicodeString
xxxA + AnsiChar + PAnsiChar + AnsiString
xxx + Char + PChar + String

und niemals irgendwie gemischt

Jelen 12. Jul 2012 19:15

AW: GetModuleName wirft Exception
 
Au weia. Da hab' ich ja mächtig auf dem Schlauch gestanden...

@Bernhard Geyer: Über die 33 Zeichen des Dateinamens hab' ich mir auch Gedanken gemacht.
Aber das war leider wie mit dem Wald und den Bäumen, das mit den Chars und AnsiChars...
Ich dachte immer nur - das kann's nicht sein.

@shmia: Ist mir sehr sympathisch, Deine Kurzform. In meiner jetzigen Fassung ist noch das überflüssige begin - end rausgeflogen.

@himitsu: Das Gefasel mit den bedenklichen Stringumwandlungen hatte ich dem Compiler verboten, weil das ganze Projekt aus einer frühen Delphi-Version stammt, wo String = ShortString war. Beim Portieren des Projekts auf D2009 klemmte es an allen Ecken und Enden, als die ShortStrings zu UnicodeStrings wurden, mit Umwandlung aller String-Deklarationen zu AnsiStrings lief es wesentlich reibungsloser.
In diesem Fall war das natürlich ärgerlich, denn das "Gefasel" hätte mir sicher den Wald gezeigt!

Danke Euch für die Hilfe! :cheers:

himitsu 12. Jul 2012 21:12

AW: GetModuleName wirft Exception
 
Zitat:

wo String = ShortString war.
Delphi 1 oder 2? :shock:

Nja, alles einfach nur auf AnsiString und PAnsiChar abzuändern, hilft auch nicht immer, denn da muß man ebenfalls aufpassen, ob auch alle aufgerufenen Funktionen und vorallem die APIs mit ANSI arbeiten.
PS: Es gibt keine ANSI-TStringList, denn diese hat man in der Unit AnsiStrings glatt vergessen, genauso arbeiten alle VCL-Komponenten fast ausschließlich nur noch mit Unicode.

Delphi-Quellcode:
procedure BplInfo(ts: TStrings);
  procedure AddInfo(const BplName: String);
  var MHnd: THandle;
  begin
    MHnd := GetModuleHandle(PChar(BplName));
    if MHnd = 0 then
      ts.Add(BplName + ': nicht geladen')
    else
      ts.Add(BplName + ': "' + GetModuleName(MHnd) + '"');
  end;
begin
  AddInfo('bdertl120.bpl');
  AddInfo('dbrtl120.bpl');
  AddInfo('designide120.bpl');
  AddInfo('rtl120.bpl');
  AddInfo('tee8120.bpl');
  AddInfo('TMSSmoothControlsPackPkgD2009.bpl');
  AddInfo('vcl120.bpl');
  AddInfo('vclactnband120.bpl');
  AddInfo('vcldb120.bpl');
  AddInfo('vclimg120.bpl');
  AddInfo('vclsmp120.bpl');
  AddInfo('vclx120.bpl');
  AddInfo('VirtualTreesD12.bpl');
  AddInfo('xmlrtl120.bpl');
end;


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