Sometimes we might need to find the Windows Username during program execution. I personally needed this for saving files to the desktop. In Windows 7, the path to the desktop is C:\users\SystemSpecificUsername\desktop.
You might need it for a myriad of other reasons. I found this in a forum where I think someone was looking for a solution to this, he found it himself then posted it for all to see (Here is the original thread). Anyway, hope you find a use for it.
This function accepts a char sent to it and writes the current username into it.
This program will print the username of the current user on a Windows system.
#include <stdio.h>
#include <windows.h>
void username(char *u);
#define INFO_BUFFER_SIZE 32767
void main()
{
char name[20];
username(name);
printf("%s",name);
}
void username(char *u) //this function rewrites a char with the username of the windows user.
{
char *c;
TCHAR infoBuf[INFO_BUFFER_SIZE];
DWORD bufCharCount = INFO_BUFFER_SIZE;
bufCharCount = INFO_BUFFER_SIZE;
GetUserName(infoBuf,&bufCharCount);
c=&infoBuf[0];
while(*c!='\0')
{
*u=*c;
u++;
c++;
}
*u='\0';
}
You might need it for a myriad of other reasons. I found this in a forum where I think someone was looking for a solution to this, he found it himself then posted it for all to see (Here is the original thread). Anyway, hope you find a use for it.
This function accepts a char sent to it and writes the current username into it.
This program will print the username of the current user on a Windows system.
#include <stdio.h>
#include <windows.h>
void username(char *u);
#define INFO_BUFFER_SIZE 32767
void main()
{
char name[20];
username(name);
printf("%s",name);
}
void username(char *u) //this function rewrites a char with the username of the windows user.
{
char *c;
TCHAR infoBuf[INFO_BUFFER_SIZE];
DWORD bufCharCount = INFO_BUFFER_SIZE;
bufCharCount = INFO_BUFFER_SIZE;
GetUserName(infoBuf,&bufCharCount);
c=&infoBuf[0];
while(*c!='\0')
{
*u=*c;
u++;
c++;
}
*u='\0';
}
No comments:
Post a Comment