not a question for a beginner

bri189a

Well-known member
Joined
Sep 11, 2003
Messages
1,004
Location
VA
Im using the following code snippets:

C#:
private unsafe struct LUID_AND_ATTRIBUTES
		{
			public System.Int64 LUID;
			public System.String Attributes;		//32 bit DWORD
		}
		private unsafe struct TOKEN_PRIVILEGES
		{
			public System.String PrivledgeCount;	//32 bit DWORD
			public LUID_AND_ATTRIBUTES [] Privledges;
		}

[DllImport("advapi32.dll")]
		static extern int AdjustTokenPrivileges (int TokenHandle, int DisableAllPrivileges, 
TOKEN_PRIVILEGES NewState, int BufferLength, TOKEN_PRIVILEGES PreviousState, int ReturnLength);

//and later in a function:

AdjustTokenPrivileges((int) nTknHWND, Convert.ToInt32(false), privledges, 0, privledges, 0);

The privledges variable is a TOKEN_PRIVILEGES struct, in actuallity I should be passing null (or rather 0) in that 5th argument, but I cant pass null because the function declaration calls for a struct (value type), nor can I pass 0 because we cant convert int to TOKEN_PRIVILEGES. Ive tried several variations of structure the function but I always get a run time error of Invalid Parameter - okay we know what that means, and Im pretty sure its the 5th argument that is causing it, but I dont know how to get around it. The purpose of all of this is to shut down Windows 2000 machines which requires the SE_NAME_PRIVILEDGE, thus the AdjustTokenPrivileges WinApi function. I have a valid process token in the first argument, and the the third argument contains the SE_ENABLE attribute for the SE_NAME_PRIVILEDGE, so that is all working. I can get this to work in straight out C++ (I dont have C++.NET yet), and it works fine, its just a matter of changing it over to C#, and that darn 5th parameter is killing me, or something Im not aware of. Any ideas? :mad:
 
If you want to pass NULL to a function from C#, you should declare your function as taking a System.IntPtr and pass System.IntPtr.Zero. Heres a sample:
C#:
[DllImport("advapi32.dll")]
static extern int AdjustTokenPrivileges (int TokenHandle, int DisableAllPrivileges, TOKEN_PRIVILEGES NewState, int BufferLength, System.IntPtr PreviousState, int ReturnLength);

//and later in a function:
AdjustTokenPrivileges((int) nTknHWND, Convert.ToInt32(false), privledges, 0, System.IntPtr.Zero, 0);

I didnt look at the rest of your function, but if its right then passing the Zero IntPtr should work.

You could also try declaring the 5th param as Int32 and passing 0 which is pretty much the equivalent of NULL though the IntPtr is the recommended method.

-nerseus
 
Back
Top