Forum > General

how do I know how much to allocate with getmem when calling an api


here's the code I'm grabbing the drives serial number.

serial : pdword;
s : string;

  GetMem(serial, 100);

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

  FreeMem(serial, 100);

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

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?

   function Serial: string;
      SerialNumber : DWORD;
      GetVolumeInformation(pChar('c:\'),nil,0, @SerialNumber, DWord(^nil), DWord(^nil), nil, 0) ;
      Result :=IntToHex(HiWord(SerialNumber), 4) +' - ' +IntToHex(LoWord(SerialNumber), 4) ;


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


[0] Message Index

Go to full version