IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Reboot d'un ordinateur avec une session verrouillé


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Développeur DELPHI
    Inscrit en
    Février 2011
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure et Loir (Centre)

    Informations professionnelles :
    Activité : Développeur DELPHI
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Février 2011
    Messages : 26
    Par défaut Reboot d'un ordinateur avec une session verrouillé
    Bonjour à tous,

    Je suis actuellement en train de développer un logiciel qui permet de rebooter un ordinateur à une heure voulut.

    Mon problème, c'est dès que je verrouille la session de celui-ci le reboot ne fonctionne pas.

    Pouvez-vous m'aider ?

    Cordialement

    Frank

  2. #2
    Max
    Max est déconnecté
    Expert confirmé

    Avatar de Max
    Homme Profil pro
    Artisan développeur
    Inscrit en
    Mai 2007
    Messages
    2 954
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Artisan développeur
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2007
    Messages : 2 954
    Par défaut
    Salut.

    Comment tu t'y prends pour faire rebooter la machine ? Tu utilises quelle méthode ? Montre quelques bouts de code sinon ça va être difficile de t'aider .

  3. #3
    Membre averti
    Homme Profil pro
    Développeur DELPHI
    Inscrit en
    Février 2011
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure et Loir (Centre)

    Informations professionnelles :
    Activité : Développeur DELPHI
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Février 2011
    Messages : 26
    Par défaut
    Bonjour,

    En faite, j'utilise les API de Windows.

    C'est un exemple qui avait été posté sur ce forum.

    Voici le contenu de mon fichier ClassAPI.cs

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
     
    namespace PcShutdown
    {
        public struct ProcessEntry32
        {
            public uint dwSize;
            public uint cntUsage;
            public uint th32ProcessID;
            public IntPtr th32DefaultHeapID;
            public uint th32ModuleID;
            public uint cntThreads;
            public uint th32ParentProcessID;
            public int pcPriClassBase;
            public uint dwFlags;
     
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            public string szExeFile;
        }
     
        class ClassAPI
        {
     
            [DllImport("KERNEL32.DLL")]
            public static extern int CreateToolhelp32Snapshot(uint flags, uint processid);
     
            [DllImport("KERNEL32.DLL")]
            public static extern int Process32First(int handle, ref ProcessEntry32 pe);
     
            [DllImport("KERNEL32.DLL")]
            public static extern int Process32Next(int handle, ref ProcessEntry32 pe);
     
            [DllImport("KERNEL32.DLL")]
            public static extern int CloseHandle(int handle);
     
            public uint SNAPPROCESS = 2;
     
            [DllImport("KERNEL32.DLL")]
            public static extern int OpenProcess(uint dwDesiredAccess, int bInheritHandle, uint dwProcessId);
     
            [StructLayout(LayoutKind.Sequential)]
            public class PROCESS_MEMORY_COUNTERS
            {
                public int cb;
                public int PageFaultCount;
                public int PeakWorkingSetSize;
                public int WorkingSetSize;
                public int QuotaPeakPagedPoolUsage;
                public int QuotaPagedPoolUsage;
                public int QuotaPeakNonPagedPoolUsage;
                public int QuotaNonPagedPoolUsage;
                public int PagefileUsage;
                public int PeakPagefileUsage;
            }
     
            [DllImport("psapi.dll")]
            public static extern int GetProcessMemoryInfo(int hProcess, [Out] PROCESS_MEMORY_COUNTERS counters, int size);
     
            [StructLayout(LayoutKind.Sequential)]
            public struct LUID
            {
                public uint LowPart;
                public uint HighPart;
            };
     
            public const int ANYSIZE_ARRAY = 1;
     
            [StructLayout(LayoutKind.Sequential)]
            public struct TOKEN_PRIVILEGES
            {
                public uint PriviledgeCount;
                public LUID Luid;
                public uint Attributes;
            }
     
            [DllImport("KERNEL32.DLL")]
            public static extern int GetCurrentProcess();
     
            [DllImport("advapi32.dll")]
            public static extern System.Boolean OpenProcessToken(
                int ProcessHandle,
                uint DesiredAccess,
                ref int Tokenhandle
            );
     
            [DllImport("advapi32.dll")]
            public static extern System.Boolean LookupPrivilegeValue(
                string lpSsytemName,
                string lpName,
                ref LUID pluid
            );
     
            [DllImport("advapi32.dll")]
            public static extern int AdjustTokenPrivileges(
                int TokenHandle,
                int DisableAllPrivileges,
                [MarshalAs(UnmanagedType.Struct)] ref TOKEN_PRIVILEGES NewState,
                int BufferLength,
                int PreviousState,
                int ReturnLength
            );
     
            [DllImport("user32.dll")]
            public static extern bool ExitWindowsEx(int flg, int rea);
     
            public enum EWX_ENUM
            {
                EWX_LOGOFF = 0x00000000,
                EWX_SHUTDOWN = 0x00000001,
                EWX_REBOOT = 0x00000002,
                EWX_FORCE = 0x00000004,
                EWX_POWEROFF = 0x00000008,
                EWX_FORCEIFHUNG = 0x00000010,
                EWX_FORCEREBOOT = EWX_REBOOT | EWX_FORCE,
                EWX_FORCEIFHUNGREBOOT = EWX_REBOOT | EWX_FORCEIFHUNG,
                EWX_FORCESHUTDOWN = EWX_SHUTDOWN | EWX_FORCE,
                EWX_FORCEIFHUNGSHUTDOWN = EWX_SHUTDOWN | EWX_FORCEIFHUNG,
                EWX_FORCEPOWEROFF = EWX_POWEROFF | EWX_FORCE,
                EWX_FORCEIFHUNGPOWEROFF = EWX_POWEROFF | EWX_FORCEIFHUNG,
                EWX_FORCELOGOFF = EWX_LOGOFF | EWX_FORCE,
                EWX_FORCEIFHUNGLOGOFF = EWX_LOGOFF | EWX_FORCEIFHUNG
            }
     
     
            public static bool FindProcessByName(string pname)
            {
                int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0);
                ProcessEntry32 pe32 = new ProcessEntry32();
                pe32.dwSize = 296;
     
                while (Process32Next(SnapShot, ref pe32) != 0)
                {
     
                    string xname = pe32.szExeFile.ToString();
     
                    if (pname.CompareTo(xname) == 0)
                    {
                        CloseHandle(SnapShot);
                        return true;
                    }
                }
                CloseHandle(SnapShot);
                return false;
            }
     
            public static int GetNumberRunning(string pname)
            {
                int ProcCount = 0;
                int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0);
                ProcessEntry32 pe32 = new ProcessEntry32();
                pe32.dwSize = 296;
                while (Process32Next(SnapShot, ref pe32) != 0)
                {
                    string xname = pe32.szExeFile.ToString();
                    if (pname.CompareTo(xname) == 0)
                    {
                        ProcCount++;
                    }
                }
                CloseHandle(SnapShot);
                return ProcCount;
            }
     
            public static uint GetProcessIdForProcess(string pname)
            {
                uint procid = 0;
                int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0);
                ProcessEntry32 pe32 = new ProcessEntry32();
                pe32.dwSize = 296;
                while (Process32Next(SnapShot, ref pe32) != 0)
                {
                    string xname = pe32.szExeFile.ToString();
                    if (pname.CompareTo(xname) == 0)
                    {
                        procid = pe32.th32ProcessID;
                        break;
                    }
                }
                CloseHandle(SnapShot);
                return procid;
            }
     
            public static int GetMemoryUsageForProcess(string pname)
            {
                try
                {
                    return GetMemoryUsageForProcess(GetProcessIdForProcess(pname));
                }
                catch
                {
                    return -1;
                }
            }
     
            public static int GetMemoryUsageForProcess(uint pid)
            {
                int mem = 0;
                int pHandle = OpenProcess(0x0400 | 0x0010, 0, pid);
                PROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS();
                if (GetProcessMemoryInfo(pHandle, pmc, 40) != 0)
                {
                    mem = pmc.WorkingSetSize;
                }
                CloseHandle(pHandle);
                return mem;
            }
     
            public static bool IsMoreThanOneRunning(string pname)
            {
                if (GetNumberRunning(pname) > 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
     
     
            public static void ExitSystem(int EWX_VALUE)
            {
                TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES();
                TOKEN_PRIVILEGES tpOld = new TOKEN_PRIVILEGES();
                LUID luid = new LUID();
     
                LookupPrivilegeValue(null, "SeShutdownPrivilege", ref luid);
                int processHandle = GetCurrentProcess();
                int TokenHandle = 0;
                OpenProcessToken(processHandle, 0x00000020 | 0x00000008, ref TokenHandle);
     
                tp.PriviledgeCount = 1;
                tp.Attributes = 0x00000002;
                tp.Luid = luid;
     
                int tpsz = Marshal.SizeOf(tp);
                tpsz = AdjustTokenPrivileges(TokenHandle, 0, ref tp, tpsz, 0, 0);
                ExitWindowsEx(EWX_VALUE, 0);
            }
     
           /* private void button1_Click(object sender, EventArgs e)
            {
                ExitSystem(Convert.ToInt32(EWX_ENUM.EWX_FORCEREBOOT));
     
            }
            */
     
        }
    }
    Et j'appelle mes fonctions de cette façon
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    //Reboote normal
     
    ClassAPI.ExitSystem(Convert.ToInt32(ClassAPI.EWX_ENUM.EWX_REBOOT));
     
    //Reboote forcé
     
    ClassAPI.ExitSystem(Convert.ToInt32(ClassAPI.EWX_ENUM.EWX_FORCEREBOOT));
    À savoir que le logiciel fonctionne parfaitement, tant que la session n'est pas verrouillée.

  4. #4
    Membre éprouvé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Avril 2006
    Messages
    1 627
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 627
    Par défaut
    Par qui/comment et quand démarre le programme ?

  5. #5
    Membre averti
    Homme Profil pro
    Développeur DELPHI
    Inscrit en
    Février 2011
    Messages
    26
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure et Loir (Centre)

    Informations professionnelles :
    Activité : Développeur DELPHI
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Février 2011
    Messages : 26
    Par défaut
    Le programme démarre au démarrage de Windows.

    Par qui : l'utilisateur local.

    Celui-ci est administrateur du poste.

  6. #6
    Expert confirmé

    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Septembre 2006
    Messages
    3 580
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Septembre 2006
    Messages : 3 580
    Par défaut
    salut

    une solution, le code suivant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
     
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            [DllImport("kernel32.dll")]
            public static extern uint GetLastError();
            [DllImport("kernel32.dll")]
            public static extern uint FormatMessage(uint Flags, IntPtr Source, uint MessageID, uint LanguageID, StringBuilder Buffer, uint Size, IntPtr Args);
     
            [DllImport("advapi32.dll")]
            static extern bool InitiateSystemShutdown(
                        [MarshalAs(UnmanagedType.LPStr)] string lpMachinename,
                        [MarshalAs(UnmanagedType.LPStr)] string lpMessage,
                        Int32 dwTimeout,
                        bool bForceAppsClosed,
                        bool bRebootAfterShutdown);
     
            private void button1_Click(object sender, EventArgs e)
            {
                bool state = InitiateSystemShutdown(@"\\127.0.0.1", string.Empty, 30, true, true);
                if (!state)
                {
                    StringBuilder Buffer = new StringBuilder(512);
                    uint MessageLength = 0;
     
                    MessageLength = FormatMessage(0x00001000, IntPtr.Zero, GetLastError(), 0, Buffer, (uint)Buffer.Capacity, IntPtr.Zero);
                    if (MessageLength > 0)
                    {
                        string Error = Buffer.ToString();
                        Error = Error.Substring(0, Error.Length - 2);
                        MessageBox.Show(Error + " (" + GetLastError().ToString() + ")");
                    }
                }
            }
        }
    Et session verrouillée, cela fonctionne également

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème avec une session
    Par ploxien dans le forum Hibernate
    Réponses: 4
    Dernier message: 07/11/2008, 17h36
  2. Comment faire une direction avec une session
    Par pierrot10 dans le forum Langage
    Réponses: 3
    Dernier message: 20/06/2007, 21h35
  3. Réponses: 6
    Dernier message: 04/09/2006, 15h48
  4. Réponses: 13
    Dernier message: 16/04/2004, 12h00
  5. [debutant][jsp]Passage d'entier avec une session sans cookie
    Par o151181 dans le forum Servlets/JSP
    Réponses: 5
    Dernier message: 04/02/2004, 18h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo