Problem mit getUserName



  • Ich habe es noch nicht geschaft den Usernamem mit GetuserName auszulesen, irgendwie hapert es immer an dem 2ten Parameter.
    Kann mir bitte jemand einen Codesnippet posten, da man imm Internet kaum infos findet.



  • #include <new>
    #include <windows.h>
    
    using namespace std;
    
    int main( )
    {
        char dummy;
        char *buffer;
        unsigned long length = 1;
    
        GetUserName( dummy, &length );
    
        if( GetLastError( ) == ERROR_MORE_DATA ) {
    
            try {
    
                buffer = new char[ length + 1 ];
    
            } catch( std::bad_alloc ) {
    
                MessageBox( 0, "Not enough memory!", "Error:", MB_OK );
                return EXIT_FAILURE;
            }
    
            GetUserName( buffer, &length );
    
        } else {
    
            MessageBox( 0, "GetUserName() failed!", "Error:", MB_OK );
            return EXIT_FAILURE;
        }
    
        MessageBox( 0, buffer, "Username:", MB_OK );
    
        delete [ ] buffer;
    }
    

    Greetz, Swordfish



  • char userName[UNLEN];
    DWORD bufferSize = sizeof(userName);
    GetUserName(userName, &bufferSize);
    


  • TCHAR statt char!
    UNICODE!/ANSI!

    Swordfish schrieb:

    char dummy;
        char *buffer;
        unsigned long length = 1;
    
        GetUserName( dummy, &length );
    

    Warum so kompliziert ?!:

    DWORD dwBufferLength = 0L;
    GetUserName(NULL, &dwBufferLength);
    PTCHAR pszNameBuffer = new TCHAR[dwBufferLength];
    GetUserName(pszNameBuffer , &dwBufferLength);
    MessageBox(NULL, pszNameBuffer, NULL, 0);
    

    PS: Fehlerüberprüfung für new ist nicht nötig, wer kein Speicher hat, hat andere Probleme. dwBufferLength muss lediglich auf 0 gesetzt werden, damit der erste Aufruf uns die Länge des Namen ermitteln kann.

    siehe:

    MSDN schrieb:

    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.

    Die Sache mit UNLEN find ich nicht sehr schön. 👎



  • unlen schrieb:

    char userName[UNLEN];
    DWORD bufferSize = sizeof(userName);
    GetUserName(userName, &bufferSize);
    

    Dazu vllt. noch was: 💡

    MSDN schrieb:

    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.

    👍 beziehungsweise:

    LMCONS.H schrieb:

    NOTE: Lengths of strings are given as the maximum lengths of the
    string in characters (not bytes). This does not include space for the
    terminating 0-characters. When allocating space for such an item,
    use the form:

    TCHAR username[UNLEN+1];

    Kurz: es fehlt ein:

    #include <LMCONS.H>
    

    und es muss heißen:

    char userName[UNLEN + 1];
    

    ⚠
    Auch hier würde ich die UNICODE-unabhängige Verwendung von TCHAR vorziehen!


Anmelden zum Antworten