User talk:ChristianWimmer/GetProcedureAddress Improvements

From Project JEDI Wiki
Jump to navigationJump to search

GetProcedureAddress Improvements

Patrick van Logchem

I was browsing through the JWA code, when it struck me that GetProcedureAddress could use a speed improvement; This method is called a lot, and the way it is written now, it always reserves stack-space while that's only needed if things fail (the first time).

It would be better to use an inline method for this (at least until the compiler starts optimizing per statement block instead of per method block) like this :

 
procedure GetProcedureAddress(var P: Pointer; const ModuleName, ProcName: string);
 
  procedure _GetProcedureAddress;
  var
    ModuleHandle: HMODULE;
  begin
    ModuleHandle := GetModuleHandle(PChar(ModuleName));
    if ModuleHandle = 0 then
    begin
      ModuleHandle := LoadLibrary(PChar(ModuleName));
      if ModuleHandle = 0 then
        raise EJwaLoadLibraryError.CreateFmt(RsELibraryNotFound, [ModuleName]);
    end;
    P := Pointer(GetProcAddress(ModuleHandle, PChar(ProcName)));
    if not Assigned(P) then
      raise EJwaGetProcAddressError.CreateFmt(RsEFunctionNotFound, [ModuleName, ProcName]);
  end;
 
begin
  if not Assigned(P) then
    _GetProcedureAddress; // Only do stack-allocation when really necessary
end;

Comments

  • ChristianWimmer 11:25, 3 February 2010 (UTC) GetProcedureAddress could also be made inline (only newer Delphi. Need to check)

Conrad T. Pino 18:00, 3 February 2010 (UTC): This is a good one; I vote yes. I'd like to consider other improvements should we proceed with this one.

Todo