Solved

api call gives access violation

Posted on 2004-04-03
9
225 Views
Last Modified: 2010-04-05
Te goal is to retrieve the current user when the program starts; username is a global string; However, this code generates an eaccessviolation error. Any assistance would be greatly appriciated.

procedure TForm1.FormCreate(Sender: TObject);
var
user:pchar;
nsize:cardinal;
begin
windows.GetUserName(user,nsize);
username:=strpas(user);
end;
0
Comment
Question by:temporalnomad
9 Comments
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility

I dont get an access violation with your code :

procedure TForm1.FormCreate(Sender: TObject);
var
 user:pchar;
 nsize:cardinal;
 UserName: String;
begin
 GetUserName(user,nsize);
 UserName:= strpas(user);
end;

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
Although, it doesn't work either

try

var
 i : cardinal;
 user:string;
begin
 i:= 255;
 setlength(user,i);
 getusername(pchar(user),i);
 setlength(user,(i-1));
 showmessage(user);
end;

SHane
0
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 125 total points
Comment Utility
function Username: string;
var
  Buffer: array [0..255] of Char;
begin
  Windows.getusername(@Buffer[0], SizeOf(Buffer));
  Result := Buffer;
end;

This should work (untested).
The original code is completely wrong because it does not allocate a buffer at all.
user is an uninitialized local pointer variable so getusername tries to overwrite a random memory area.
nsize is also uninitialized.
0
 
LVL 4

Expert Comment

by:tobjectpascal
Comment Utility
procedure TForm1.Button1Click(Sender: TObject);
var
 UserN: String;
 Len: Dword;
begin
 SetLength(UserN,255);
 Len:=Length(UserN);
 GetUserName(@UserN[1],Len);
 ShowMessage(Trim(UserN)); // UserN:=Trim(UserN);
end;


Code works and tested under 2K, i find it very strange that it refuses to allow GetUserName(whatever,255); very strict.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:tobjectpascal
Comment Utility
Oh yeah now i see why, it has to modify the dword param :)

procedure TForm1.Button1Click(Sender: TObject);
var
 UserN: String;
 Len: Dword;
begin
 SetLength(UserN,255);
 Len:=Length(UserN);
 GetUserName(@UserN[1],Len);
 SetLength(UserN,Len-1);
 ShowMessage(UserN);
end;
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
I can't believe you gave robert_marquardt the points.

Not only did i provide a perfectly good working answer, but i was the first to provide it.

 robert_marquardt,  took my answer and ran with it, changing it to use a buffer array instead.


Shane
0
 
LVL 4

Expert Comment

by:tobjectpascal
Comment Utility
var
  Buffer: array [0..255] of Char;
begin
  Windows.getusername(@Buffer[0], SizeOf(Buffer));
  Result := Buffer;
End;


Does not compile under Delphi 5 AT ALL so yeah as to why he would accept that is beyond me.
0
 

Author Comment

by:temporalnomad
Comment Utility
he gave me the info i was really looking for, about the uninitialized pointer...but i cannot figure out how to grant any 'assist' points...i am new to EE. And i will give you each some, because all responses _were_ helpful...but as i said, i do not know how.
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
Dont worry about it.

Thanks for acknowledging though!

Good luck!

Shane
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now