Author Topic: Win: IsUserAnAdmin() and CheckTokenMembership(..)  (Read 6236 times)

Offline jmb

  • Multiple posting newcomer
  • *
  • Posts: 24
Win: IsUserAnAdmin() and CheckTokenMembership(..)
« on: January 20, 2015, 12:33:20 pm »
Hi

In VisualStudioExpress-c++ there is an function "IsUserAnAdmin()" available.
But it doesn't work in codeblocks. So I found the following solution in the net:

(Its for testing, whether an user is Admin)

Code
BOOL IsUserAdmin(VOID)
/*++
Routine Description: This routine returns TRUE if the caller's
process is a member of the Administrators local group. Caller is NOT
expected to be impersonating anyone and is expected to be able to
open its own process and process token.
Arguments: None.
Return Value:
   TRUE - Caller has Administrators local group.
   FALSE - Caller does not have Administrators local group. --
*/
{
BOOL b;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
b = AllocateAndInitializeSid(
    &NtAuthority,
    2,
    SECURITY_BUILTIN_DOMAIN_RID,
    DOMAIN_ALIAS_RID_ADMINS,
    0, 0, 0, 0, 0, 0,
    &AdministratorsGroup);
if(b)
{
    if (!CheckTokenMembership( NULL, AdministratorsGroup, &b))
    {
         b = FALSE;
    }
    FreeSid(AdministratorsGroup);
}

return(b);
}





But I just get this:

undefined reference to `CheckTokenMembership(void*, void*, int*)@12'

Any Idea what to do ?




Linux Mint, Cinnamon 19.3 , Code::Blocks 20.03

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Win: IsUserAnAdmin() and CheckTokenMembership(..)
« Reply #1 on: January 20, 2015, 12:58:52 pm »
Any Idea what to do ?
1.) Google for the error.
2.) Respect the forum rules. This is a C::B forum and not a general programming forum.

I'll give you a hint anyways:
As seen on:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa376389%28v=vs.85%29.aspx

...does CheckTokenMembership require to link against Advapi32 in the right order.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline jmb

  • Multiple posting newcomer
  • *
  • Posts: 24
Re: Win: IsUserAnAdmin() and CheckTokenMembership(..)
« Reply #2 on: January 20, 2015, 01:27:58 pm »
Any Idea what to do ?
1.) Google for the error.

I did that. See below ...

Quote
2.) Respect the forum rules. This is a C::B forum and not a general programming forum.

Sorry. But for me this question has to do specifically with the usage of codeblocks. I didn't asked anything about programming. I asked about that specific problem I have in codeblocks. This was the reason, why I mentioned VisualStudio.

Quote
I'll give you a hint anyways:
As seen on:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa376389%28v=vs.85%29.aspx

...does CheckTokenMembership require to link against Advapi32 in the right order.

Thank you. But I found that by myself. I can't find that ".lib."  So I tried to add the
libadvapi32.a  in the section "Linker-Settings". It doesn't work.

I've just begun to work with codeblocks yesterday. So sorry, if that question is a bit nooby.




Linux Mint, Cinnamon 19.3 , Code::Blocks 20.03

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Win: IsUserAnAdmin() and CheckTokenMembership(..)
« Reply #3 on: January 20, 2015, 02:53:45 pm »
Sorry. But for me this question has to do specifically with the usage of codeblocks. I didn't asked anything about programming. I asked about that specific problem I have in codeblocks. This was the reason, why I mentioned VisualStudio.

In this case please (reread) the forum rules.
Please also read this wiki-entry: http://wiki.codeblocks.org/index.php?title=FAQ-General#Q:_What_Code::Blocks_is_not.3F .
By the way, you can also use VC's commandline version as compiler in Code::Blocks.

Offline jmb

  • Multiple posting newcomer
  • *
  • Posts: 24
Re: Win: IsUserAnAdmin() and CheckTokenMembership(..)
« Reply #4 on: January 20, 2015, 04:28:38 pm »
ok, so I was totally wrong about my expectations about the forum. My first Idea was, as I mentioned above, that this forum will
discuss all topics around the usage of that IDE. I thought, this includes how to set up compilers and linkers, too.

During my investigations I found things like : "in this and that IDE it is handled so and so. I dont know, how it works in codeblocks.."

My next step was then to search for an codeblocks-forum *shrugs*



Linux Mint, Cinnamon 19.3 , Code::Blocks 20.03

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Win: IsUserAnAdmin() and CheckTokenMembership(..)
« Reply #5 on: January 20, 2015, 04:43:56 pm »
ok, so I was totally wrong about my expectations about the forum. My first Idea was, as I mentioned above, that this forum will
discuss all topics around the usage of that IDE. I thought, this includes how to set up compilers and linkers, too.

During my investigations I found things like : "in this and that IDE it is handled so and so. I dont know, how it works in codeblocks.."

My next step was then to search for an codeblocks-forum *shrugs*

If you know what to set, but don't know where to do it in Code::Blocks it's a valid question here.
But if you only know what it should do, but don't know how to do it with the compiler you use, the question is not valid.

The compiler shipped with C::B is shipped for convenience, but we can not give any support for it, sorry.
Code::Blocks supports lots of compilers and it's absolutely impossible to give support for all of them.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Win: IsUserAnAdmin() and CheckTokenMembership(..)
« Reply #6 on: January 20, 2015, 04:46:00 pm »
And what's more, it's clearly stated in the forum rules.
When registering here you accepted to follow these rules.
And a link to the rules is placed above the "Post"-button as reminder for the users.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Win: IsUserAnAdmin() and CheckTokenMembership(..)
« Reply #7 on: January 20, 2015, 07:58:21 pm »
Any way... I doubt that the compiler that ships with C::B does not support that. Please post the full build log... Just to be sure.

And bare in mind that although this sounds harsh we have to be strict when it comes to compiler errors. This is an error coming from the compiler just being reported by the IDE therefore it would really not belong here for the reasons Jens stated. C::B supports way to many compilers and if you additionally take their versions into account, probably nobody can provide meaningful answers. :-)
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline jmb

  • Multiple posting newcomer
  • *
  • Posts: 24
Re: Win: IsUserAnAdmin() and CheckTokenMembership(..)
« Reply #8 on: January 20, 2015, 08:52:24 pm »

And bare in mind that although this sounds harsh we have to be strict when it comes to compiler errors. This is an error coming from the compiler just being reported by the IDE therefore it would really not belong here for the reasons Jens stated. C::B supports way to many compilers and if you additionally take their versions into account, probably nobody can provide meaningful answers. :-)

Thank you, for making this clear ;-)  Makes sense. I really thought, its an codeblock-problem.

Quote
Any way... I doubt that the compiler that ships with C::B does not support that. Please post the full build log... Just to be sure.


It seems, there are some people, who have exactly the same problem. And I didn't found a good solution.

Nevertheless, here are the logs: (Source of affected function is in my first posting here)


------------------------------------- cut ----------------------------------


Build-Log:
------------

-------------- Clean: Release in DNS_Umschalter (compiler: GNU GCC Compiler)---------------

Cleaned "DNS_Umschalter - Release"

-------------- Build: Release in DNS_Umschalter (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -ansi -O2 -ansi  -c M:\!CodeBlocks_Tests\DNS_Umschalter\DNS_Umschalter\DNS_Umschalter.cpp -o obj\Release\DNS_Umschalter.o
M:\!CodeBlocks_Tests\DNS_Umschalter\DNS_Umschalter\DNS_Umschalter.cpp: In function 'BOOL IsUserAdmin()':
M:\!CodeBlocks_Tests\DNS_Umschalter\DNS_Umschalter\DNS_Umschalter.cpp:591:42: warning: missing braces around initializer for 'BYTE [6] {aka unsigned char [6]}' [-Wmissing-braces]
windres.exe  -J rc -O coff -i M:\!CODEB~1\DNS_UM~1\DNS_UM~1\HAUPTD~1.RC -o obj\Release\hauptdialog.res
mingw32-g++.exe  -o bin\Release\DNS_Umschalter.exe obj\Release\DNS_Umschalter.o  obj\Release\hauptdialog.res -s  -lgdi32 -luser32 -lkernel32 -lcomctl32 -ladvapi32 -mwindows
obj\Release\DNS_Umschalter.o:DNS_Umschalter.cpp:(.text+0x1128): undefined reference to `CheckTokenMembership(void*, void*, int*)@12'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 1 warning(s) (0 minute(s), 0 second(s))
 


Build-Messages:
-------------------

||=== Build: Release in DNS_Umschalter (compiler: GNU GCC Compiler) ===|
CodeBlocks_Tests\DNS_Umschalter\DNS_Umschalter\DNS_Umschalter.cpp||In function 'BOOL IsUserAdmin()':|
CodeBlocks_Tests\DNS_Umschalter\DNS_Umschalter\DNS_Umschalter.cpp|591|warning: missing braces around initializer for 'BYTE [6] {aka unsigned char [6]}' [-Wmissing-braces]|
obj\Release\DNS_Umschalter.o:DNS_Umschalter.cpp|| undefined reference to `CheckTokenMembership(void*, void*, int*)@12'|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|


Linux Mint, Cinnamon 19.3 , Code::Blocks 20.03