![]() | Register | FAQ | Members | Social Groups | User Map | Calendar | Search | Today's Posts | Mark Forums Read |
| | | LinkBack | Thread Tools | Search Thread | Language |
| Sponsored Links |
| | #1 |
![]() Join Date: Oct 2007 Location: Auckland, New Zealand
Posts: 1,702
Thanks: 59
Thanked 250 Times in 227 Posts
Blog Entries: 2 Rep Power: 54 | 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. |
| |
| | #2 |
![]() Join Date: Nov 2006
Posts: 181
Thanks: 3
Thanked 13 Times in 11 Posts
Rep Power: 7 | 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; 16-06-2008 at 08:40 PM.. |
| |
| The Following User Says Thank You to monkeyx For This Useful Post: | SYNACK (17-06-2008) |
| | #3 |
![]() Join Date: Oct 2007 Location: Auckland, New Zealand
Posts: 1,702
Thanks: 59
Thanked 250 Times in 227 Posts
Blog Entries: 2 Rep Power: 54 | 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);
|
| |
| | #4 |
![]() Join Date: Nov 2006
Posts: 181
Thanks: 3
Thanked 13 Times in 11 Posts
Rep Power: 7 | 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; 17-06-2008 at 09:40 AM.. |
| |
| The Following User Says Thank You to monkeyx For This Useful Post: | SYNACK (17-06-2008) |
| | #5 |
![]() Join Date: Jan 2006
Posts: 498
Thanks: 1
Thanked 20 Times in 16 Posts
Rep Power: 9 | 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? |
| |
| The Following User Says Thank You to PiqueABoo For This Useful Post: | SYNACK (17-06-2008) |
| | #6 | |
![]() Join Date: Oct 2007 Location: Auckland, New Zealand
Posts: 1,702
Thanks: 59
Thanked 250 Times in 227 Posts
Blog Entries: 2 Rep Power: 54 | Thanks for your responses, I have checked and it looks like it is picking it up in intellisense. The error was: Quote:
Thanks again for your help. Last edited by SYNACK; 17-06-2008 at 12:44 PM.. | |
| |
| | #7 | |
![]() Join Date: Jan 2006
Posts: 498
Thanks: 1
Thanked 20 Times in 16 Posts
Rep Power: 9 | Quote:
| |
| |
| | #8 | |
![]() Join Date: Oct 2007 Location: Auckland, New Zealand
Posts: 1,702
Thanks: 59
Thanked 250 Times in 227 Posts
Blog Entries: 2 Rep Power: 54 | Quote:
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);
}
}
}
}
}
| |
| |
| | #9 |
![]() Join Date: Nov 2006
Posts: 181
Thanks: 3
Thanked 13 Times in 11 Posts
Rep Power: 7 | 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. |
| |
| | #10 |
![]() Join Date: Oct 2007 Location: Auckland, New Zealand
Posts: 1,702
Thanks: 59
Thanked 250 Times in 227 Posts
Blog Entries: 2 Rep Power: 54 | 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 |
| |
| |
| | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| LogMeIn & Java runtime 6 | ITWombat | *nix | 2 | 31-12-2007 11:03 PM |
| Free stuff - online reference works | SpuffMonkey | Educational Software | 0 | 16-03-2007 01:45 PM |
| Is there much of a market for old reference material?? | sidewinder | General Chat | 4 | 28-02-2007 11:58 AM |
| Windows Server 2003 Command Reference Guide - FREE | Nij.UK | Windows | 5 | 30-01-2007 09:21 AM |
| Updating Java Runtime | ITWombat | Windows | 3 | 18-05-2006 12:44 PM |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search Thread |
|
|




