Calling GetUserName

B

BMermuys

Hi,
inline

Philip Carnstam said:
Hi,

This is driving me nuts... How do I call the advapi32 function GetUserName.
Syntax:
bool GetUserName(String User, ref int Sz);

Use a stringbuffer instead:

[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
extern static int GetUserName (StringBuilder name, ref int sz);

static string GetUserName()
{
int sz = 0;
StringBuilder sbName = new StringBuilder(sz);
GetUserName ( sbName, ref sz ); // get size


sbName.Capacity = sz; // allocates space for the name
GetUserName ( sbName, ref sz ); // get name
return sbName.ToString();
}

Have a look at the static Environment.Username property. It does the same
as GetUserName().

hth,
greetings
 
P

Philip Carnstam

Hi,

This is driving me nuts... How do I call the advapi32 function GetUserName.
Syntax:
bool GetUserName(String User, ref int Sz);

This returns the size of the would be returned string User, but when I try
to set User as a reference I get an execution error. Why?
All I can find in MSDN is that when calling this function from VB I should
use byval for User. This surprises me though, since that is not supposed to
return anything.

Thanks for any help,
Philip
 
B

Bram

Hi,

read between your lines. I don't know much about VB but the principles are
the same.

Philip Carnstam said:
Hi,

This is driving me nuts... How do I call the advapi32 function GetUserName.
Syntax:
bool GetUserName(String User, ref int Sz);

This returns the size of the would be returned string User, but when I try
to set User as a reference I get an execution error. Why?
All I can find in MSDN is that when calling this function from VB I should
use byval for User. This surprises me though, since that is not supposed to
return anything.

You are right, it does not return something like reference arguments. You
just have to supply a buffer which must be filled with the user name. So it
does not need to return a different buffer to you. It just fills the
supplied buffer, which you still can access after the function returns. So
specifying 'string' by value is just fine.
Some remarks: you should specify 'int' as the return type as BOOL is defined
to be 4 bytes wide.
I would also use a character array (char[]) of sufficient length instead of
a string.
I just tried this myself, and for some reason I don't get the ansi version
to work. So you might need to specify CharSet.Unicode as well (otherwise it
defaults to ansi).

This should work (C#)

[DllImport("advapi32.dll"), CharSet=CharSet.Unicode)]
static extern int GetUserName(char[], ref int length);

public void SomeMethod()
{
char[] username = new char[32];
int length = 32;
int result = GetUserName(username, ref length);

MessageBox.Show(username); // let's show it;
}

Hope this helps,

Bram.
 
P

Philip Carnstam

Thanks,

Has been a while since I used InterOp, I was using UTF8 encoding...

Thanks,
Philip

Bram said:
Hi,

read between your lines. I don't know much about VB but the principles are
the same.

Philip Carnstam said:
Hi,

This is driving me nuts... How do I call the advapi32 function GetUserName.
Syntax:
bool GetUserName(String User, ref int Sz);

This returns the size of the would be returned string User, but when I try
to set User as a reference I get an execution error. Why?
All I can find in MSDN is that when calling this function from VB I should
use byval for User. This surprises me though, since that is not supposed to
return anything.

You are right, it does not return something like reference arguments. You
just have to supply a buffer which must be filled with the user name. So it
does not need to return a different buffer to you. It just fills the
supplied buffer, which you still can access after the function returns. So
specifying 'string' by value is just fine.
Some remarks: you should specify 'int' as the return type as BOOL is defined
to be 4 bytes wide.
I would also use a character array (char[]) of sufficient length instead of
a string.
I just tried this myself, and for some reason I don't get the ansi version
to work. So you might need to specify CharSet.Unicode as well (otherwise it
defaults to ansi).

This should work (C#)

[DllImport("advapi32.dll"), CharSet=CharSet.Unicode)]
static extern int GetUserName(char[], ref int length);

public void SomeMethod()
{
char[] username = new char[32];
int length = 32;
int result = GetUserName(username, ref length);

MessageBox.Show(username); // let's show it;
}

Hope this helps,

Bram.
Thanks for any help,
Philip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top