Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

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                                          
0
sierra20
Asked:
sierra20
1 Solution
 
rene_moellerCommented:
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
0
 
khenr29jCommented:
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
0
 
erajojCommented:
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
erajojCommented:
Sorry!
I was too slow...

/// John
0
 
sierra20Author Commented:
Hi

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

Thanks.

Sierra
0
 
khenr29jCommented:
Here you go Sierra.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now