Recent

Author Topic: how do I know how much to allocate with getmem when calling an api  (Read 5477 times)

jw

  • Full Member
  • ***
  • Posts: 126
here's the code I'm grabbing the drives serial number.

var
serial : pdword;
s : string;

begin
  GetMem(serial, 100);

  GetVolumeInformation(pChar('c:\'), nil, 0, serial, DWord(nil^), DWord(nil^) , nil, 0);

  s:=hexstr(serial^,8);
  FreeMem(serial, 100);

  edit1.text:=copy(s,1,4) + '-' + copy(s,5,8);
end;

ok I got it working but still not sure how it actually works.

pdword?  memory address one word in length being pointed to? so it 2 bytes but already reserved as soon as I define it right

now i need to define how much memory to use from where the pointer points to.  The delphi page used getmem to allocate 260 bytes  I lowered it to 100 but if I say getmem(serial,0) the program still works.

it seems to me the drives serial number is a double word xxxx-xxxx so it seems
getmem(serial,4) would be sufficient.  Does any of this sound right?
                                                     

faber

  • Guest
Re: how do I know how much to allocate with getmem when calling an api
« Reply #1 on: April 20, 2010, 09:02:21 pm »
   function Serial: string;
   var
      SerialNumber : DWORD;
   begin
      GetVolumeInformation(pChar('c:\'),nil,0, @SerialNumber, DWord(^nil), DWord(^nil), nil, 0) ;
      Result :=IntToHex(HiWord(SerialNumber), 4) +' - ' +IntToHex(LoWord(SerialNumber), 4) ;
   end;

jw

  • Full Member
  • ***
  • Posts: 126
Re: how do I know how much to allocate with getmem when calling an api
« Reply #2 on: April 22, 2010, 05:03:19 pm »
thanks

carrots in wrong place
GetVolumeInformation(PChar('c:\'), nil, 0, @SerialNumber, DWORD(nil^), DWORD(nil^), nil, 0);

 

TinyPortal © 2005-2018