Solved

api call gives access violation

Posted on 2004-04-03
9
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10750627

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
ID: 10750633
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
ID: 10750814
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:tobjectpascal
ID: 10752417
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
 
LVL 4

Expert Comment

by:tobjectpascal
ID: 10752425
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
ID: 10753450
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
ID: 10753796
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
ID: 10754414
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
ID: 10754573
Dont worry about it.

Thanks for acknowledging though!

Good luck!

Shane
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

624 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