已知道API
--------------------------------
声明:   
    Declare   Function   SHShutDownDialog   Lib   "shell32"   Alias   "#60"   (ByVal   YourGuess   As   Long)   As   Long   
    
调用:   
    SHShutDownDialog   0 
--------------------------------
请问在C#里怎样写呀
比如我在窗体中有个"关机" 的按钮当点击时就把关闭windows的窗口调出来
请会的兄弟详细说说谢谢了!

解决方案 »

  1.   

    using System;
    using System.Runtime.InteropServices;
       
    class shoutdown{
       [StructLayout(LayoutKind.Sequential, Pack=1)]
       internal struct TokPriv1Luid
       {
          public int Count;
          public long Luid;
          public int Attr;
       }   [DllImport("kernel32.dll", ExactSpelling=true) ]
       internal static extern IntPtr GetCurrentProcess();   [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
       internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );   [DllImport("advapi32.dll", SetLastError=true) ]
       internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );   [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
       internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
    ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );   [DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
       internal static extern bool ExitWindowsEx( int flg, int rea );   internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
       internal const int TOKEN_QUERY = 0x00000008;
       internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
       internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
       internal const int EWX_LOGOFF = 0x00000000;
       internal const int EWX_SHUTDOWN = 0x00000001;
       internal const int EWX_REBOOT = 0x00000002;
       internal const int EWX_FORCE = 0x00000004;
       internal const int EWX_POWEROFF = 0x00000008;
       internal const int EWX_FORCEIFHUNG = 0x00000010;   private static void DoExitWin(int flg)
       {
          bool ok;
          TokPriv1Luid tp;
          IntPtr hproc = GetCurrentProcess();
          IntPtr htok = IntPtr.Zero;
          ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
          tp.Count = 1;
          tp.Luid = 0;
          tp.Attr = SE_PRIVILEGE_ENABLED;
          ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
          ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
          ok = ExitWindowsEx( flg, 0 );
       }      public static void Main()
          {
             Console.WriteLine("正在关闭计算机……");
             // 修改 EWX_SHUTDOWN 或者 EWX_LOGOFF, EWX_REBOOT等实现不同得功能。
             // 在XP下可以看到帮助信息,以得到不同得参数
             // SHUTDOWN /?
             DoExitWin(EWX_SHUTDOWN);
          }
    }
    作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2004年6月8日 1点43分37秒
      

  2.   

    jrl5365(king007)谢谢你 你给的是关机代码吧!!?我是想把系统自带的关机对话框调出来呀??
      

  3.   

    去看看  http://www.pinvoke.net/
      

  4.   

    有 一 个 未 公 开 的 API来 实 现 这 个 功 能 , 是 在 Shell32.dll中 , 但 没 有 名 字 , 索 引 号 为 60。 下 面 是 调 用 方 法 : 
        typedef int (CALLBACK *SHUTDOWNDLG) (int); 
         
        void CDlgDlg::OnOK() 
        { 
         CString s; 
         
         HINSTANCE hInst = LoadLibrary("shell32"); 
         SHUTDOWNDLG SHShutDownDialog; 
         
         if(hInst != 0) 
         { 
         SHShutDownDialog = (SHUTDOWNDLG)GetProcAddress(hInst, (LPSTR)60); 
         (*SHShutDownDialog)(0); 
         FreeLibrary(hInst); 
         } 
        } 
    GetProcAddress C# Signature:
    [DllImport("kernel32.dll", CharSet=CharSet.Ansi, ExactSpelling=true)]
    public static extern UIntPtr GetProcAddress(IntPtr hModule, string procName);
      

  5.   

    大哥们我水平一般看得不是很明请完整帮我写下好吗?
    using System;
    ............
     //这里要using什么??namespace 调用关机对话框
    {
            public class Form1 : System.Windows.Forms.Form
    {
                .........................
              这里要声明什么
                ......................... private void button1_Click(object sender, System.EventArgs e)
    {

                         // 这个按钮应怎样定
               }
              }
    }
      

  6.   

    我知道一定要引用这个的
    using System.Runtime.InteropServices; 急呀
    Firestone2003(笨笨小猪) 兄出手吧
      

  7.   

    就是用P/Invoke调用shell32.dll下面索引号为60的API...
    不会调用的话...那也没办法了...
      

  8.   

    chaircat(chaircat)兄不要这么说呀!!为人为到底,我是初学者呀
      

  9.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace 调用关机对话框
    {
        public partial class Form1 : Form
        {
            [DllImport("shell32",EntryPoint="#60")] 
            public extern static int SHShutdownDialog(int sig);
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                SHShutdownDialog(0);
                
            }
        }
    }搞不清楚里面函数的参数是什么意思,随便赋了个值,反正测试能过
    另外.能不能告诉我你怎么找到#60号函数的....未定义的函数我不太会查...
    有事和我联系
    [email protected]
      

  10.   

    public class DllInvoke
        {        #region Win API
            [DllImport("kernel32.dll")]
            private extern static IntPtr LoadLibrary(string path);        [DllImport("kernel32.dll")]
            private extern static IntPtr GetProcAddress(IntPtr lib, int funcName);        [DllImport("kernel32.dll")]
            private extern static bool FreeLibrary(IntPtr lib);
            #endregion        private IntPtr hLib;
            public DllInvoke(String DLLPath)
            {
                hLib = LoadLibrary(DLLPath);
            }        ~DllInvoke()
            {
                FreeLibrary(hLib);
            }        //将要执行的函数转换为委托
            public Delegate Invoke(int APIName, Type t)
            {
                IntPtr api = GetProcAddress(hLib, APIName);
                return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
            }
        }        public partial class Form1 : Form
            {                       public Form1()
                {
                    InitializeComponent();
                }
                public delegate int ShutDownComputer(int param);
                private void button1_Click(object sender, EventArgs e)
                {
                    DllInvoke dll = new DllInvoke("shell32.dll");                    ShutDownComputer shutdowndlg = (ShutDownComputer)dll.Invoke(60, typeof(ShutDownComputer));
                        shutdowndlg(0);
                }                   }晚上腐败去了,刚刚回来