Groups | Blog | Home
all groups > dotnet interop > january 2007 >

dotnet interop : CheckTokenMembership with Interop


Arkady Frenkel
1/9/2007 6:38:49 PM
Hi, dears!

When I took example of using
CheckTokenMembership() from
http://msdn2.microsoft.com/en-us/library/aa376389.aspx

to check IsUserAdmin() that work great in C++, but when I call the
CheckTokenMembership() in

csharp for user without admin rights , it returned true opposite to c++
where it return false and that what checked in example. OTOH IsMember return
false as needed in this case.

My question why interop change return code from false to true ?

TIA

Arkady



Mattias Sjögren
1/9/2007 10:37:01 PM

[quoted text, click to view]

Without seeing any of your code, my guess will have to be that you've
declared the function incorrectly.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Arkady Frenkel
1/10/2007 9:09:20 AM
Hi, Mattias!
[DllImport("advapi32.dll", SetLastError = true)]

static extern bool CheckTokenMembership(IntPtr TokenHandle, IntPtr
SidToCheck, out bool IsMember);

bool b = false ;

IntPtr TokenHandle = IntPtr.Zero;

IntPtr AdministratorsGroup is correct and equal "S-1-5-32-544" ( admin
group ) when I used

ConvertSidToStringSid() to see result of AllocateAndInitializeSid()

if (!CheckTokenMembership(TokenHandle, AdministratorsGroup, out b)) return
me true where C++ code return false ( as I wrote b return correct false for
such user )

TIA

Arkady



[quoted text, click to view]

Arkady Frenkel
1/10/2007 9:31:49 AM
In advance additional a liitle bit strange ( at least for me ) with interop
was before when I received SID of admin group :
I set
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]

static extern bool AllocateAndInitializeSid(

ref byte[] pIdentifierAuthority,byte nSubAuthorityCount,

int dwSubAuthority0, int dwSubAuthority1,int dwSubAuthority2, int
dwSubAuthority3,

int dwSubAuthority4, int dwSubAuthority5, int dwSubAuthority6, int
dwSubAuthority7,

out IntPtr pSid);

and call it

byte[] SECURITY_NT_AUTHORITY = {0,0,0,0,0,5} ;

bool b = AllocateAndInitializeSid(ref SECURITY_NT_AUTHORITY, ...



return pSid was wrong something like S-1-123456-1 OTOH when I changed

declaration to ref byte pIdentifierAuthority instead of byte[] and call

bool b = AllocateAndInitializeSid(ref SECURITY_NT_AUTHORITY[0],

correct S-1-5-32-544 returned

TIA

Arkady



[quoted text, click to view]

AddThis Social Bookmark Button