Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm using the following code snippets:

 


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 can't pass null because the function declaration calls for a struct (value type), nor can I pass 0 because we can't convert int to TOKEN_PRIVILEGES. I've 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 I'm pretty sure it's the 5th argument that is causing it, but I don't 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 don't have C++.NET yet), and it works fine, it's just a matter of changing it over to C#, and that darn 5th parameter is killing me, or something I'm not aware of. Any ideas? :mad:

  • *Experts*
Posted

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. Here's a sample:

[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 didn't look at the rest of your function, but if it's 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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...