PDA

View Full Version : سوال: shutdown به همراه بستن اجباری همه برنامه ها



NASA's Spaceman
سه شنبه 12 فروردین 1393, 18:17 عصر
سلام
من یه کد برا خاموش کردن نوشتم ولی اگه برنامه ای باز باشه خاموش نمیشه
میشه کد به اجبار بستن تمام پنجره های باز رو بهم بدین؟
با سپاس

اینم کد
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;


namespace WindowsFormsApplication93
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("powerprof.dll")]
public static extern Int32 SetSuspendState(Int32 Hibernate, Int32 force, Int32 Disiable);
[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int uflags, int dwReason);
[DllImport("user32.dll")]
public static extern void LockworkStation();
private void button1_Click(object sender, EventArgs e)
{
Process.Start("shutdown", "-r -t 00");
}

Mahmoud.Afrad
سه شنبه 12 فروردین 1393, 19:07 عصر
اگر میخوای از پروسس shutdown.exe لستفاده کنی پس نقش لون apiهایی که import کردی چیه؟!!!!!!!!!!!!!!!

NASA's Spaceman
سه شنبه 12 فروردین 1393, 19:13 عصر
من فقط همین یه کدش رو گذاشتم فقط جواب بگیرم و بقیه dll ها کدهاشون رو نوشتم
خواهشا کد بستن پنجره ها رو بهم بدین
با سپاس

sohil_ww
سه شنبه 12 فروردین 1393, 19:19 عصر
Process.Start("shutdown", "-r -t 00");

اگه اشتباه نکنم این دستور ریست باشه نه خاموش کردن

Process.Start("Shutdown", "/s /t 0");
این پارامتر های خاموش کردنه

NASA's Spaceman
سه شنبه 12 فروردین 1393, 19:21 عصر
دوست عزیز اینو که خودمم بلدم
میخوام وقتی خاموش یا ریستارت میکنه خودش هر چی پنجره باز هست رو ببنده و سوال نکنه
با سپاس

Mahmoud.Afrad
سه شنبه 12 فروردین 1393, 19:24 عصر
اگر از پروسس shutdown استفاده میکنی باید پارامتر f رو هم ارسال کنید.

ولی بهتره از همون api استفاده کنید.

[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 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);
}

private void button1_Click(object sender, EventArgs e)
{
// Forces processes to terminate if they do not respond
DoExitWin(EWX_FORCEIFHUNG);
}

NASA's Spaceman
سه شنبه 12 فروردین 1393, 19:29 عصر
ممنون
ولی کدنویسی کوتاه تر نداره؟
با سپاس

Mahmoud.Afrad
سه شنبه 12 فروردین 1393, 19:36 عصر
والا معمولش اینه که کدهای بدون استفاده رو کنار میزارن

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int flag, int reason);
internal const int EWX_FORCEIFHUNG = 0x00000010;
private void button1_Click(object sender, EventArgs e)
{
ExitWindowsEx(EWX_FORCEIFHUNG, 0);
}


میتونی کد کامل رو به صورت کلاس دربیاری و در هر پروژه ای ازش استفاده کنی.

NASA's Spaceman
سه شنبه 12 فروردین 1393, 19:47 عصر
چجوری میتونم یه کلاس رو تو هر پروژه ای خواستم استفاده کنم
خواهشا کدنویسیش رو بهم بگین
با سپاس

NASA's Spaceman
سه شنبه 12 فروردین 1393, 22:29 عصر
ببخشید اون کد طولانی با اون کد کوتاه شده چه فرقی داره؟
با سپاس

Mousavmousab
چهارشنبه 13 فروردین 1393, 01:14 صبح
چجوری میتونم یه کلاس رو تو هر پروژه ای خواستم استفاده کنم
خواهشا کدنویسیش رو بهم بگین
با سپاس



یک کلاس بساز و یه نامی بهش اختصاص بده مثلاٌ mainFunction بعد این کد ها را توش بنویس . البته یک تابع با معنی مربوط به اون کار.
همین کلاس را یه جا نگه دار بعد توی هر پروژه Copy/paste کن به همین راحتی

viiictor
چهارشنبه 13 فروردین 1393, 01:47 صبح
از shutdown.exe ویندوز با استفاده از این دستور استفاده کنید که f/ برای Force Close برنامه های در حال اجرا ، s/ برای shutdown و 0 f/ برای زمان خاموش کردن = 0 استفاده میشود

shutdown /s /f /t 0