Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

FERMER, REBOOT, LOGOFF. DIRECT OU PAR DECOMPTE


Information sur la source

Catégorie :API Classé sous : reboot, token, timer, shutdown, rc Niveau : Débutant Date de création : 28/04/2006 Date de mise à jour : 28/04/2006 19:38:15 Vu / téléchargé: 6 206 / 487

Note :
9 / 10 - par 3 personnes
9,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (9)
Ajouter un commentaire et/ou une note

Description

Cliquez pour voir la capture en taille normale
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

  • /*
  • * 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;
  • }
/*
 * 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;
}

Fichier Zip

Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
  • main.cppTélécharger ce fichier [Réservé aux membres club]Voir ce fichier16 174 octets
  • main.oTélécharger ce fichier [Réservé aux membres club]9 833 octets
  • Makefile.winTélécharger ce fichier [Réservé aux membres club]838 octets
  • MyReboot.devTélécharger ce fichier [Réservé aux membres club]910 octets
  • MyReboot.ex_Télécharger ce fichier [Réservé aux membres club]33 972 octets

Télécharger le zip

Historique

28 avril 2006 16:31:28 :
- ortho
28 avril 2006 19:38:16 :
- image

Commentaires et avis

signaler à un administrateur
Commentaire de dPompei2 le 29/04/2006 13:51:03

on dirrait que tu t'y connais mieux en api windows que moi qui suis plutot linux :p
alors, p-e toi tu le sais: comment on peut executer un programme avec les droits d'un autre utilisateur ? je m'explique: qqun démarre mon prog puis a un moment le prog demande s'il veut faire qqcha vec les droits d'un autre utilisateur et il demande user+pass et alors il execute sa avec les droits de l'utilisatuer ... ça DOIT aller puisque j'ai déjà vu ça dans je sais plus quel programme ...

merci d'avance

signaler à un administrateur
Commentaire de dPompei2 le 29/04/2006 13:55:15

en plus je crois qu'il pourra même m'être bien utile ton ptit prog :P

signaler à un administrateur
Commentaire de BruNews le 29/04/2006 14:11:11 administrateur CS

Regarde CreateProcessAsUser() dans MSDN.

signaler à un administrateur
Commentaire de deck_bsd le 29/04/2006 17:29:19

Je ne le sais pas, mais maintenant que tu le dit cela serai une chose intéressente à faire. Je vai faire des recherches. En tout cas les (bon) conseils de BruNews m'on déjà mis sur une piste :p . sacré BruNews que ferait-on sans lui.

signaler à un administrateur
Commentaire de deck_bsd le 29/04/2006 17:35:24

sur la bonne voie : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocessasuser.asp

signaler à un administrateur
Commentaire de dPompei2 le 29/04/2006 19:00:29

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

signaler à un administrateur
Commentaire de deck_bsd le 02/05/2006 08:39:23

oui je la mettrai ici quand je l'aurai faite.

signaler à un administrateur
Commentaire de kerneltony95 le 11/10/2006 20:10:35

t' es balez toi en faite en api bj pour le code :)

signaler à un administrateur
Commentaire de maxil12 le 22/08/2007 11:34:09

thank you very match :)

Ajouter un commentaire

Discussions en rapport avec ce code source dans le forum

Reboot/Shutdown ordinateur distant [ par Troie ] Bonjour, j'aimerais savoir comment redémarrer et arrêter un ordinateur distant (en indiquant le nom de l'ordinateur par exemple) en utilisant VC++ ?Me ShutDown ou Reboot : WM_QUERYENDSESSION ou WM_ENDSESSION [ par mfrai02 ] Salut,lorsque je veux traiter le message WM_QUERYENDSESSION ou WM_ENDSESSION, je peux déterminer grace au lParam si l'utilisateur à réaliser un logoff Comment on peut utiliser les timer avec Windows? [ par electroonn ] Bonjour,je me demande comment on peut armer un timer en C++, je sais qu'il ya des fonctions comme SetTimer et KillTimer, mais je ne sais pas trop comm Unicode et fichiers .RC ... [ par orbb ] Bonjour,J'ai un programme unicode qui traite des données en arabe, et son interface decrite dans un fichier .rc (ressources) est en francais.Mon probl SetTimer() [ par niou42 ] Bonjourvoila j'aimerais mettre en place un timer ( et donc aussi un kill timer ) en c++j'ai un bouton dans une mfc qui contient ce code :while(true){  Timer Dll [ par wxccxw ] Salut, j'ai une dll injecter et j'aimerai faire l'equivalent d'un sleep dedans pour attendre une seconde avant de lancer un truc quel method je doit u Visual C++ 6 : error LNK2001: unresolved external symbol [ par gros_landais ] Lorsque je link ce server TCP/IP avec Visual C++ 6.0 j'ai les erreurs de link suivante :--------------------Configuration: all - Win32 Debug---------- timer generer automatiquement [ par mars527 ] Bonjour a tousvoila je vous expose mon probleme:je dois rendre un projet d'informatique la semaine prochaine, projet qui doit simuler un carrefour.la INSERT dans une base de données access [ par hitle ] Salut,Je suis actuellement en train de réaliser une application intranet. Pour ce projet j'ai besoin de faire des INSERT dans une base de données de t Pb avec la fonction system ("shutdown....") [ par heresia83 ] Bonjour ! Je débute en C.. et je n'arrive pas à faire fonctionner la fonction "system".J'ai fait de nombreuses recherches, notamment sur ce forum, j'a


Nos sponsors

Sondage...

CalendriCode

Juillet 2009
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
2728293031  

Consulter la suite du CalendriCode

Téléchargements

Comparez les prix Nouvelle version

Photothèque Nouveau !



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,250 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS


Certaines images présentes sur le site (notament certains avatars) sont issues des collections IconShock, donc si vous souhaitez utiliser ces icons vous devez les acheter, ne les copiez pas et ne utilisez pas dans vos sites et applications sans les avoir commandé.