🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #5783

Re: Using GetUserNameEx()

#5783
From: Remy Lebeau (TeamB)
Date: Wed, 29 Jun 2005 19:00
53 lines
2696 bytes
"Jeff" <Jeff@nospam.com> wrote in message
news:42c2e09e$1@newsgroups.borland.com...

> I'm trying to use a Windows API call to get the full name of
> the person logged into the current system. When I compile
> my code below I get an "Unresolved external 'GetUserNameExW'
> referenced from..." linker error.

Try adding "secur32.lib" to your project.  It is in the $(BCB)\Lib\PSDK
folder.

> WCHAR   wszDN[MAXBUFF];  //Character buffer for DN Name
> ULONG   cchDN = MAXBUFF; //Length of DN Name

Rather than hard-coding your buffer size, you should allow GetUserNameEx()
to tell you what buffer size to use.

    ULONG cchDN = 0;
    GetUserNameEx(NameDisplay, NULL, &cchDN);
    if( GetLastError() == ERROR_MORE_DATA )
    {
        WCHAR *wszDN = new WCHAR[cchDN];
        if( GetUserNameEx(NameDisplay, wszDN, &cchDN) )
            //...
        delete[] wszDN;
    }

> if (!GetUserNameEx(NameDisplay, wszDN, &cchDN))

Do note that GetUserNameEx() is only available on Win2K Pro/Server, XP, and
Win2K3 Server.  If you need your application to run on any other OS
versions, then you cannot statically link to GetUserNameEx() or else the
application will fail to run at all on them.  You would have to use
LoadLibrary() and GetProcAddress() instead to access the function pointer
dynamically at runtime.

>     lbeAddress->Text = static_cast<String>(wszDN);

That is a very dangerous cast.  You are casting a wchar_t[] buffer into an
AnsiString, which it is not.  Just assign the wchar_t[] directly to the Text
instead.  AnsiString has a constructor that accepts a wchar_t* as a
parameter:

    lbeAddress->Text = wszDN;

Or alternatively, it also has a constructor that accepts a WideString as a
parameter:

    lbeAddress->Text = WideString(wszDN, cchDN);


Gambit
Technical Details
Message-ID: <42c2f171$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.maxwell.syr.edu!irazu.switch.ch!switch.ch!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <42c2e09e$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,bb7477294a9a4681
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <42c2e09e$1@newsgroups.borland.com>
Subject: Re: Using GetUserNameEx()
Date: Wed, 29 Jun 2005 12:00:47 -0700
Lines: 55
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <42c2f171$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1120072049 68.69.17.41 (29 Jun 2005 12:07:29 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.maxwell.syr.edu!irazu.switch.ch!switch.ch!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail