Thread: 'GetUserNameEx': identifier not found error

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    5

    'GetUserNameEx': identifier not found error

    I am trying to use "GetUserNameEx" function with VS.Net but facing some trouble while compiling this. I am getting error as

    "error C3861: 'GetUserNameEx': identifier not found, even with argument-dependent lookup"

    I verified that Secur32.dll is available in system32 directory. And I added Secure32.lib in "Project properties -> Linker -> General -> Additional Library Directories also. But still no success.

    Is it something to do with not able to locate this lib and dll files? how can i confirm that Secure32.lib file is visible to this project? ( by default where will be libraries residing? )

    Regards,
    Shivaraj

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you including the right header-file? Since it's a "Unicode/Ansi" function, it will have a macro somewhere that translates it to GetUserNameExA or GetUserNameExW for the Ansi and Unicode versions respectively. It looks like your linker is not looking for a A/W version of the function, which makes me think that you are not using the correct headerfile - but I'm only guessing.

    Quote Originally Posted by MSDN
    Declared in Secext.h; include Security.h.
    From:
    http://msdn2.microsoft.com/en-us/library/ms724435.aspx

    --
    Mats
    Last edited by matsp; 11-07-2007 at 04:13 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    Hi, Thanks for your suggestion. After including Secext.h and Security.h I am getting many errors as
    error C2146: syntax error : missing ';' before identifier 'GetUserNameExA'
    error C1189: #error : You must define one of SECURITY_WIN32, SECURITY_KERNEL, or
    error C2086: 'BOOLEAN SEC_ENTRY' : redefinition etc...

    And these are from either SecExt.h file or from Sspi.h file. Any idea whats wrong?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't know, but you can try defining SECURITY_WIN32.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    Hi,
    After adding #define SECURITY_WIN32 in stdafx.h file it worked !!! But now hitting some issues wrt to the usage of GetUserNameEx function

    my usage looks like
    ----------------------------
    PULONG bufsize = 0;
    BOOLEAN success = GetUserNameEx((EXTENDED_NAME_FORMAT)8, (LPSTR)UserName, &bufsize);
    ----------------------------
    but its throwing a error:
    error C2664: 'GetUserNameExA' : cannot convert parameter 3 from 'PULONG *__w64 ' to 'PULONG'

    Any idea how to use GetUserNameEx function? Any sample code which I can look into?

    Regards,
    Shivaraj

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by shivarajbm View Post
    Code:
    ULONG bufsize = 0;
    BOOLEAN success = GetUserNameEx((EXTENDED_NAME_FORMAT)8, (LPSTR)UserName, &bufsize);
    Use code tags when presenting code.
    PULONG = ULONG*. Don't be fooled by the stupid naming system they use. Use a regular ULONG and pass its address.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    Hi, I added only <Security.h> and modified the code to use TCHAR buffer.

    Code:
    TCHAR sBuffer[256]; 
    DWORD Ret = sizeof(sBuffer); 
    if(GetUserNameEx(NameUserPrincipal,sBuffer,&Ret) == 0) 
    {
       ...
    }
    after this I am getting a Linker error as
    error LNK2019: unresolved external symbol _GetUserNameExA@12 referenced in function "void __cdecl CreateProfileWithIProfAdmin(void)" (?CreateProfileWithIProfAdmin@@YAXXZ)

    To resolve this I added <SecExt.h> where this function is declared. After adding <SecExt.h> I am getting errors as

    error C2146: syntax error : missing ';' before identifier 'GetUserNameExA'
    error C2086: 'BOOLEAN SEC_ENTRY' : redefinition

    etc... I feel this is again to do with header files. Any hint on this?

    Regards,
    Shivaraj

  8. #8
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    your best bet is to install the latest service pack for your version of VS, making sure to set environment variables to its include and lib directories during the installation.

    as for the headers and libraries, just look for them in the service pack's include and lib directories in Program Files. chances are you're missing secur32.lib.

    secur32.lib and secur32.dll are not the same kind of libraries. in your case, you want the object library, not the dynamic link library. the DLL is ultimately where the functions exist, and will be loaded at run time. the linker will give you an error if the object file cannot be found at run time, just as the Windows loader will give you an appropriate error should the DLL not be found at run time. this is implicit linking, you can also explicitly load your functions using the LoadLibrary and GetProcAddress calls, but remember to handle errors appropriately.
    Last edited by Bleech; 11-12-2007 at 03:29 AM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps including the overall header as recommended by MS -> security.h rather than SecExt.h - the latter is included BY security.h, but there are probably other declarations in security.h that are necessary to compile the code. Microsoft isn't saying:
    [quote]
    Declared in Secext.h; include Security.h.
    [/code]
    just because they think it's fun to tell you to include a bigger header-file than the one where the function is declared - it's because some other "stuff" is needed in Secext.h that are brought in with Security.h.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    Hi All,

    Finally I am able to run this piece of code and was able to fetch the required domain information. As it was stated in previous mailchain, I reinstalled my SDK and started a new project. Added only required header files and libraries. After this it started working fine. Sad part is not knowing why it caused this problem before. But any way its working now !!!

    This was the first time I posted query to this site, its nice to see so many responces in such a short time. Thanks once again for spending your valuable time.

    Cheers,
    Shivaraj

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    Perhaps including the overall header as recommended by MS -> security.h rather than SecExt.h - the latter is included BY security.h, but there are probably other declarations in security.h that are necessary to compile the code. Microsoft isn't saying:
    Declared in Secext.h; include Security.h.
    just because they think it's fun to tell you to include a bigger header-file than the one where the function is declared - it's because some other "stuff" is needed in Secext.h that are brought in with Security.h.

    --
    Mats
    Just including Security.h doesn't work... It will #error you saying either one of two defines must be defined. I checked that.

    Good to hear it's working, though.

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    1
    You have to be careful with the order of the #include and #define. Everything must be after the stdafx.h and SECURITY_WIN32 must be before Security.h. Try this...

    Code:
    #include "stdafx.h"
    #define SECURITY_WIN32
    #include "Security.h"
    
    GUID GetUserGUID()
    {
    	ULONG bufsize = 1024;
    	TCHAR s[1024];
    	if(!GetUserNameEx(NameUniqueId, s, &bufsize)) {
    		::AfxMessageBox("Failed to get users GUID");
    
                    blah blah blah
    }

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    1
    #define SECURITY_WIN32 //#define and #include order shud not be changed
    #include <Security.h> // Needed for GetUserNameEx function

    #pragma comment(lib, "Secur32.lib")


    Above should be the contents in ur stdafx.h file right above the using namespace statement and below the .h file details

    Abhinay

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    3 year old post is now dead and buried.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM