已知道API
--------------------------------
声明:
Declare Function SHShutDownDialog Lib "shell32" Alias "#60" (ByVal YourGuess As Long) As Long
调用:
SHShutDownDialog 0
--------------------------------
请问在C#里怎样写呀
比如我在窗体中有个"关机" 的按钮当点击时就把关闭windows的窗口调出来
请会的兄弟详细说说谢谢了!
--------------------------------
声明:
Declare Function SHShutDownDialog Lib "shell32" Alias "#60" (ByVal YourGuess As Long) As Long
调用:
SHShutDownDialog 0
--------------------------------
请问在C#里怎样写呀
比如我在窗体中有个"关机" 的按钮当点击时就把关闭windows的窗口调出来
请会的兄弟详细说说谢谢了!
解决方案 »
- C#TCP服务端socket异步编程问题
- 奇怪的Form的MouseEnter和MouseLeave事件
- 找到吐血没解决的问题,高手近来看看,关于图形的,实时曲线的缩放......
- winform程序動態指定服務器ip問題?
- vs2005中如何发送HTML的邮件
- 一个关于WINFORM更新datagrid写库问题~谢谢!
- c# mdi程序问题,怎样去除子窗口最大化后的"最小化""还原""关闭"按钮?
- C#如何判断对比两个集合?
- 定义简单的interface为什么出错,有简单代码;谢谢!
- Aforge同時显示多个摄像头的问题(急)
- 使用Microsoft Visual Studio 2005 无法"启动调试"怎么办?
- 比较字符串的问题。。。
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秒
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);
using System;
............
//这里要using什么??namespace 调用关机对话框
{
public class Form1 : System.Windows.Forms.Form
{
.........................
这里要声明什么
......................... private void button1_Click(object sender, System.EventArgs e)
{
// 这个按钮应怎样定
}
}
}
using System.Runtime.InteropServices; 急呀
Firestone2003(笨笨小猪) 兄出手吧
不会调用的话...那也没办法了...
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]
{ #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);
} }晚上腐败去了,刚刚回来