Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

api call gives access violation

Posted on 2004-04-03
9
Medium Priority
?
233 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 375 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Technology Partners: 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!

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

721 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