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 :

lancer une appli sur le bureau depuis un service


Sujet :

C#

  1. #1
    Membre confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juin 2005
    Messages
    700
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Juin 2005
    Messages : 700
    Points : 488
    Points
    488
    Par défaut lancer une appli sur le bureau depuis un service
    Bonjour
    Je n'arrive pas a trouver un article clair expliquant comment faire.

    J'ai bien compris que le service s'execute dans la session 0
    j'ai lu dans msdn qu'il fallait faire un pinvoke de CreateProcessAsUser
    mais je n'ai pas trouvé d'exemple complet et fonctionnel en c#.

    Pourriez vous m'aider?

    PS : j'arrive à récupérer le token de l'utilisateur grace à l'event OnSessionChange, reste plus qu'a trouver comment lancer un processus sur son bureau (dans sa session quoi...).

    L'idée finale : un autoupdater qui relance l'appli une fois la mise a jour effectuée.

    Merci

  2. #2
    Expert éminent sénior Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 172
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 172
    Points : 25 112
    Points
    25 112
    Par défaut
    je ne suis pas totalement sûr de ce que je vais dire, mais un service ne peut pas lancer une application sur un bureau
    windows ne se limite pas à windows home qui déjà permet d'avoir plusieurs sessions ouvertes à un instant T, et sur un windows server un même utilisateur peut ouvrir plusieurs sessions
    donc je ne pense pas qu'on puisse pointer vers un user en particulier, une session peut etre ...

    par contre si tu lances une application à l'ouverture de session (et donc sur toutes les sessions) ton service pourra dialoguer avec et dire à l'applicatio de session de démarrer un processus
    Cours complets, tutos et autres FAQ ici : C# - VB.NET

  3. #3
    Membre confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juin 2005
    Messages
    700
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Juin 2005
    Messages : 700
    Points : 488
    Points
    488
    Par défaut
    en fait, l'event OnSessionChange permet de récuperer l'id de session, et ce qui se passe (lock , unlock, login, logout).

    il y a une autre raison à mon souhait de vouloir lancer l'appli sur le bureau depuis le service : mon appli requiert d'etre admin, donc l'uac s'affiche à son lancement. Les utilisateurs qui le configurent pour se lancer au démarrage se plaignent d'avoir l'uac à chaque démarrage windows. J'epere en le lançant depuis Local System, contourner ce problème.

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552
    Par défaut
    Salut:

    En utilisant CreateProcessAsUser de la lib c++ advapi32.dll, on peut créer directement un processus qui appartient à un utilisateur connecté,
    je crois que c'est ce que tu souhaites faire :

    ProcessUtilities.CreateUIProcessForServiceRunningAsLocalSystem(file, param);

    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
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
     
    using System;
    using System.Reflection;
    using System.Security.Principal;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
     
     
    public class ProcessUtilities
        {
            /*** Imports ***/
            #region Imports
     
            [DllImport( "advapi32.dll", EntryPoint = "AdjustTokenPrivileges", SetLastError = true )]
            public static extern bool AdjustTokenPrivileges( IntPtr in_hToken, [MarshalAs( UnmanagedType.Bool )]bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, UInt32 BufferLength, IntPtr PreviousState, IntPtr ReturnLength );
     
            [DllImport( "advapi32.dll", EntryPoint = "OpenProcessToken", SetLastError = true )]
            public static extern bool OpenProcessToken( IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle );
     
            [DllImport( "advapi32.dll", EntryPoint = "LookupPrivilegeValue", SetLastError = true, CharSet = CharSet.Auto )]
            public static extern bool LookupPrivilegeValue( string lpSystemName, string lpName, out LUID lpLuid );
     
            [DllImport( "userenv.dll", EntryPoint = "CreateEnvironmentBlock", SetLastError = true )]
            public static extern bool CreateEnvironmentBlock( out IntPtr out_ptrEnvironmentBlock, IntPtr in_ptrTokenHandle, bool in_bInheritProcessEnvironment );
     
            [DllImport( "kernel32.dll", EntryPoint = "CloseHandle", SetLastError = true )]
            public static extern bool CloseHandle( IntPtr handle );
     
            [DllImport( "wtsapi32.dll", EntryPoint = "WTSQueryUserToken", SetLastError = true )]
            public static extern bool WTSQueryUserToken( UInt32 in_nSessionID, out IntPtr out_ptrTokenHandle );
     
            [DllImport( "kernel32.dll", EntryPoint = "WTSGetActiveConsoleSessionId", SetLastError = true )]
            public static extern uint WTSGetActiveConsoleSessionId();
     
            [DllImport( "Wtsapi32.dll", EntryPoint = "WTSQuerySessionInformation", SetLastError = true )]
            public static extern bool WTSQuerySessionInformation( IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out IntPtr ppBuffer, out uint pBytesReturned );
     
            [DllImport( "wtsapi32.dll", EntryPoint= "WTSFreeMemory", SetLastError = false )]
            public static extern void WTSFreeMemory( IntPtr memory );
     
            [DllImport( "userenv.dll", EntryPoint = "LoadUserProfile", SetLastError = true )]
            public static extern bool LoadUserProfile( IntPtr hToken, ref PROFILEINFO lpProfileInfo );
     
            [DllImport( "advapi32.dll", EntryPoint = "CreateProcessAsUser", SetLastError = true, CharSet = CharSet.Auto )]
            public static extern bool CreateProcessAsUser( IntPtr in_ptrUserTokenHandle, string in_strApplicationName, string in_strCommandLine, ref SECURITY_ATTRIBUTES in_oProcessAttributes, ref SECURITY_ATTRIBUTES in_oThreadAttributes, bool in_bInheritHandles, CreationFlags in_eCreationFlags, IntPtr in_ptrEnvironmentBlock, string in_strCurrentDirectory, ref STARTUPINFO in_oStartupInfo, ref PROCESS_INFORMATION in_oProcessInformation );
     
            #endregion //Imports
     
            /*** Delegates ***/
     
            /*** Structs ***/
     
            #region Structs
            [StructLayout( LayoutKind.Sequential )]
            public struct LUID
            {
                public uint m_nLowPart;
                public uint m_nHighPart;
            }
     
            [StructLayout( LayoutKind.Sequential )]
            public struct TOKEN_PRIVILEGES
            {
                public int m_nPrivilegeCount;
                public LUID m_oLUID;
                public int m_nAttributes;
            }
     
            [StructLayout( LayoutKind.Sequential )]
            public struct PROFILEINFO
            {
                public int dwSize;
                public int dwFlags;
     
                [MarshalAs( UnmanagedType.LPTStr )]
                public String lpUserName;
                [MarshalAs( UnmanagedType.LPTStr )]
                public String lpProfilePath;
                [MarshalAs( UnmanagedType.LPTStr )]
                public String lpDefaultPath;
                [MarshalAs( UnmanagedType.LPTStr )]
                public String lpServerName;
                [MarshalAs( UnmanagedType.LPTStr )]
                public String lpPolicyPath;
                public IntPtr hProfile;
            }
     
     
     
            [StructLayout( LayoutKind.Sequential )]
            public struct STARTUPINFO
            {
                public Int32 cb;
     
                public string lpReserved;
     
                public string lpDesktop;
     
                public string lpTitle;
     
                public Int32 dwX;
     
                public Int32 dwY;
     
                public Int32 dwXSize;
     
                public Int32 dwXCountChars;
     
                public Int32 dwYCountChars;
     
                public Int32 dwFillAttribute;
     
                public Int32 dwFlags;
     
                public Int16 wShowWindow;
     
                public Int16 cbReserved2;
     
                public IntPtr lpReserved2;
     
                public IntPtr hStdInput;
     
                public IntPtr hStdOutput;
     
                public IntPtr hStdError;
            }
     
     
     
            [StructLayout( LayoutKind.Sequential )]
            public struct PROCESS_INFORMATION
            {
     
                public IntPtr hProcess;
     
                public IntPtr hThread;
     
                public Int32 dwProcessID;
     
                public Int32 dwThreadID;
     
            }
     
            [StructLayout( LayoutKind.Sequential )]
            public struct SECURITY_ATTRIBUTES
            {
     
                public Int32 Length;
     
                public IntPtr lpSecurityDescriptor;
     
                public bool bInheritHandle;
     
            }
     
            #endregion //Structs
     
     
     
            /*** Classes ***/
     
     
     
            /*** Enums ***/
     
            #region Enums
     
     
            public enum CreationFlags
            {
     
                CREATE_SUSPENDED = 0x00000004,
     
                CREATE_NEW_CONSOLE = 0x00000010,
     
                CREATE_NEW_PROCESS_GROUP = 0x00000200,
     
                CREATE_UNICODE_ENVIRONMENT = 0x00000400,
     
                CREATE_SEPARATE_WOW_VDM = 0x00000800,
     
                CREATE_DEFAULT_ERROR_MODE = 0x04000000,
     
            }
     
     
     
            public enum WTS_INFO_CLASS
            {
     
                WTSInitialProgram,
     
                WTSApplicationName,
     
                WTSWorkingDirectory,
     
                WTSOEMId,
     
                WTSSessionId,
     
                WTSUserName,
     
                WTSWinStationName,
     
                WTSDomainName,
     
                WTSConnectState,
     
                WTSClientBuildNumber,
     
                WTSClientName,
     
                WTSClientDirectory,
     
                WTSClientProductId,
     
                WTSClientHardwareId,
     
                WTSClientAddress,
     
                WTSClientDisplay,
     
                WTSClientProtocolType
            }
     
     
     
            #endregion //Enums
     
     
     
            /*** Defines ***/
     
            #region Defines
     
     
     
            private const int TOKEN_QUERY = 0x08;
     
            private const int TOKEN_ADJUST_PRIVILEGES = 0x20;
     
            private const int SE_PRIVILEGE_ENABLED = 0x02;
     
     
     
            public const int ERROR_NO_TOKEN                    = 1008;
     
            public const int RPC_S_INVALID_BINDING            = 1702;
     
     
     
            #endregion //Defines
     
     
     
            /*** Methods ***/
     
            #region Methods
     
     
     
            /*
     
                If you need to give yourself permissions to inspect processes for their modules,
     
                and create tokens without worrying about what account you're running under,
     
                this is the method for you :) (such as the token privilege "SeDebugPrivilege")
     
            */
     
            static public bool AdjustProcessTokenPrivileges( IntPtr in_ptrProcessHandle, string in_strTokenToEnable )
            {
     
                IntPtr l_hProcess = IntPtr.Zero;
     
                IntPtr l_hToken = IntPtr.Zero;
     
                LUID l_oRestoreLUID;
     
                TOKEN_PRIVILEGES l_oTokenPrivileges;
     
     
     
                Debug.Assert( in_ptrProcessHandle != IntPtr.Zero );
     
     
     
                //Get the process security token
     
                if( false == OpenProcessToken( in_ptrProcessHandle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out l_hToken ) )
                    return false;
     
     
     
     
                //Lookup the LUID for the privilege we need
     
                if (false == LookupPrivilegeValue(String.Empty, in_strTokenToEnable, out l_oRestoreLUID))
                    return false;
     
     
     
                //Adjust the privileges of the current process to include the new privilege
     
                l_oTokenPrivileges.m_nPrivilegeCount = 1;
     
                l_oTokenPrivileges.m_oLUID = l_oRestoreLUID;
     
                l_oTokenPrivileges.m_nAttributes = SE_PRIVILEGE_ENABLED;
     
     
     
                if( false == AdjustTokenPrivileges( l_hToken, false, ref l_oTokenPrivileges, 0, IntPtr.Zero, IntPtr.Zero ) )
                    return false;
     
     
     
                return true;
     
            }
     
     
     
            /*
     
                Start a process the simplest way you can imagine
     
            */
     
            static public int SimpleProcessStart( string in_strTarget, string in_strArguments )
            {
                Process l_oProcess = new Process();
     
                Debug.Assert(l_oProcess != null);
     
     
     
                l_oProcess.StartInfo.FileName = in_strTarget;
     
                l_oProcess.StartInfo.Arguments = in_strArguments;
     
     
     
                if (true == l_oProcess.Start())
                {
     
                    return l_oProcess.Id;
     
                }
                return -1;
            }
     
     
     
            /*
     
                All the magic is in the call to WTSQueryUserToken, it saves you changing DACLs,
     
                process tokens, pulling the SID, manipulating the Windows Station and Desktop
     
                (and its DACLs) - if you don't know what those things are, you're lucky and should
     
                be on your knees thanking God at this moment.
     
     
     
                DEV NOTE:  This method currently ASSumes that it should impersonate the user
     
                                  who is logged into session 1 (if more than one user is logged in, each
     
                                  user will have a session of their own which means that if user switching
     
                                  is going on, this method could start a process whose UI shows up in
     
                                  the session of the user who is not actually using the machine at this
     
                                  moment.)
     
     
     
                DEV NOTE 2:    If the process being started is a binary which decides, based upon
     
                                    the user whose session it is being created in, to relaunch with a
     
                                    different integrity level (such as Internet Explorer), the process
     
                                    id will change immediately and the Process Manager will think
     
                                    that the process has died (because in actuality the process it
     
                                    launched DID in fact die only that it was due to self-termination)
     
                                    This means beware of using this service to startup such applications
     
                                    although it can connect to them to alarm in case of failure, just
     
                                    make sure you don't configure it to restart it or you'll get non
     
                                    stop process creation ;)
     
            */
     
            static public int CreateUIProcessForServiceRunningAsLocalSystem(string in_strTarget, string in_strArguments )
            {
                PROCESS_INFORMATION l_oProcessInformation = new PROCESS_INFORMATION();
     
                SECURITY_ATTRIBUTES l_oSecurityAttributes = new SECURITY_ATTRIBUTES();
     
                STARTUPINFO l_oStartupInfo = new STARTUPINFO();
     
                PROFILEINFO l_oProfileInfo = new PROFILEINFO();
     
                IntPtr l_ptrUserToken = new IntPtr( 0 );
     
                uint l_nActiveUserSessionId = 0xFFFFFFFF;
     
                string l_strActiveUserName = "";
     
                int l_nProcessID = -1;
     
                IntPtr l_ptrBuffer = IntPtr.Zero;
     
                uint l_nBytes = 0;
     
     
                try
                {
                    //The currently active user is running what session?
                    l_nActiveUserSessionId = WTSGetActiveConsoleSessionId();
     
                    if( l_nActiveUserSessionId == 0xFFFFFFFF )
                    {
                        throw new Exception( "ProcessUtilities" + "->" + MethodInfo.GetCurrentMethod().Name + "->" + "The call to WTSGetActiveConsoleSessionId failed,  GetLastError returns: " + Marshal.GetLastWin32Error().ToString() );
                    }
     
                    if( false == WTSQuerySessionInformation( IntPtr.Zero, (int)l_nActiveUserSessionId, WTS_INFO_CLASS.WTSUserName, out l_ptrBuffer, out l_nBytes ) )
                    {
                        int l_nLastError = Marshal.GetLastWin32Error();
     
                        //On earlier operating systems from Vista, when no one is logged in, you get RPC_S_INVALID_BINDING which is ok, we just won't impersonate
     
                        if( l_nLastError != RPC_S_INVALID_BINDING )
                        {
                            throw new Exception( "ProcessUtilities" + "->" + MethodInfo.GetCurrentMethod().Name + "->" + "The call to WTSQuerySessionInformation failed,  GetLastError returns: " + Marshal.GetLastWin32Error().ToString() );
                        }
     
                        //No one logged in so let's just do this the simple way
     
                        return SimpleProcessStart( in_strTarget, in_strArguments );
                    }
     
     
     
                    l_strActiveUserName = Marshal.PtrToStringAnsi( l_ptrBuffer );
     
                    WTSFreeMemory( l_ptrBuffer );
     
     
                    //We are supposedly running as a service so we're going to be running in session 0 so get a user token from the active user session
     
                    if( false == WTSQueryUserToken( (uint)l_nActiveUserSessionId, out l_ptrUserToken ) )                
                    {
     
                        int l_nLastError = Marshal.GetLastWin32Error();
     
                        //Remember, sometimes nobody is logged in (especially when we're set to Automatically startup) you should get error code 1008 (no user token available)
     
                        if( ERROR_NO_TOKEN != l_nLastError )
                        {
                            //Ensure we're running under the local system account
     
                            WindowsIdentity l_oIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();
     
                            if( "NT AUTHORITY\\SYSTEM" != l_oIdentity.Name )
                            {
                                throw new Exception( "ProcessUtilities" + "->" + MethodInfo.GetCurrentMethod().Name + "->" + "The call to WTSQueryUserToken failed and querying the process' account identity results in an identity which does not match 'NT AUTHORITY\\SYSTEM' but instead returns the name:" + l_oIdentity.Name + "  GetLastError returns: " + l_nLastError.ToString() );
                            }
     
                            throw new Exception( "ProcessUtilities" + "->" + MethodInfo.GetCurrentMethod().Name + "->" + "The call to WTSQueryUserToken failed, GetLastError returns: " + l_nLastError.ToString() );
                        }
     
                        //No one logged in so let's just do this the simple way
                        return SimpleProcessStart( in_strTarget, in_strArguments );
                    }
     
                    //Create an appropriate environment block for this user token (if we have one)
     
                    IntPtr l_ptrEnvironment = IntPtr.Zero;
                    Debug.Assert( l_ptrUserToken != IntPtr.Zero );
     
                    if( false == CreateEnvironmentBlock( out l_ptrEnvironment, l_ptrUserToken, false ) )
                    {
                        throw new Exception( "ProcessUtilities" + "->" + MethodInfo.GetCurrentMethod().Name + "->" + "The call to CreateEnvironmentBlock failed, GetLastError returns: " + Marshal.GetLastWin32Error().ToString() );
                    }
     
                    l_oSecurityAttributes.Length = Marshal.SizeOf( l_oSecurityAttributes );
     
                    l_oStartupInfo.cb = Marshal.SizeOf( l_oStartupInfo );
     
                    //DO NOT set this to "winsta0\\default" (even though many online resources say to do so)
     
                    l_oStartupInfo.lpDesktop = String.Empty;
     
                    l_oProfileInfo.dwSize = Marshal.SizeOf( l_oProfileInfo );
     
                    l_oProfileInfo.lpUserName = l_strActiveUserName;
     
     
                    //Remember, sometimes nobody is logged in (especially when we're set to Automatically startup)
                    if( false == LoadUserProfile( l_ptrUserToken, ref l_oProfileInfo ) )
                    {
                        throw new Exception( "ProcessUtilities" + "->" + MethodInfo.GetCurrentMethod().Name + "->" + "The call to LoadUserProfile failed, GetLastError returns: " + Marshal.GetLastWin32Error().ToString() );
                    }
     
     
                    if (false == CreateProcessAsUser(l_ptrUserToken, in_strTarget, in_strTarget + " " + in_strArguments, ref l_oSecurityAttributes, ref l_oSecurityAttributes, false, CreationFlags.CREATE_UNICODE_ENVIRONMENT, l_ptrEnvironment, null, ref l_oStartupInfo, ref l_oProcessInformation))
                    {
                        //System.Diagnostics.EventLog.WriteEntry( "CreateProcessAsUser FAILED", Marshal.GetLastWin32Error().ToString() );
                        throw new Exception( "ProcessUtilities" + "->" + MethodInfo.GetCurrentMethod().Name + "->" + "The call to CreateProcessAsUser failed, GetLastError returns: " + Marshal.GetLastWin32Error().ToString() );
                    }
                    l_nProcessID = l_oProcessInformation.dwProcessID;
     
                }
     
                catch( Exception l_oException )
                {
                    throw new Exception( "ProcessUtilities" + "->" + MethodInfo.GetCurrentMethod().Name + "->" + "An unhandled exception was caught spawning the process, the exception was: " + l_oException.Message );
                }
     
                finally
                {
                    if( l_oProcessInformation.hProcess != IntPtr.Zero )
                    {
                        CloseHandle( l_oProcessInformation.hProcess );
                    }
     
                    if( l_oProcessInformation.hThread != IntPtr.Zero )
                    {
                        CloseHandle( l_oProcessInformation.hThread );
                    }
                }
                return l_nProcessID;
     
            }
     
            #endregion //Methods
        }


    Sinon j'ai pas trop compris ta problématique, mais y a une solution plus standard (genre adobe, apple, google updater... le font je pense) c'est d'avoir un client sur chaque session qui communique avec un Service central avec de L'IPC...
    voilou
    J’espère que ça t'a aidé...

  5. #5
    Membre confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juin 2005
    Messages
    700
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Juin 2005
    Messages : 700
    Points : 488
    Points
    488
    Par défaut
    merci de ta réponse, je vais regarder ça.

    Avoir une UI pour mon service ne me pose pas de problème, en effet je ferai une appli qui fera guise d'UI et communiquera avec le service via IPC.

    J'ai d'ailleur lu qu'il ne fallait surtout pas créer de fenetre dans un service car ca ouvre une faille de sécurité importante (on peut envoyer des messages windows au "compte" Local system ... si j'ai bien compris).

    Non mon soucis c'est de lancer l'execution d'un logiciel windows form depuis un service, vers un bureau, celui de la session actuellement ouverte.

    On dirait que ton code va m'aider, je vous dirai ça.

    PS : aurais tu l'url de la page où tu as vu ce code?

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2007
    Messages
    1 002
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2007
    Messages : 1 002
    Points : 552
    Points
    552
    Par défaut
    giova_fr
    je ne me rappel pas de l url,
    d'ailleurs je pense que j'ai du faire certaine modif,
    le code que je t'ai donné fonctionne, je l'ai testé
    c'est deja pas mal

    j'ai essayé de googler 2s mais j'ai pas trouvé...
    dsl


    EDIT:
    http://benoman.com/?p=50

    Ce lien semble etre la source !!!

    Aiiie:
    It’s as simple as that. This worked fine in Windows 7, but I couldn’t seem to make it happen on Windows XP for some reason. Oh well. Better luck to you.
    J'ai pas testé sur XP
    Je ne sais pas ce qu'en pense les autres, Mais je pense que c'est une erreur de conception de vouloir faire ça... donc tâte toi bien avant

  7. #7
    Membre du Club
    Homme Profil pro
    Collégien
    Inscrit en
    Septembre 2011
    Messages
    113
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Collégien

    Informations forums :
    Inscription : Septembre 2011
    Messages : 113
    Points : 44
    Points
    44
    Par défaut
    Bonjour.


    Je ne peux vous apportez la solution maintenant, je ne suis pas sur mon ordinateur, et je ne connais pas le code source par coeur.
    Cependant, il n'est pas très compliqué de retrouvé le nom d'utilisateur(pour ensuite aller dans : nomuser/bureau/application.exe)
    Je donne suite à ceci dès que je suis sur mon ordinateur, mais j'ai trouvé ce code source dans la FAQ C# ou dans les Sources C# de Developpez.

Discussions similaires

  1. Lancer une appli sur serveur distant
    Par sixshot69 dans le forum Web & réseau
    Réponses: 5
    Dernier message: 31/03/2011, 10h16
  2. Réponses: 0
    Dernier message: 27/02/2011, 17h30
  3. Réponses: 1
    Dernier message: 09/04/2009, 09h25
  4. impossible à lancer une appli sur click
    Par grimberman dans le forum Général JavaScript
    Réponses: 0
    Dernier message: 29/05/2008, 15h02
  5. Comment lancer une appli sur une machine distante
    Par J.Michel dans le forum VB 6 et antérieur
    Réponses: 6
    Dernier message: 13/09/2007, 13h00

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