473,396 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Windows services failed on windows 7 when remote desktop

Dear all,

I have the windows service program running on windows 7. When windows start and login, this windows service will start. But when log off and remote desktop connection and log in, the windows service does not start. Do you experience this issues?? Do you know how to solve??

Windows service does really failed to load when doing remote desktop. It has below error in event viewer.
It show error event id: 5000, source: .Net Runtime 2.0 Error in event viewer log

Description:

EventType clr20r3. P1 Monitor.exe. P2 1.0.0.0.P3 4dd1eec3, P4 system, P5 2.0.0.0, P6 471ebf0d, P7 3a23, P8 22, P9 system.componentmodel.win32, P10 NIL

It seems to be access denied. Are there any difference between normal login and remote desktop login?? Do you have any ideas to solve??

I have below code in starting the exe file.

//CODING
Expand|Select|Wrap|Line Numbers
  1. private Process m_Process;
  2.  
  3. public void runMonitor()
  4. {
  5.  
  6. ApplicationLoader.PROCESS_INFORMATION procInfo;
  7. ApplicationLoader.StartProcessAndBypassUAC("Monitor.exe", out procInfo);
  8.  
  9.  
  10. this.m_Process = Process.GetProcessById((int)procInfo.dwProcessId);
  11.  
  12.  
  13. ......................
  14.  
  15. }
  16.  
