Article View: borland.public.cppbuilder.nativeapi
Article #5782Re: Using GetUserNameEx()
From: Jeff
Date: Wed, 29 Jun 2005 19:23
Date: Wed, 29 Jun 2005 19:23
61 lines
3024 bytes
3024 bytes
Thank you very much, Great comments!
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:42c2f171$1@newsgroups.borland.com...
>
> "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:
<42c2f522$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.google.com!newsread.com!newsprint.newsread.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<42c2e09e$1@newsgroups.borland.com> <42c2f171$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,bb7477294a9a4681,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Jeff" <Jeff@nospam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <42c2e09e$1@newsgroups.borland.com> <42c2f171$1@newsgroups.borland.com> Subject: Re: Using GetUserNameEx() Date: Wed, 29 Jun 2005 13:23:17 -0600 Lines: 63 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2527 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527 NNTP-Posting-Host: 198.68.195.180 Message-ID: <42c2f522$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1120072994 198.68.195.180 (29 Jun 2005 12:23:14 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.google.com!newsread.com!newsprint.newsread.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail