bri189a Posted October 29, 2003 Posted October 29, 2003 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: Quote
*Experts* Nerseus Posted October 29, 2003 *Experts* Posted October 29, 2003 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 Quote "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
Recommended Posts