Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2007 Feb : PChar - Fill Buffer

www.cryer.info
Managed Newsgroup Archive

PChar - Fill Buffer

Subject:PChar - Fill Buffer
Posted by:"Churc" (cebas..@gmail.com)
Date:14 Feb 2007 04:27:41

Helloo

I would like to know if im doing it correctly! x|

Im was learning how to pass data between DLL/EXE - EXE/DLL without
using Memory Managers, and reading many articles and people helping me
here, im a little confused yet, about the BUFFER!

Example

In a DLL i have the code:

(* DLL *)

procedure CreateMachineID(lpBuffer: PChar; var nBuffSize: Integer);
StdCall;
var
  szBuff: String;

Function SerialNum: String;
var
  Serial, DirLen, Flags: DWORD;
  DLabel : array[0..11] of Char;
begin
  try
    GetVolumeInformation(PChar(GetSysDrive), dLabel, 12, @Serial,
DirLen, Flags, nil, 0);
    Result := LowerCase(Copy(IntToHex(Serial, 8), 1, 4));
  except
    Result := '0000';
  end;
end;

Function RandomChars(nLen: Integer): String;
const
  MACHINEID_BASE = '1234567890abcdefghijklmnopqrstuvwxyz';
var
  S: String;
  i, N: integer;
begin
  Randomize;
  for i := 1 to nLen do
  begin
    N := Random(Length(MACHINEID_BASE)) + 1;
    S := S + MACHINEID_BASE[N];
  end;
  Result := S;
end;

begin
  szBuff := SerialNum + RandomChars(4);

  (* HERE - if the buffer is less than the Length of szBuff
     then i send the correct buffer length back and do nothing...
     Its like to get the correct buffer passing a nil as lpBuffer...
     Correct? *)

  if (lpBuffer = nil) or (nBuffSize <= Length(szBuff)) then
  nBuffSize := Length(szBuff) + 1 else
  begin

    (* HERE - if the buffer is more than then length of szBuff
       then i make a correction, to tell the routine the correct

       length...
       Correct? *)

    nBuffSize := Length(szBuff) + 1;

    (* HERE - i pass the data... *)
    StrLCopy(lpBuffer, PChar(szBuff), nBuffSize - 1);
  end;
end;

In a EXE im doing the following...

(* EXE *)

TCreateMachineIDProc = record
    Proc: procedure (lpBuffer: PChar; var nBuffSize: Integer); StdCall;
    hHandle: THandle;
  end;

var
  CreateMachineIDProc: TCreateMachineIDProc;

Function GetMachineID: String;
var
  iLen: Integer;
begin
  Result := ReadFileSetting('MACHINE_ID', '-1', '');
  if (Result = '-1') or (Length(Result) <> 8) then
  with CreateMachineIDProc do
  begin
    if (hHandle = 0) then
    hHandle := LoadLibrary('mydll.dll');
    if (hHandle <> 0) then
    begin
      @Proc := GetProcAddress(hHandle, 'CreateMachineID');
      if Assigned(Proc) then
      begin
        (* HERE - i set some length... can be maybe 10 since this
           function will not return more than 8 chars...
           Correct? *)
        iLen := MAX_PATH;

        Proc(PChar(Result), iLen);

       (* HERE - if then iLen, is more than the first buffer size
           that i sent, then the buffer size was to small... we need
           to call again with the correct buffer...
           Correct? *)
        if (iLen > MAX_PATH) then
        Proc(PChar(Result), iLen);

        (* HERE - with the correct buffer size, we set the correct
           length for the function results.... *)

        SetLength(Result, iLen);
      end;
      Result := PChar(Result);
      FreeLibrary(hHandle);
    end;
    if (Length(Result) = 8) then
    WriteFileSetting('MACHINE_ID', Result, '');
  end;
end;


My question is about the buffer, if im doing it correctly or not, i was
calling the function 2 times, one to get the correct buffer size and
another to get the content, but, i think its decreasing performance,
since the function need to do the things 2 times, one for get the
correct buffer and another to pass data...

Please someone can helpme? :D

Thanks...

Best Regards for all!

Carlos

--

Replies:

www.cryer.info
Managed Newsgroup Archive