Solved

api call gives access violation

Posted on 2004-04-03
9
228 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
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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