Fermer, reboot, logoff. direct ou par decompte

Soyez le premier à donner votre avis sur cette source.

Vue 9 246 fois - Téléchargée 648 fois

Description

Et bien tous est écrit dans le titre.

Voici ce que cette source apporte comme aide:

- utilisation de token.
- API ExitWindows, ExitWindowsEx, SetTimer et KillTimer.
- DialogBox sans rc.
- utilisation de Timer.

Si vous avez des suggestions. N'hésitez pas

Source / Exemple :


/*

  • Crée par : Deck_bsd
  • Date de création : 27/04/2006
  • Fonction : Redémarrer l'ordinateur, éteindre l'ordinateur ou fermer l'utilisateur courrant.
  • /
#include <windows.h> #include <stdio.h> #include <commctrl.h> #define IDM_QUIT 11 #define IDM_HELP 22 /* Declare Windows procedure */ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK PropoProc(HWND hwDlg, UINT message, WPARAM wParam, LPARAM lParam); unsigned int ConvertHourToMillisecond(HWND hwCountDown,unsigned int uiSize); void token(); /* Make the class name into a global variable */ char szClassName[ ] = "WindowsApp"; HINSTANCE GlobalHInstance; int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { HWND hwnd; /* This is the handle for our window */ MSG messages; /* Here messages to the application are saved */ WNDCLASSEX wincl; /* Data structure for the windowclass */ HMENU hmMain; HMENU hmHelp; HMENU hmQuit; /* The Window structure */ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ wincl.style = CS_DBLCLKS; /* Catch double-clicks */ wincl.cbSize = sizeof (WNDCLASSEX); /* Use default icon and mouse-pointer */ wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; /* No menu */ wincl.cbClsExtra = 0; /* No extra bytes after the window class */ wincl.cbWndExtra = 0; /* structure or the window instance */ /* Use Windows's default color as the background of the window */ wincl.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1); /* Création du menu */ hmQuit = CreateMenu(); AppendMenu(hmQuit,MF_STRING,IDM_QUIT,"Exit"); hmMain = CreateMenu(); AppendMenu(hmMain,MF_POPUP,(UINT)hmQuit,"&File"); hmHelp = CreateMenu(); AppendMenu(hmHelp,MF_STRING,IDM_HELP,"A propos de..."); AppendMenu(hmMain,MF_POPUP,(UINT)hmHelp,"?"); /* Register the window class, and if it fails quit the program */ if (!RegisterClassEx (&wincl)) return 0; /* The class is registered, let's create the program*/ hwnd = CreateWindowEx ( 0, /* Extended possibilites for variation */ szClassName, /* Classname */ "MyReboot", /* Title Text */ WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, /* default window */ CW_USEDEFAULT, /* Windows decides the position */ CW_USEDEFAULT, /* where the window ends up on the screen */ 300, /* The programs width */ 310, /* and height in pixels */ HWND_DESKTOP, /* The window is a child-window to desktop */ hmMain, /* No menu */ hThisInstance, /* Program Instance handler */ NULL /* No Window Creation data */ ); GlobalHInstance = hThisInstance; /* Make the window visible on the screen */ ShowWindow (hwnd, nFunsterStil); /* Run the message loop. It will run until GetMessage() returns 0 */ while (GetMessage (&messages, NULL, 0, 0)) { if(messages.message == WM_KEYDOWN && messages.wParam == VK_ESCAPE) PostQuitMessage(0); /* Translate virtual-key messages into character messages */ TranslateMessage(&messages); /* Send message to WindowProcedure */ DispatchMessage(&messages); } /* The program return-value is 0 - The value that PostQuitMessage() gave */ return messages.wParam; } /* This function is called by the Windows function DispatchMessage() */ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HWND hwButtonLogOff,hwButtonReboot,hwButtonShutDown,hwButtonFormat,hwButtonStop; static HWND hwLabelUser; static HWND hwStatusBar; static HWND hwCountDown; HWND hwLabelCD; static unsigned short int usiChoice=0; unsigned int uiSize; char szBuffer[30]; char szText[60]="Current user : "; DWORD dwWord = 30; switch (message) /* handle the messages */ { case WM_CREATE : hwButtonLogOff = CreateWindow("button","Log off",WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 90,60,100,20,hwnd,(HMENU)1,GlobalHInstance,NULL); hwButtonReboot = CreateWindow("button","Reboot",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 90,100,100,20,hwnd,(HMENU)2,GlobalHInstance,NULL); hwButtonShutDown = CreateWindow("button","Shutdown",WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 90,140,100,20,hwnd,(HMENU)3,GlobalHInstance,NULL); hwButtonFormat = CreateWindow("button","Format",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 210,185,55,20,hwnd,(HMENU)4,GlobalHInstance,NULL); hwButtonStop = CreateWindow("button","Stop the countdown",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 70,220,150,20,hwnd,(HMENU)5,GlobalHInstance,NULL); hwLabelUser = CreateWindow("static","",WS_CHILD | WS_VISIBLE | SS_SUNKEN | SS_CENTER, 18,25,260,20,hwnd,NULL,GlobalHInstance,NULL); hwCountDown = CreateWindow("edit","",WS_CHILD | WS_VISIBLE | WS_BORDER | ES_CENTER, 90,185,100,20,hwnd,NULL,GlobalHInstance,NULL); hwLabelCD = CreateWindow("static","Countdown ? : ",WS_CHILD | WS_VISIBLE, 5,185,85,20,hwnd,NULL,GlobalHInstance,NULL); hwStatusBar = CreateStatusWindow(WS_VISIBLE | WS_CHILD, "No action",hwnd,1); HFONT PoliceTahoma; PoliceTahoma = CreateFont(15,0,0,0,0,0,0,0,0,0,0,0,0,"tahoma"); SendMessage(hwLabelUser,WM_SETFONT,(long)PoliceTahoma,0); SendMessage(hwCountDown,WM_SETFONT,(long)PoliceTahoma,0); SendMessage(hwLabelCD,WM_SETFONT,(long)PoliceTahoma,0); SendMessage(hwButtonFormat,WM_SETFONT,(long)PoliceTahoma,0); SendMessage(hwButtonStop,WM_SETFONT,(long)PoliceTahoma,0); GetUserName(szBuffer,&dwWord); lstrcat(szText,szBuffer); SetWindowText(hwLabelUser,szText); return 0; case WM_COMMAND : if(HIWORD(wParam) == BN_CLICKED){ switch(LOWORD(wParam)){ case 1 : uiSize = SendMessage(hwCountDown,WM_GETTEXTLENGTH,0,0); if(uiSize != 0){ usiChoice = 1; SetTimer(hwnd,(UINT)1,ConvertHourToMillisecond(hwCountDown,uiSize),NULL); }else{ ExitWindows(0,0); SetWindowText(hwStatusBar,"Log off of the current user..."); } break; case 2 : uiSize = SendMessage(hwCountDown,WM_GETTEXTLENGTH,0,0); if(uiSize != 0){ usiChoice = 2; SetTimer(hwnd,(UINT)1,ConvertHourToMillisecond(hwCountDown,uiSize),NULL); }else{ token(); ExitWindowsEx(EWX_REBOOT | EWX_FORCE,0); SetWindowText(hwStatusBar,"Reboot..."); } break; case 3 : uiSize = SendMessage(hwCountDown,WM_GETTEXTLENGTH,0,0); if(uiSize != 0){ usiChoice = 3; SetTimer(hwnd,(UINT)1,ConvertHourToMillisecond(hwCountDown,uiSize),NULL); }else{ token(); ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,0); SetWindowText(hwStatusBar,"Windows is shutting down"); } break; case 4 : MessageBox(hwnd,"Hour Format : 00:00:00","Format",MB_OK | MB_ICONINFORMATION); break; case 5 : KillTimer(hwnd,(UINT)1);break; case 11 : PostQuitMessage(0); break; case 22 : HGLOBAL hgMemory; LPDLGTEMPLATE dltDlgHelp; LPWORD lpWord; LPWSTR lpwsWindowNameUnicode; hgMemory = GlobalAlloc(GPTR,512); if(!hgMemory) break; dltDlgHelp = (LPDLGTEMPLATE) hgMemory; dltDlgHelp->style = WS_CAPTION | WS_POPUP | DS_MODALFRAME | WS_SYSMENU | WS_MINIMIZEBOX; dltDlgHelp->cx = 210; dltDlgHelp->cy = 100; dltDlgHelp->x = GetSystemMetrics(SM_CXSCREEN)/6; dltDlgHelp->y = GetSystemMetrics(SM_CYSCREEN)/6; lpWord = (LPWORD) (dltDlgHelp+1); lpwsWindowNameUnicode = (LPWSTR) (lpWord+2); MultiByteToWideChar(CP_ACP,0,"A propos de :",-1,lpwsWindowNameUnicode,128); DialogBoxIndirect(GlobalHInstance,dltDlgHelp,0,(DLGPROC)PropoProc); break; } } return 0; case WM_TIMER : switch(usiChoice){ case 1 : ExitWindows(0,0);SetWindowText(hwStatusBar,"Log off of the current user...");break; case 2 : token();ExitWindowsEx(EWX_REBOOT | EWX_FORCE,0);SetWindowText(hwStatusBar,"Reboot...");break; case 3 : token();ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,0);SetWindowText(hwStatusBar,"Windows is shutting down");break; } KillTimer(hwnd,(UINT)1); return 0; case WM_DESTROY : PostQuitMessage (0); /* send a WM_QUIT to the message queue */ break; default: /* for messages that we don't deal with */ return DefWindowProc (hwnd, message, wParam, lParam); } return 0; } void token(){ HANDLE haToken; TOKEN_PRIVILEGES tpToken; /* Crée une nouvelle "file" de process qui va s'ajuster au autre */ OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&haToken); /* On recherche la valeur du privilège */ LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tpToken.Privileges[0].Luid); /* Nombre d'entrées dans le tableau des privileges */ tpToken.PrivilegeCount = 1; /* On défini l'attribut */ tpToken.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(haToken,FALSE,&tpToken,sizeof(tpToken),NULL,NULL); } LRESULT CALLBACK PropoProc(HWND hwDlg, UINT message, WPARAM wParam, LPARAM lParam){ HWND hwGroup,hwButtonOk,hwLabelInfo; HWND hwButtonSite,hwButtonMail; switch(message){ case WM_INITDIALOG : hwGroup = CreateWindow("button","MyReboot",WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 10,10,400,160,hwDlg,NULL,GlobalHInstance,NULL); hwButtonOk = CreateWindow("button","OK",WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 16,146,30,20,hwDlg,(HMENU)111,GlobalHInstance,NULL); hwLabelInfo = CreateWindow("static","Créer par : Deck_bsd \n\nVersion : 3.0 \n\nLicense : Freeware",WS_CHILD | WS_VISIBLE, 120,30,150,90,hwDlg,NULL,GlobalHInstance,NULL); hwButtonSite = CreateWindow("button","http://deck-bsd.eurower.net",WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 120,115,200,20,hwDlg,(HMENU)1111,GlobalHInstance,NULL); hwButtonMail = CreateWindow("button","deck_bsd01@yahoo.fr",WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 120,140,200,20,hwDlg,(HMENU)11111,GlobalHInstance,NULL); HFONT PoliceComic; HFONT PoliceVerdana; PoliceComic = CreateFont(15,0,0,0,0,0,0,0,0,0,0,0,0,"comic sans MS"); PoliceVerdana = CreateFont(15,0,0,0,0,0,0,0,0,0,0,0,0,"verdana"); SendMessage(hwGroup,WM_SETFONT,(long)PoliceComic,0); SendMessage(hwButtonOk,WM_SETFONT,(long)PoliceComic,0); SendMessage(hwLabelInfo,WM_SETFONT,(long)PoliceVerdana,0); SendMessage(hwButtonSite,WM_SETFONT,(long)PoliceVerdana,0); SendMessage(hwButtonMail,WM_SETFONT,(long)PoliceVerdana,0); break; case WM_COMMAND : if(HIWORD(wParam) == BN_CLICKED){ switch(LOWORD(wParam)){ case 111 : EndDialog(hwDlg,0); break; case 1111 : ShellExecute(NULL,"open","http://deck-bsd.eurower.net",0,NULL,SW_MAXIMIZE); break; case 11111 : ShellExecute(NULL,"open","mailto:deck_bsd01@yahoo.fr",0,NULL,SW_MAXIMIZE); break; } } break; case WM_CLOSE : EndDialog(hwDlg,0); break; } return 0; } unsigned int ConvertHourToMillisecond(HWND hwCountDown,unsigned int uiSize){ char *ptTime; unsigned int uiHour,uiMin,uiSec,uiTotal; ptTime = (char*)malloc(sizeof(char)*uiSize+1); GetWindowText(hwCountDown,ptTime,uiSize+1); sscanf(ptTime,"%d:%d:%d",&uiHour,&uiMin,&uiSec); free(ptTime); uiTotal = (uiHour*60)*60 + uiMin*60 + uiSec; uiTotal *= 1000; return uiTotal; }

Codes Sources

A voir également

Ajouter un commentaire Commentaires
Messages postés
5
Date d'inscription
lundi 6 novembre 2006
Statut
Membre
Dernière intervention
23 novembre 2007

thank you very match :)
Messages postés
152
Date d'inscription
dimanche 22 janvier 2006
Statut
Membre
Dernière intervention
26 mars 2007

t' es balez toi en faite en api bj pour le code :)
Messages postés
1243
Date d'inscription
jeudi 31 mars 2005
Statut
Membre
Dernière intervention
3 août 2016
2
oui je la mettrai ici quand je l'aurai faite.
Messages postés
55
Date d'inscription
samedi 27 mars 2004
Statut
Membre
Dernière intervention
1 septembre 2006

eh bien merci les ga' sa fait plaisir des réponses aussi rapides !
BRUNEWS j'avais cherché déjà mais jamais trouvé ... merci beaucoup ;)
DECK_BSD, si tu fais un petit programme qui utilise ça, tu le mettras ici ? si oui, je l'attends deja avec impatience :p
Messages postés
1243
Date d'inscription
jeudi 31 mars 2005
Statut
Membre
Dernière intervention
3 août 2016
2
Afficher les 9 commentaires

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.