+ Post New Thread
Results 1 to 10 of 10
Coding Thread, C# Reference System.Runtime.InteropServices? in Coding and Web Development; Hi I have some C# code that I want to use for a project that interfaces directly with some Windows ...
  1. #1

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,034
    Thank Post
    852
    Thanked 2,663 Times in 2,260 Posts
    Blog Entries
    9
    Rep Power
    766

    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.

  2. #2
    monkeyx's Avatar
    Join Date
    Nov 2006
    Posts
    364
    Thank Post
    8
    Thanked 52 Times in 41 Posts
    Rep Power
    25
    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 08:40 PM.

  3. Thanks to monkeyx from:

    SYNACK (17th June 2008)

  4. #3

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,034
    Thank Post
    852
    Thanked 2,663 Times in 2,260 Posts
    Blog Entries
    9
    Rep Power
    766
    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);

  5. #4
    monkeyx's Avatar
    Join Date
    Nov 2006
    Posts
    364
    Thank Post
    8
    Thanked 52 Times in 41 Posts
    Rep Power
    25
    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 Attached Images
    Last edited by monkeyx; 17th June 2008 at 09:40 AM.

  6. Thanks to monkeyx from:

    SYNACK (17th June 2008)

  7. #5

    Join Date
    Jan 2006
    Location
    Surburbia
    Posts
    2,178
    Thank Post
    74
    Thanked 307 Times in 243 Posts
    Rep Power
    115
    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?

  8. Thanks to PiqueABoo from:

    SYNACK (17th June 2008)

  9. #6

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,034
    Thank Post
    852
    Thanked 2,663 Times in 2,260 Posts
    Blog Entries
    9
    Rep Power
    766
    Thanks for your responses, I have checked and it looks like it is picking it up in intellisense. The error was:

    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; 17th June 2008 at 12:44 PM.

  10. #7

    Join Date
    Jan 2006
    Location
    Surburbia
    Posts
    2,178
    Thank Post
    74
    Thanked 307 Times in 243 Posts
    Rep Power
    115
    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.

  11. #8

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,034
    Thank Post
    852
    Thanked 2,663 Times in 2,260 Posts
    Blog Entries
    9
    Rep Power
    766
    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.

  12. #9
    monkeyx's Avatar
    Join Date
    Nov 2006
    Posts
    364
    Thank Post
    8
    Thanked 52 Times in 41 Posts
    Rep Power
    25
    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.

  13. #10

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,034
    Thank Post
    852
    Thanked 2,663 Times in 2,260 Posts
    Blog Entries
    9
    Rep Power
    766
    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

SHARE:
+ Post New Thread

Similar Threads

  1. LogMeIn & Java runtime 6
    By ITWombat in forum *nix
    Replies: 2
    Last Post: 31st December 2007, 10:03 PM
  2. Free stuff - online reference works
    By SpuffMonkey in forum Educational Software
    Replies: 0
    Last Post: 16th March 2007, 12:45 PM
  3. Is there much of a market for old reference material??
    By sidewinder in forum General Chat
    Replies: 4
    Last Post: 28th February 2007, 10:58 AM
  4. Replies: 5
    Last Post: 30th January 2007, 08:21 AM
  5. Updating Java Runtime
    By ITWombat in forum Windows
    Replies: 3
    Last Post: 18th May 2006, 12:44 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •