Welcome, Register for free! or Login below:
EduGeek.net RSS Feeds Register FAQ Members Social Groups User Map Calendar Search Today's Posts Mark Forums Read

Go Back   EduGeek.net Forums > Coding and Web Development > Coding
Reply
 
LinkBack Thread Tools Search Thread Language
Sponsored Links
Old 16-06-2008, 06:47 PM   #1
 
SYNACK's Avatar
 
Join Date: Oct 2007
Location: Auckland, New Zealand
Posts: 1,702
newzealand
Thanks: 59
Thanked 250 Times in 227 Posts
Blog Entries: 2
Rep Power: 54 SYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud of
Send a message via MSN to SYNACK
Question C# Reference System.Runtime.InteropServices?

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.
  Reply With Quote
Old 16-06-2008, 08:34 PM   #2
 
monkeyx's Avatar
 
Join Date: Nov 2006
Posts: 181
Thanks: 3
Thanked 13 Times in 11 Posts
Rep Power: 7 monkeyx will become famous soon enough
Default

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..
  Reply With Quote
The Following User Says Thank You to monkeyx For This Useful Post:
SYNACK (17-06-2008)
Old 17-06-2008, 05:31 AM   #3
 
SYNACK's Avatar
 
Join Date: Oct 2007
Location: Auckland, New Zealand
Posts: 1,702
newzealand
Thanks: 59
Thanked 250 Times in 227 Posts
Blog Entries: 2
Rep Power: 54 SYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud of
Send a message via MSN to SYNACK
Default

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);
  Reply With Quote
Old 17-06-2008, 09:36 AM   #4
 
monkeyx's Avatar
 
Join Date: Nov 2006
Posts: 181
Thanks: 3
Thanked 13 Times in 11 Posts
Rep Power: 7 monkeyx will become famous soon enough
Default

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.
Attached Images
File Type: jpg interop.JPG (62.3 KB, 4 views)

Last edited by monkeyx; 17-06-2008 at 09:40 AM..
  Reply With Quote
The Following User Says Thank You to monkeyx For This Useful Post:
SYNACK (17-06-2008)
Old 17-06-2008, 11:13 AM   #5
 
PiqueABoo's Avatar
 
Join Date: Jan 2006
Posts: 498
Thanks: 1
Thanked 20 Times in 16 Posts
Rep Power: 9 PiqueABoo will become famous soon enoughPiqueABoo will become famous soon enough
Default

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?
  Reply With Quote
The Following User Says Thank You to PiqueABoo For This Useful Post:
SYNACK (17-06-2008)
Old 17-06-2008, 12:40 PM   #6
 
SYNACK's Avatar
 
Join Date: Oct 2007
Location: Auckland, New Zealand
Posts: 1,702
newzealand
Thanks: 59
Thanked 250 Times in 227 Posts
Blog Entries: 2
Rep Power: 54 SYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud of
Send a message via MSN to SYNACK
Default

Thanks for your responses, I have checked and it looks like it is picking it up in intellisense. The error was:

Quote:
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
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.

Thanks again for your help.

Last edited by SYNACK; 17-06-2008 at 12:44 PM..
  Reply With Quote
Old 17-06-2008, 07:49 PM   #7
 
PiqueABoo's Avatar
 
Join Date: Jan 2006
Posts: 498
Thanks: 1
Thanked 20 Times in 16 Posts
Rep Power: 9 PiqueABoo will become famous soon enoughPiqueABoo will become famous soon enough
Default

Quote:
I removed the SuppressUnmanagedCodeSecurity
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.
  Reply With Quote
Old 17-06-2008, 08:09 PM   #8
 
SYNACK's Avatar
 
Join Date: Oct 2007
Location: Auckland, New Zealand
Posts: 1,702
newzealand
Thanks: 59
Thanked 250 Times in 227 Posts
Blog Entries: 2
Rep Power: 54 SYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud of
Send a message via MSN to SYNACK
Default

Quote:
Originally Posted by PiqueABoo View Post
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.
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:

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);
    }
}
}
}
}
It is just a small component of a much larger and more devious device that I hope to construct, an intelligent network shutdown agent.
  Reply With Quote
Old 17-06-2008, 08:32 PM   #9
 
monkeyx's Avatar
 
Join Date: Nov 2006
Posts: 181
Thanks: 3
Thanked 13 Times in 11 Posts
Rep Power: 7 monkeyx will become famous soon enough
Default

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.
  Reply With Quote
Old 17-06-2008, 08:49 PM   #10
 
SYNACK's Avatar
 
Join Date: Oct 2007
Location: Auckland, New Zealand
Posts: 1,702
newzealand
Thanks: 59
Thanked 250 Times in 227 Posts
Blog Entries: 2
Rep Power: 54 SYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud ofSYNACK has much to be proud of
Send a message via MSN to SYNACK
Default

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
  Reply With Quote
Reply

Register now for FREE and post messages!


Username: Password: Confirm Password: E-Mail: Confirm E-Mail:
Birthday:      
Image Verification
  I agree to forum rules 

Similar Threads
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
Search Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +1. The time now is 02:56 AM.
Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
Copyright EduGeek.net