LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Computer USer name

Is there a way to read the computer login name using cvi?

cvier
0 Kudos
Message 1 of 11
(4,640 Views)
#include

char UserName[100] = "";
DWORD *NrOfChars;

*NrOfChars = 100;
GetUserName(UserName, NrOfChars);


Good luck!

Erwin Timmerman


Here's the description from the Windows SDK:

BOOL GetUserName(
LPTSTR lpBuffer, // name buffer
LPDWORD nSize // size of name buffer
);

Parameters
lpBuffer
[out] Pointer to the buffer to receive the null-terminated string containing the user's logon name. If this buffer is not large enough to contain the entire user name, the function fails. A buffer size of (UNLEN + 1) characters will hold the maximum length user name including the terminating null character. UNLEN is defined in Lmcons.h.
nSize
[in/out] Pointer to a DWORD variable that, on input, specifies the maximum size, in TCHARs, of the buffer specified by the lpBuffer parameter. If the function succeeds, the variable receives the number of characters copied to the buffer. If the buffer is not large enough, the function fails and the variable receives the required buffer size, in TCHARs, including the terminating null character.
Return Values
If the function succeeds, the return value is a nonzero value, and the variable pointed to by nSize contains the number of characters copied to the buffer specified by lpBuffer, including the terminating null character.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
If the current thread is impersonating another client, the GetUserName function returns the user name of the client that the thread is impersonating.
0 Kudos
Message 2 of 11
(4,638 Views)
#include should be #include windows.h, with windows.h between triangular brackets.
0 Kudos
Message 3 of 11
(4,635 Views)
Tried it, using CVI5.5.1 with win SDK packaged within.

Getting a dereference of pointer on the *NrOfChars = 100;

cvier
0 Kudos
Message 4 of 11
(4,627 Views)
In the example above, no memory was ever allocated to store NrOfChars and is thus giving an access violation; this should work however:

char UserName[100] = "";
DWORD NrOfChars = sizeof(UserName);

GetUserName(UserName, &NrOfChars);

Regards,

Alex
0 Kudos
Message 5 of 11
(4,623 Views)
Works, reads back user login name as desired from a win2K machine.

Thanks for your help on this.
cvier
0 Kudos
Message 6 of 11
(4,620 Views)

Hi,

I am trying to get the username by using same function GetUserName from windows API.

But soon I include windows.h to my project, I am getting re-declaration errors as some functions were duplicated in utility.h.  I also use utility.h to get Date, time and for Timedelay. Can Any one help me here...

Attached are my errors.

Thanks in Advance..

mama007

0 Kudos
Message 7 of 11
(4,326 Views)
There is also "GetCurrentUser" from the Programmer's toolbox, and I think it has been there since at least CVI 4.

JB
--
To whom it may concern: My alias is also my nickname, I've had it since I was a (very) skinny basketball-playing teen. OK, so I've got a 38 inch waist now, but my hometown friends haven't shaken that appellation for me. I trust that you will someday be OK with that alias, as I have been with that nickname.
0 Kudos
Message 8 of 11
(4,320 Views)
Make sure that the #include "windows.h" statement is before all other #include statements in your code. Can lead to all sorts of problems if it is not...
 
JR
0 Kudos
Message 9 of 11
(4,314 Views)
Used the function from Programmers ToolBox and the problem sorted. Found many other windows functiosn there. I did checked in the CVI help index for the function before and it didn't gave me results?...
Thanks for the Help...
0 Kudos
Message 10 of 11
(4,291 Views)