SYNACK (17th June 2008)

Hi I have some C# code that I want to use for a project that interfaces directly with some Windows DLLs. Problem is that I need to use System.Runtime.InteropServices and ad a reference to it in MS Visual Studio 2005.
I can't find it in the list of avalible references when I try to add it and I can't find any DLL called System.Runtime.InteropServices.dll on my system. Its a full install of VS2005 and I have even looked for it in full installs of VS2003 and VS2008.
I have spent a couple of hours on Google searching for clues but as it seems to be a simple task no one has written a helpful hint anywhere that I can find.
Does anyone here happen to have any pointers as to where I could find this elusive reference as it is driving me nuts.![]()
Have tried adding
using System.Runtime.InteropServices;
to the top of your project where other standard refernces are normally inserted?
It does not normally need a spefic DLL to be referenced? I take it the dotnet SDK is installed ?
If that does not work perhaps you post section of code ?
PS had a great day watching the cricket at Chester Le Street yesterday![]()
Last edited by monkeyx; 16th June 2008 at 09:40 PM.
SYNACK (17th June 2008)

Thanks for the reply monkeyx, I have added the appropriate using declaration to the top of the code but VS seems to need it to be added to the project references list in the solution explorer.
excerpt of code below:
Code:using System; using System.Collections.Generic; using System.Text; using System; using System.Runtime.InteropServices; using System.Security.Principal; using System.Security; namespace ShutDWN { sealed class Win32TokenPrivileges { internal const uint SE_PRIVILEGE_DISABLED = 0x00000000; internal const uint SE_PRIVILEGE_ENABLED = 0x00000002; [StructLayout(LayoutKind.Sequential)] internal struct LUID { internal uint LowPart; internal uint HighPart; } [StructLayout(LayoutKind.Sequential)] internal struct LUID_AND_ATTRIBUTES { internal LUID Luid; internal uint Attributes; } [StructLayout(LayoutKind.Sequential)] internal struct TOKEN_PRIVILEGE { internal uint PrivilegeCount; internal LUID_AND_ATTRIBUTES Privilege; } [DllImport("advapi32", CharSet = CharSet.Unicode, SetLastError = true), SuppressUnmanagedCodeSecurity] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, [In, Out] ref LUID Luid);
Just to confirm on VS 2005 setup
I had to attach image as not showing up?
If you can type that entry using auto complete then VS is seeing it as a valid namespace reference.
Can you confirm if VS studio is doing that?
I noticed that you are using a DLLimport, are you referencing advapi32.dll
in your references and it maybe you need to add .dll onto your DllImport
ie [DllImport("advapi32.dll",
Hope the above helps if not let me know.
Last edited by monkeyx; 17th June 2008 at 10:40 AM.
SYNACK (17th June 2008)
Given the "using" line interop just works for me (do have SDK, do always put ".dll" in there but I'm not convinced you need to, don't have interop it in the references).
What does VS actually complain about?
SYNACK (17th June 2008)

Thanks for your responses, I have checked and it looks like it is picking it up in intellisense. The error was:
I was kind of confused by the code example and so suspected the references. I removed the SuppressUnmanagedCodeSecurity bit and it seems to have come to its senses. Well apart from all my other coding errors.Error 2 The type or namespace name 'SuppressUnmanagedCodeSecurity' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\UN\My Documents\Visual Studio 2005\Projects\ShutDWN\ShutDWN\Form1.cs 22 40 ShutDWN
Thanks again for your help.
![]()
Last edited by SYNACK; 17th June 2008 at 01:44 PM.
I had VS2K5 fired up for something else so I just quickly threw an Main() and a few brackets at the bottom of your code excerpt and it built with no complaints.I removed the SuppressUnmanagedCodeSecurity

Thanks, there were some iffy line breaks further down that were messing with it to, this is my first major jump into C# so it was tricky to track some of the stuff down. This is the full code block:
It is just a small component of a much larger and more devious device that I hope to construct, an intelligent network shutdown agent.Code:using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Security.Principal; using System.Security; namespace ShutDWN { sealed class Win32TokenPrivileges { internal const uint SE_PRIVILEGE_DISABLED = 0x00000000; internal const uint SE_PRIVILEGE_ENABLED = 0x00000002; [StructLayout(LayoutKind.Sequential)] internal struct LUID { internal uint LowPart; internal uint HighPart; } [StructLayout(LayoutKind.Sequential)] internal struct LUID_AND_ATTRIBUTES { internal LUID Luid; internal uint Attributes; } [StructLayout(LayoutKind.Sequential)] internal struct TOKEN_PRIVILEGE { internal uint PrivilegeCount; internal LUID_AND_ATTRIBUTES Privilege; } [DllImport("advapi32", CharSet = CharSet.Unicode, SetLastError = true),SuppressUnmanagedCodeSecurity][return: MarshalAs(UnmanagedType.Bool)] internal static extern bool LookupPrivilegeValue(string lpSystemName,string lpName, [In, Out] ref LUID Luid); [DllImport("advapi32", CharSet = CharSet.Unicode, SetLastError = true),SuppressUnmanagedCodeSecurity][return: MarshalAs(UnmanagedType.Bool)] internal static extern bool AdjustTokenPrivileges(IntPtr tokenHndl,bool diasableAll, [In] ref TOKEN_PRIVILEGE newTokenState, int length,ref TOKEN_PRIVILEGE prevTokenState, ref uint retLength); [DllImport("advapi32.dll",EntryPoint="InitiateSystemShutdown", CharSet = CharSet.Unicode, SetLastError = true), SuppressUnmanagedCodeSecurity] [return: MarshalAs(UnmanagedType.Bool)] // Explicit marshaling directive required here! (BOOL in Windows is a DWORD, bool in C# is a byte value) internal static extern bool InitiateSystemShutdown(string lpMachineName,string lpMessage,int dwTimeout,[MarshalAs(UnmanagedType.Bool)] bool bForceAppsClosed,[MarshalAs(UnmanagedType.Bool)] bool bRebootAfterShutdown); } sealed class Program { static void Main() { IntPtr tokenHandle = WindowsIdentity.GetCurrent().Token; // consider using a safehandle here Win32TokenPrivileges.LUID luid = new Win32TokenPrivileges.LUID(); bool ret = Win32TokenPrivileges.LookupPrivilegeValue(""); if (ret == true) { Win32TokenPrivileges.TOKEN_PRIVILEGE tokenPriv = new Win32TokenPrivileges.TOKEN_PRIVILEGE(); Win32TokenPrivileges.TOKEN_PRIVILEGE prevToken = new Win32TokenPrivileges.TOKEN_PRIVILEGE(); tokenPriv.PrivilegeCount = 1; tokenPriv.Privilege.Luid = luid; tokenPriv.Privilege.Attributes = Win32TokenPrivileges.SE_PRIVILEGE_ENABLED; uint retlen = 0; ret = Win32TokenPrivileges.AdjustTokenPrivileges(tokenHandle, false, ref tokenPriv, Marshal.SizeOf(tokenPriv), ref prevToken, ref retlen); if (ret == false) { Console.WriteLine("AdjustTokenPrivileges error {0}", Marshal.GetLastWin32Error()); Environment.Exit(1); } ret = Win32TokenPrivileges.InitiateSystemShutdown("", "System shutdown requested .... ", 30, true, true); if (ret == false) { Console.WriteLine("InitiateSystemShutdown error {0}", Marshal.GetLastWin32Error()); Environment.Exit(1); } } } } }![]()
Have you considered going down the WMI route to shutdown ie
WMI - Easy Shutdown
and
Shutting down a PC using WMI and C# at The Social Programmer
Being nosey about your project I guess. I have tried Pinvoke and WMI curious to know what you think.

The WMI method is no where near as full featured unless you have Vista in which case it is actually better. You have way more control with the DLL.
Also WMI traffic must be explicitly enabled with group policy for it to run properly over a network which adds another configuration hurdle for end users.
My code will actually attempt to use WMI calls for some of the extra features like checking to see who is logged on and whether the station is locked. It can then check this against a list of users who might actually be allowed to be in the school at that time, ie me and will not bother shutting it down.
This way the shutdown can be run every half hour or so after school finishes and it will not disrupt anyone who is actually using the machines. Its powers of observation could also be turned to snitching on users that constantly leave their machines on![]()
![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)