Link to home
Start Free TrialLog in
Avatar of sierra20
sierra20

asked on

getwindowsdirectory (netdll.dll???)

Hi

  I use the function GetWindowsDirectory, I have no problem in win95 but in winnt 4, when the function is
  executed, its give me an error :
 
  var directory : string;
  GetWindowsDirectory(PChar(directory),Max_Path);

"""An exception as occurs (EAccessViolation) with the message access violation of address '77F66DEF'
of module 'netdll.dll'. Write of address '00403DD5'.Processus stop """

P.S.:(i translate from the french error)

I dont understand what going on.

Thanks                                          
Avatar of rene_moeller
rene_moeller

Hi sierra20,

I get the same error in NT, but this piece of code works:

var
  Buffer : PChar;
  BufferSize : integer;
begin
  BufferSize := 255;
  GetMem(Buffer, BufferSize);
  GetWindowsDirectory(Buffer, BufferSize);
  ShowMessage(StrPas(Buffer));
  FreeMem(Buffer);
end;

Rene_Moeller
You need to allocate memory for the string before you let Windows overwrite it.  NT is probably more sensitive to memory overwrites and doesn't let you shoot yourself in the foot like Windows '95 does.

Corrected code:
var directory : string;
Begin
  SetLength(Directory, GetWindowsDirectory(Nil, 0)+1);
  GetWindowsDirectory(PChar(directory),Max_Path);
  // etc

The first line allocates enough memory to hold the length of the windows directory returned by GetWindowsDirectory(Nil, 0) and adds one character to hold the trailing null character.
Best wishes,
     -Kristoffer
Hi,
In Windows 95, everything seems to be allowed!? ;-)
You have to allocate heap space by setting the string length to MAX_PATH:

  var directory : string; // creates empty string

  SetLength( directory, MAX_PATH ); // allocates heap space
  GetWindowsDirectory(PChar(directory),MAX_PATH); // now working!

/// John

Sorry!
I was too slow...

/// John
Avatar of sierra20

ASKER

Hi

Kristoffer, you can answers the question because you give me the best answers.

Thanks.

Sierra
ASKER CERTIFIED SOLUTION
Avatar of khenr29j
khenr29j

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial