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
Solved

api call gives access violation

Posted on 2004-04-03
9
229 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

840 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