Thread: Getting current user username

  1. #1
    Registered User Arhaikos's Avatar
    Join Date
    Jan 2016
    Location
    Greece
    Posts
    14

    Getting current user username

    Hello,

    I tried the code as follows:
    Code:
       
    #include <windows.h>
    #include <stdio.h>
    #include <Lmcons.h>
    #include <string>
    #include <iostream>
    int main()
    {
     char username[UNLEN +1];
        DWORD username_len = UNLEN + 1;
        GetUserName((LPWSTR)username, &username_len);
        for each(auto test in username)
        {
            cout << test ;
        }
    }
    Which shows me that I indeed have chars inside username.
    Now I try to do the following as described here on the answer also but it doesn't work.

    The problem by the compiler is on that line and specifically on variable username:
    Code:
    GetUserName(username, &username_len);
    Error says: Argument of type "Char*" is incompatible with parameter of type LPWSTR.
    So I change my code to this:
    Code:
    char username[UNLEN + 1];
            DWORD username_len = UNLEN + 1;
            GetUserName((LPWSTR)username, &username_len);
    And when I print it, I only get the first letter. Why?

    Thanks in advance!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    "LPWSTR" is basically "wchar_t*". "Char*" is, well, "char*"... Do you see why casting it to another pointer type doesn't work?
    Devoted my life to programming...

  3. #3
    Registered User Arhaikos's Avatar
    Join Date
    Jan 2016
    Location
    Greece
    Posts
    14
    Well I tried this one as well:
    Code:
        wchar_t username[UNLEN +1];
        DWORD username_len = UNLEN + 1;
        GetUserName(username, &username_len);
        cout << username<<"\n";
    Still isn't working..

  4. #4
    Registered User Arhaikos's Avatar
    Join Date
    Jan 2016
    Location
    Greece
    Posts
    14
    Anyway, wcout fixed it
    Code:
        wchar_t username[UNLEN +1];
        DWORD username_len = UNLEN + 1;
        GetUserName(username, &username_len);
        wcout << username <<"\n";
    Thanks

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You probably have unicode selected in your project settings. But that's a good thing, since otherwise it won't handle non-US characters properly. That's why it wants to use wchar_t instead of char. Most Win32 API functions have two versions: one that takes char* and one that takes wchar_t*. By default, the compiler selects the unicode versions and they want wchar_t*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-21-2015, 07:17 AM
  2. How to get current username
    By ssharish2005 in forum C Programming
    Replies: 3
    Last Post: 09-25-2007, 02:44 PM
  3. how to get current user home directory
    By George2 in forum Linux Programming
    Replies: 4
    Last Post: 05-08-2007, 02:46 PM
  4. Getting the user's local username
    By Datamike in forum C Programming
    Replies: 8
    Last Post: 11-26-2005, 06:00 PM
  5. [C] Get the current logged user (win2k)
    By BianConiglio in forum Windows Programming
    Replies: 10
    Last Post: 03-09-2005, 12:39 AM