请问windows下怎样获取当前用户名?
多谢! 问题点数:20、回复次数:16Top
1 楼jww330(追意)回复于 2001-05-24 16:40:00 得分 0
具体一点。Top
2 楼rh(花覆茅檐)回复于 2001-05-24 16:45:00 得分 0
getusernameTop
3 楼verybigbug(等待中)回复于 2001-05-24 16:46:00 得分 0
char buf[64];
GetUserName(buf, sizeof(buf));Top
4 楼rh(花覆茅檐)回复于 2001-05-24 16:46:00 得分 0
BOOL GetUserName(
LPTSTR lpBuffer, // name buffer
LPDWORD nSize // size of name buffer
);
Top
5 楼rh(花覆茅檐)回复于 2001-05-24 16:49:00 得分 0
BOOL GetUserName(
LPTSTR lpBuffer, // name buffer
LPDWORD nSize // size of name buffer
);
Top
6 楼Regen219(雨)回复于 2001-05-24 16:53:00 得分 0
在注册表中的HKEY_LOCAL_MACHINE\Network\Logon下可以找到username,即当前用户名Top
7 楼greatll(流流)回复于 2001-05-25 12:48:00 得分 0
CDaoWorkspace::GetUserName?是CString类型的啊。
MFC中好像没有上面几位大虾说的BOOL GetUserName吧?
Top
8 楼yangmajituipo()回复于 2001-05-25 12:50:00 得分 0
是sdk中的,再看看Top
9 楼greatll(流流)回复于 2001-05-25 12:52:00 得分 0
注册表中HKEY_LOCAL_MACHINE下没有Network键啊(至少2000没有)。Top
10 楼flyswatter(细雨)回复于 2001-05-25 12:59:00 得分 0
The GetUserName function retrieves the user name of the current thread. This is the name of the user currently logged onto the system.
BOOL GetUserName(
LPTSTR lpBuffer, // address of name buffer
LPDWORD nSize // address of size of name buffer
);
Parameters
lpBuffer
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
Pointer to a DWORD variable that, on input, specifies the maximum size, in characters, 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 characters, including the terminating null character.
Return Values
If the function succeeds, the return value is nonzero, 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, callGetLastError.
Remarks
If the current thread is impersonating another client, the GetUserName function returns the user name of the client that the thread is impersonating.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in winbase.h.
Import Library: Use advapi32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.
See Also
System Information Overview, System Information Functions,LookupAccountName
Top
11 楼rh(花覆茅檐)回复于 2001-05-25 13:01:00 得分 10
拜托!我们说的是api!而不是mfc里的类成员函数!Top
12 楼flyswatter(细雨)回复于 2001-05-25 13:02:00 得分 5
参数说明:
LPTSTR lpBuffer接收用户名,字符串
LPDWORD nSize 是个变量,要初始化,指定字符串的长度,返回后接受用户名实际长度
Top
13 楼Regen219(雨)回复于 2001-05-25 13:08:00 得分 0
我没说是2000下的哦Top
14 楼greatll(流流)回复于 2001-05-25 13:10:00 得分 0
哇靠,这么多热情的兄弟,叫我把分给谁好啊?
BTW,偶是新手,谁能告诉俺该怎样给分啊?Top
15 楼kimryo(God is on my side)回复于 2001-05-25 13:11:00 得分 0
char *name=new char[100];
GetUserName(name,100);Top
16 楼Gladiator()回复于 2001-05-25 13:16:00 得分 5
//得到当前用户
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#define UULEN 256
int main(int argc,char *argv[])
{
printf("Whoami tell who are you in nt/2000, -- bingle\n");
//OpenProcess();
HANDLE hp , htoken;
char buff[1024];
unsigned long size = 1024;
TOKEN_USER *tuser;
PSID sid;
char user[UULEN], domain[UULEN];
SID_NAME_USE snu;
hp = htoken = INVALID_HANDLE_VALUE;
hp = GetCurrentProcess();
if(!OpenProcessToken(hp, TOKEN_QUERY, &htoken))
{
printf("OpenProcessToken error : %u\r\n", GetLastError());
goto exit_main;
}
if(!GetTokenInformation(htoken, TokenUser, (void*)buff, size, &size))
{
printf("GetTokenInformation error : %u\r\n", GetLastError());
goto exit_main;
}
tuser = (TOKEN_USER*)buff;
sid = tuser->User.Sid;
size = UULEN;
if(!LookupAccountSid(NULL, sid, user, &size, domain, &size, &snu))
{
printf("LookupAccountSid error : %u\r\n", GetLastError());
goto exit_main;
}
printf("you are '%s\\%s'\r\n", domain, user);
exit_main:
if(htoken != INVALID_HANDLE_VALUE)CloseHandle(htoken);
if(hp != INVALID_HANDLE_VALUE)CloseHandle(hp);
return 0;
}
http://www.i-hy.com/xml
Top




