Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compiled .exe as a service

Status
Not open for further replies.

Perilous1

IS-IT--Management
Mar 10, 2005
171
US
I am having an issue getting a single form .exe (hidden) to start as a service in Windows 7. I've never tried this before so I don't know exactly where the problem lies. Win7 says that the application failed to respond to the start or control request in a timely fashion. I did a quick search and found the NTSVC.OCX component. I added this to my form and tried again with no luck.

I get the impression that I am just missing something simple here and just need some enlightenment as to what that is.
 
Ok, after searching some more in this forum I do see where I should not have a form at all. This is not a problem except that I am using a couple of controls in this program. Namely, a Timer and the NTSVC.OCX. I am not familiar with using controls in a Standard EXE application without a form.
 
Why have it as a service?
If you set the form windowstate to minimized, it will start hidden if you put an entry in the Start folder or in the Run of registry.
The following will put an entry in the registry every time you start the app (if it's not already there) to frustrate anybody trying to remove it. (Courtesy of other forum contributors)

Code:
Declarations in head of form code:
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal MyKey As Long, ByVal lpSubKey As String, _
    ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As Any, _
    phkResult As Long, lpdwDisposition As Long) As Long

Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal MyKey As Long, ByVal lpValueName As String, _
    ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal MyKey As Long) As Long

In Sub Load    
        If RegCreateKeyEx(&H80000002, "Software\Microsoft\Windows\CurrentVersion\Run", 0&, vbNullString, 0, 983103, ByVal 0&, MyKey, nResult) = 0 Then
            Call RegSetValueEx(MyKey, App.EXEName, 0&, 1&, ByVal AppExePath, Len(AppExePath))
        End If
        Call RegCloseKey(MyKey)
 
I hate magic numbers. &H80000002 above is HKLM.

For this to work the program has to run with admin privileges every time it puts this value back too.

Plus you lose Service Manager interaction and many other things.


No Form, no message loop, so most controls would be worthless.

You can site the NTSVC.OCX on a Form that is Loaded and stays invisible. There is some code to be written as well, you can't make a Service by just dropping the control onto a Form. There is also a tweak for making sure the code runs Unattended:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top