Im using the following code snippets:
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?
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?