//ApplicationLoader file
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Security;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace StoreService
  7. {
  8.     /// <summary>
  9.     /// Class that allows running applications with full admin rights. In
  10.     /// addition the application launched will bypass the Vista UAC prompt.
  11.     /// </summary>
  12.     public class ApplicationLoader
  13.     {
  14.         #region Structures
  15.  
  16.         [StructLayout(LayoutKind.Sequential)]
  17.         public struct SECURITY_ATTRIBUTES
  18.         {
  19.             public int Length;
  20.             public IntPtr lpSecurityDescriptor;
  21.             public bool bInheritHandle;
  22.         }
  23.  
  24.         [StructLayout(LayoutKind.Sequential)]
  25.         public struct STARTUPINFO
  26.         {
  27.             public int cb;
  28.             public String lpReserved;
  29.             public String lpDesktop;
  30.             public String lpTitle;
  31.             public uint dwX;
  32.             public uint dwY;
  33.             public uint dwXSize;
  34.             public uint dwYSize;
  35.             public uint dwXCountChars;
  36.             public uint dwYCountChars;
  37.             public uint dwFillAttribute;
  38.             public uint dwFlags;
  39.             public short wShowWindow;
  40.             public short cbReserved2;
  41.             public IntPtr lpReserved2;
  42.             public IntPtr hStdInput;
  43.             public IntPtr hStdOutput;
  44.             public IntPtr hStdError;
  45.         }
  46.  
  47.         [StructLayout(LayoutKind.Sequential)]
  48.         public struct PROCESS_INFORMATION
  49.         {
  50.             public IntPtr hProcess;
  51.             public IntPtr hThread;
  52.             public uint dwProcessId;
  53.             public uint dwThreadId;
  54.         }
  55.  
  56.         #endregion
  57.  
  58.         #region Enumerations
  59.  
  60.         enum TOKEN_TYPE : int
  61.         {
  62.             TokenPrimary = 1,
  63.             TokenImpersonation = 2
  64.         }
  65.  
  66.         enum SECURITY_IMPERSONATION_LEVEL : int
  67.         {
  68.             SecurityAnonymous = 0,
  69.             SecurityIdentification = 1,
  70.             SecurityImpersonation = 2,
  71.             SecurityDelegation = 3,
  72.         }
  73.  
  74.         #endregion
  75.  
  76.         #region Constants
  77.  
  78.         public const int TOKEN_DUPLICATE = 0x0002;
  79.         public const uint MAXIMUM_ALLOWED = 0x2000000;
  80.         public const int CREATE_NEW_CONSOLE = 0x00000010;
  81.  
  82.         public const int IDLE_PRIORITY_CLASS = 0x40;
  83.         public const int NORMAL_PRIORITY_CLASS = 0x20;
  84.         public const int HIGH_PRIORITY_CLASS = 0x80;
  85.         public const int REALTIME_PRIORITY_CLASS = 0x100;
  86.  
  87.         #endregion
  88.  
  89.         #region Win32 API Imports
  90.  
  91.         [DllImport("kernel32.dll", SetLastError = true)]
  92.         private static extern bool CloseHandle(IntPtr hSnapshot);
  93.  
  94.         [DllImport("kernel32.dll")]
  95.         static extern uint WTSGetActiveConsoleSessionId();
  96.  
  97.         [DllImport("advapi32.dll", EntryPoint = "CreateProcessAsUser", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
  98.         public extern static bool CreateProcessAsUser(IntPtr hToken, String lpApplicationName, String lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes,
  99.             ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandle, int dwCreationFlags, IntPtr lpEnvironment,
  100.             String lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
  101.  
  102.         [DllImport("kernel32.dll")]
  103.         static extern bool ProcessIdToSessionId(uint dwProcessId, ref uint pSessionId);
  104.  
  105.         [DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx")]
  106.         public extern static bool DuplicateTokenEx(IntPtr ExistingTokenHandle, uint dwDesiredAccess,
  107.             ref SECURITY_ATTRIBUTES lpThreadAttributes, int TokenType,
  108.             int ImpersonationLevel, ref IntPtr DuplicateTokenHandle);
  109.  
  110.         [DllImport("kernel32.dll")]
  111.         static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
  112.  
  113.         [DllImport("advapi32", SetLastError = true), SuppressUnmanagedCodeSecurityAttribute]
  114.         static extern bool OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, ref IntPtr TokenHandle);
  115.  
  116.         #endregion
  117.  
  118.         /// <summary>
  119.         /// Launches the given application with full admin rights, and in addition bypasses the Vista UAC prompt
  120.         /// </summary>
  121.         /// <param name="applicationName">The name of the application to launch</param>
  122.         /// <param name="procInfo">Process information regarding the launched application that gets returned to the caller</param>
  123.         /// <returns></returns>
  124.         public static bool StartProcessAndBypassUAC(String applicationName, out PROCESS_INFORMATION procInfo)
  125.         {
  126.             uint winlogonPid = 0;
  127.             IntPtr hUserTokenDup = IntPtr.Zero, hPToken = IntPtr.Zero, hProcess = IntPtr.Zero;
  128.             procInfo = new PROCESS_INFORMATION();
  129.  
  130.             // obtain the currently active session id; every logged on user in the system has a unique session id
  131.             uint dwSessionId = WTSGetActiveConsoleSessionId();
  132.  
  133.             // obtain the process id of the winlogon process that is running within the currently active session
  134.             Process[] processes = Process.GetProcessesByName("winlogon");
  135.             foreach (Process p in processes)
  136.             {
  137.                 if ((uint)p.SessionId == dwSessionId)
  138.                 {
  139.                     winlogonPid = (uint)p.Id;
  140.                 }
  141.             }
  142.  
  143.             // obtain a handle to the winlogon process
  144.             hProcess = OpenProcess(MAXIMUM_ALLOWED, false, winlogonPid);
  145.  
  146.             // obtain a handle to the access token of the winlogon process
  147.             if (!OpenProcessToken(hProcess, TOKEN_DUPLICATE, ref hPToken))
  148.             {
  149.                 CloseHandle(hProcess);
  150.                 return false;
  151.             }
  152.  
  153.             // Security attibute structure used in DuplicateTokenEx and CreateProcessAsUser
  154.             // I would prefer to not have to use a security attribute variable and to just 
  155.             // simply pass null and inherit (by default) the security attributes
  156.             // of the existing token. However, in C# structures are value types and therefore
  157.             // cannot be assigned the null value.
  158.             SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
  159.             sa.Length = Marshal.SizeOf(sa);
  160.  
  161.             // copy the access token of the winlogon process; the newly created token will be a primary token
  162.             if (!DuplicateTokenEx(hPToken, MAXIMUM_ALLOWED, ref sa, (int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification, (int)TOKEN_TYPE.TokenPrimary, ref hUserTokenDup))
  163.             {
  164.                 CloseHandle(hProcess);
  165.                 CloseHandle(hPToken);
  166.                 return false;
  167.             }
  168.  
  169.             // By default CreateProcessAsUser creates a process on a non-interactive window station, meaning
  170.             // the window station has a desktop that is invisible and the process is incapable of receiving
  171.             // user input. To remedy this we set the lpDesktop parameter to indicate we want to enable user 
  172.             // interaction with the new process.
  173.             STARTUPINFO si = new STARTUPINFO();
  174.             si.cb = (int)Marshal.SizeOf(si);
  175.             si.lpDesktop = @"winsta0\default"; // interactive window station parameter; basically this indicates that the process created can display a GUI on the desktop
  176.  
  177.             // flags that specify the priority and creation method of the process
  178.             int dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;
  179.  
  180.             // create a new process in the current user's logon session
  181.             bool result = CreateProcessAsUser(hUserTokenDup,        // client's access token
  182.                                             null,                   // file to execute
  183.                                             applicationName,        // command line
  184.                                             ref sa,                 // pointer to process SECURITY_ATTRIBUTES
  185.                                             ref sa,                 // pointer to thread SECURITY_ATTRIBUTES
  186.                                             false,                  // handles are not inheritable
  187.                                             dwCreationFlags,        // creation flags
  188.                                             IntPtr.Zero,            // pointer to new environment block 
  189.                                             null,                   // name of current directory 
  190.                                             ref si,                 // pointer to STARTUPINFO structure
  191.                                             out procInfo            // receives information about new process
  192.                                             );
  193.  
  194.             // invalidate the handles
  195.             CloseHandle(hProcess);
  196.             CloseHandle(hPToken);
  197.             CloseHandle(hUserTokenDup);
  198.  
  199.             return result; // return the result
  200.         }
  201.  
  202.     }
  203. }
Jul 17 '11 #1
0 1444

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andrei | last post by:
Hello, I posted yesterday the same problem, but now I want to be more specific about it. My intention is to create a windows service to process some Word documents. If I use CreateObject to...
2
by: James | last post by:
Dear Access Guru's, Hopefully you can help me (as Microsoft don't seem to be able to) We have an Access database on a Windows 2003 server with 5 CALS in our office. Access is loaded onto...
3
by: Elp | last post by:
Hi, I have developped a Windows applcation (in C#) that allows, among other things, users to view and control the desktop of a remote Windows XP Pro machine. I have actually simply embedded the...
1
by: anonieko | last post by:
Problem When Remote Desktop to my main office machine, and when trying to connect to SQL Express, I was becoming increasingly frustrated with the following error: Error: 'Failed to generate a...
5
by: Larry | last post by:
Hi, I am a newcomer to VC++ (specifically programming on Microsoft platforms); and I have a very basic question about windows services. Using Windows Services mechanism is it possible to...
15
by: =?Utf-8?B?TVNU?= | last post by:
To demonstrate my problem, I have a very simple VB Windows application. It has a text box that is used to display a counter, a button to reset the counter, and a timer that increments the counter...
15
by: =?Utf-8?B?RWxpb3Ro?= | last post by:
I try to install Windows Services but it show this error during the installation process, "Insufficient System resources exist to complete the requested service." I created this Services in VB...
0
balabaster
by: balabaster | last post by:
Hi, I know this question has been answered before (somewhat) but I have a twist... I've written a client/server application where the server portion runs as a windows service and allows...
6
by: Bob Altman | last post by:
Hi all, This is a long shot, but I figure it doesn't hurt to ask... I have some applications that work fine when I run them on my PC at work, but "fail to initialize (0x000005)" when I'm...
1
by: Keland | last post by:
I am useing XP pro. Since installing dotnet 3.5 and SP3 I get a error message when I try to connect to my office useing remote desktop. The e/m is Terminal Services Active X control...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.