Einzelnen Beitrag anzeigen

helgew

Registriert seit: 30. Jul 2008
125 Beiträge
 
#1

Problem mit Strings in DLL

  Alt 3. Aug 2008, 01:03
Guten Abend,

Wie bekomme ich es gebacken, Strings global verfügbar zu machen?

Mein Ansatz bisher :

Delphi-Quellcode:
unit GlobalStrings;

interface
  uses
    windows;

type TGlobalString = packed record
  length: WORD;
  hMapObj: THandle;
end;



function SetGlobalString(str: String): TGlobalString;
function GetGlobalString(gstr: TGlobalString): String;
procedure FreeGlobalString(var gstr: TGlobalString);


implementation


function SetGlobalString(str: String): TGlobalString;
var
  pstr : PChar;
begin
  result.length := length(str);
  result.hMapObj := CreateFileMapping(
                INVALID_HANDLE_VALUE, // use paging file
                nil, // default security attributes
                PAGE_READWRITE, // read/write access
                0, // size: high 32-bits
                result.length, // size: low 32-bits
                'dllmemfilemap'); // name of map object

  pstr := MapViewOfFile(
                result.hMapObj , // object to map view of
                FILE_MAP_WRITE, // read/write access
                0, // high offset: map from
                0, // low offset: beginning
                0); // default: map entire file
  CopyMemory(pstr,@str[1],result.length);
end;

procedure FreeGlobalString(var gstr: TGlobalString);
begin
  if gstr.hMapObj <>0 then
  begin
    gstr.length := 0;
    CloseHandle(gstr.hMapObj);
    gstr.hMapObj := 0;
  end;
end;

function GetGlobalString(gstr: TGlobalString): String;
var
  pstr : PChar;
begin
  if gstr.hMapObj <> 0 then
  begin
    pstr := MapViewOfFile(
                gstr.hMapObj , // object to map view of
                FILE_MAP_READ, // read/write access
                0, // high offset: map from
                0, // low offset: beginning
                0); // default: map entire file
    result := string(pstr);
    setlength(result,gstr.length);
  end;
end;

end.
Das ist noch nicht ausgegoren, aber wann muss ich einen solchen String erstellen?
  Mit Zitat antworten Zitat