Thema: Delphi MySQL-Header

Einzelnen Beitrag anzeigen

Schorschi5566

Registriert seit: 6. Feb 2006
197 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#6

AW: MySQL-Header

  Alt 4. Aug 2010, 10:27
Hallo Himitsu,

wenn man jetzt noch einen Mechanismus für die Memory-loaded-DLL einbaut, ist's doch schon fast perfekt.

Bei mysql.pas habe ich es so reinoperiert...

Delphi-Quellcode:
var
  libmysql_handle: HMODULE = 0;
  libmysql_mem_handle: PBTMemoryModule = nil;
  libmysql_mem_buffer : Pointer;
  libmysql_in_memory : Boolean;

.
.
.

// Load library from Ressource into Memory. Internal Resname = libmysql
procedure libmysql_mem_load(name : PChar);

.
.
.

procedure libmysql_mem_load(name : PChar);
var
  ms : TMemoryStream;
  rs : TResourceStream;
  m_DllDataSize : Int64;
begin
  if libmysql_mem_handle = nil then
  begin
    if 0 <> FindResource(hInstance, 'libmysql', RT_RCDATA) then
    begin
      rs := TResourceStream.Create(hInstance, 'libmysql', RT_RCDATA);
      ms := TMemoryStream.Create;
      try
        ms.LoadFromStream(rs);
        ms.Position := 0;
        m_DllDataSize := ms.Size;
        libmysql_mem_buffer := GetMemory(m_DllDataSize);
        ms.Read(libmysql_mem_buffer^, m_DllDataSize);
      finally
        ms.Free;
        rs.Free;
      end;
      if libmysql_mem_buffer <> nil then
      begin
        libmysql_mem_handle := BTMemoryLoadLibary(libmysql_mem_buffer, m_DllDataSize);
        if libmysql_mem_handle = nil then
        begin
          internal_libmysql_status := LIBMYSQL_MISSING;
          raise Exception.CreateFmt(ELibraryNotFound, [name]);
        end
        else
        begin
          libmysql_in_memory := True;
          internal_libmysql_ClientVersion := mysql_get_client_version;
          if internal_libmysql_ClientVersion >= mysql_not_compatible_version then
          begin
            internal_libmysql_status := LIBMYSQL_INCOMPATIBLE;
            raise Exception.CreateFmt(sLibMySql_Version_Incompatible, [mysql_get_client_info]);
          end
          else
            internal_libmysql_status := LIBMYSQL_READY;
        end;
      end;
    end;
  end;
end;

procedure libmysql_free;
begin
  if libmysql_handle <> 0 then
    FreeLibrary(libmysql_handle);
  libmysql_handle := 0;
  if libmysql_mem_handle <> nil then
  begin
    BTMemoryFreeLibrary(libmysql_mem_handle);
    FreeMemory(libmysql_mem_buffer);
  end;
  libmysql_mem_handle := nil;
  libmysql_in_memory := False;
  internal_libmysql_status := LIBMYSQL_UNDEFINED;
  FinalizeLibmysql;
end;

procedure LoadProcAddress(var proc: FARPROC; name: PAnsiChar);
var
  ModulPath: String;
begin
  if proc = nil then
  begin
    if (libmysql_handle = 0) and (libmysql_mem_handle = nil) then
      raise Exception.Create(sLibMySql_Undefined);
    proc := GetProcAddress(name);
    if proc = nil then
    begin
      internal_libmysql_status := LIBMYSQL_INCOMPATIBLE;
      SetLength(ModulPath, MAX_PATH);
      SetLength(ModulPath, GetModuleFileName(libmysql_handle,
          Pointer(ModulPath), MAX_PATH));
      raise Exception.CreateFmt(EProcNotFound, [name, ModulPath])
    end;
    PushProc(@proc);
  end;
end;

function GetProcAddress(lpProcName: LPCSTR): FARPROC; overload;
begin
  if libmysql_in_memory then
    result := BTMemoryGetProcAddress(libmysql_mem_handle, lpProcName)
  else
    result := GetProcAddress(libmysql_handle, lpProcName);
end;
Dann habe ich noch die alten Aufrufe von GetProcAddress durch die neuen ersetzt. Das war's.

Verwendet habe ich die Unit BTMemoryModule von Martin Offenwanger. Hier!



Grüße,
Uwe
Uwe
  Mit Zitat antworten Zitat