Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: x64core en 13 Agosto 2011, 21:23 pm



Título: duda acerca de los servicios :P
Publicado por: x64core en 13 Agosto 2011, 21:23 pm
buenas cree este tema relacionado a este :P

http://foro.elhacker.net/analisis_y_diseno_de_malware/manejar_sevicios_desde_vb-t123518.0.html

tengo una duda de como crear un servicio e visto que estan las funciones pero creo que me estoy equivocando en algo :P o no funciona en win 7 ???

todo el codigo de vb lo agrego a un modulo y despues llamo a la funcion "main" luego en esa funcion agrego la funcion "ServiceInstall" que creo que en los paremtros me quivoco noce :P yo escribo el nombre de la PC o es el nombre del usuario del que se esta ocupando? luego el segundo parametro es el nombre del servicio no :P y el tercero la ruta del EXE yo escribo la ruta del notepad :P
bueno si hay alguien que enseñe como funciona el codigo gracias  ;D


Título: Re: duda acerca de los servicios :P
Publicado por: ThunderCls en 15 Agosto 2011, 17:47 pm
En que te falla y que error te devuelve?
El prototipo de la funcion es:

Código
  1. ServiceInstall(ComputerName As String, ServiceName As String, Path As String)

El primer parametro es el nombre de la PC (Segun la MSDN - http://msdn.microsoft.com/en-us/library/ms684323(v=vs.85).aspx):
The name of the target computer. If the pointer is NULL or points to an empty string, the function connects to the service control manager on the local computer.

El segundo y el tercer parametro estan relacionados con "CreateService" (http://msdn.microsoft.com/en-us/library/ms682450(v=vs.85).aspx). Por lo que estuve viendo en la implementacion de la funcion, se utiliza el mismo parametro de funcion "ServiceName" para los parametros de API "lpServiceName" y "lpDisplayName" que no necesariamente tienen que ser iguales...bueno, es solo una aclaracion.
Saludos


Título: Re: duda acerca de los servicios :P
Publicado por: x64core en 15 Agosto 2011, 19:14 pm
pues yo queria saber como usar las funciones osea como instalar digamos el notepad como servicio :P
yo e investigado un poco pero no logro hacerlo :/


(e hecho una copia del notepad al C )
Código
  1. Sub Main()
  2. Dim ComputerN As String
  3. Dim LenC As Long
  4.  
  5. ComputerN = Space(MAX_COMPUTERNAME_LENGTH)
  6. LenC = MAX_COMPUTERNAME_LENGTH + 1
  7. GetComputerName ComputerN, LenC
  8. ComputerN = Left$(ComputerN, LenC)
  9.  
  10. ServiceInstall ComputerN, "NOTEPAD", "C:\NOTEPAD.exe"
  11.  
  12.    'ServiceStop "", "ipodservice"
  13.    'ServiceStart "", "ipodservice"
  14.    'ServicePause "", "ipodservice"
  15.    'ServiceInstall "", "ipodservice",
  16.    'ServiceUnInstall "", "ipodservice"
  17.    'MsgBox ServiceStatus("", "ipodservice")
  18. End Sub

y cuando llamo a la funcion de instalar el servicio no me instala nada :/ yo uso window 7 noce si eso puede afectar aunq en la msdn dice que desde 98 creo :P en adelanet :P es mas e puesto esta linea despues de la llamada a la API createservice

Código
  1. If Err.LastDllError <> 0 Then Debug.Print Error(Err.LastDllError)

para saber cual es el error y me devuelve esto:

"Error definido por la aplicación o el objeto"

:P


Título: Re: duda acerca de los servicios :P
Publicado por: x64core en 2 Septiembre 2011, 05:24 am
:P sale buenas e logrado instalar un servicio :P
con este codigo:


Código
  1. Public Function Service_Install(SVC_NAME As String, SVC_DESC As String) As Boolean
  2.    Dim lHManager           As Long
  3.    Dim lHService           As Long
  4.    Dim lResult             As Long
  5.    Dim tStatus             As SERVICE_STATUS
  6.    Dim sSvcPath            As String
  7.    Dim sAccount            As String
  8.  
  9. On Error GoTo Handler
  10.    sSvcPath = App.Path + "\" & SVC_NAME
  11.    sAccount = "LocalSystem"
  12.    lHManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_CREATE_SERVICE)
  13.    lResult = CreateService(lHManager, SVC_NAME, SVC_DESC, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, sSvcPath, vbNullString, vbNullString, vbNullString, sAccount, vbNullString)
  14.    If Not lResult = 0 Then
  15.        Service_Install = True
  16.    Else
  17.        GoTo Handler
  18.    End If
  19.    CloseServiceHandle lHManager
  20. On Error GoTo 0
  21. Exit Function
  22.  
  23. Handler:
  24.    If Not lHManager = 0 Then CloseServiceHandle lHManager
  25. End Function
  26.  
  27.  
  28. Public Function Service_StartUp(SVC_NAME As String, svcStartType As eServiceStartType) As Boolean
  29.    Dim lHManager           As Long
  30.    Dim lHService           As Long
  31.    Dim lResult             As Long
  32.  
  33. On Error GoTo Handler
  34.    lHManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_CONNECT)
  35.    lHService = OpenService(lHManager, SVC_NAME, SERVICE_CHANGE_CONFIG)
  36.    lResult = ChangeServiceConfig(lHService, SERVICE_NO_CHANGE, svcStartType, SERVICE_NO_CHANGE, vbNullString, vbNullString, 0&, vbNullString, vbNullString, vbNullString, vbNullString)
  37.    If Not lResult = 0 Then Service_StartUp = True
  38.    CloseServiceHandle lHService
  39.    CloseServiceHandle lHManager
  40. On Error GoTo 0
  41. Exit Function
  42.  
  43. Handler:
  44.    If Not lHService = 0 Then CloseServiceHandle lHService
  45.    If Not lHManager = 0 Then CloseServiceHandle lHManager
  46. End Function
  47.  

y lo utilizo asi:


Código
  1. If Service_Install(App.EXEName & ".exe", "EJEMPLO SERVICIO") Then
  2.    Debug.Print Service_StartUp(App.EXEName & ".exe", START_AUTO)
  3. Else
  4.    Debug.Print "SVC NOT"
  5. End If
  6.  

compilo el proyecto y lo ejecuto, me instala el servicio el proceso de programa aparece como un proceso normal en el admin de tareas, en la pestaña de servicios lo encuentro instalado:

(http://img691.imageshack.us/img691/3019/75453454.png)
pero al parecer no esta iniciado ( claro no tiene la funcion para ser iniciado ) luego lo inicio manualmente y me dice esto:

(http://img9.imageshack.us/img9/2237/83443786.png)
y luego apago y vuelvo a encender y no esta iniciado el servicio
alguien me puede ayudar? :( :P


 pero ahora el problema es que no me inicia el servicio :P


Título: Re: duda acerca de los servicios :P
Publicado por: Elemental Code en 2 Septiembre 2011, 22:30 pm
que programa estas poniendo como servicio.?
Cualquier programa se puede poner como servicio?


Título: Re: duda acerca de los servicios :P
Publicado por: raul338 en 3 Septiembre 2011, 01:07 am
Cualquier programa se puede, pero al iniciarlo el programa debe "terminar" la llamada devolviendo un código antes de un tiempo determinado, como hacerlo, no sé :xD


Título: Re: duda acerca de los servicios :P
Publicado por: x64core en 3 Septiembre 2011, 02:24 am
que programa estas poniendo como servicio.?
Cualquier programa se puede poner como servicio?

bueno yo hice un programa sin interfas ( form ) porque lei en algun sitio no recuerdo bien que los servicios no deben de tener GUI :P entonces hice un medio hook para detectar los pendrive :P y me pasa ese problema :P

Cualquier programa se puede, pero al iniciarlo el programa debe "terminar" la llamada devolviendo un código antes de un tiempo determinado, como hacerlo, no sé :xD

pues ahora que lo dices raul338 cuando inicio el servicio instalado con codigo el programa creado para iniciar el servicio se cuelga :P
debe ser eso que dices :P y luego hice un programa normal que crea 100 archivos de texto en una X carpeta ( todo eso esta en el sub main del modulo porque no les agrego form ) luego de comprobar que se crearon los 100 archivos termina el sub main del modulo y finaliza el programa y el otro programa que llamo a la funcion para inciar el servicio noce cuelga :P
bueno espero que alguien si pueda



Título: Re: duda acerca de los servicios :P
Publicado por: BlackZeroX en 3 Septiembre 2011, 21:10 pm
.
http://foro.elhacker.net/analisis_y_diseno_de_malware/manejar_sevicios_desde_vb-t123518.0.html

En el mismo enlace trae un ejemplo en lenguaje C... solo traducelo.

Dulces Lunas!¡.


Título: Re: duda acerca de los servicios :P
Publicado por: BlackZeroX en 3 Septiembre 2011, 21:50 pm
.
Aqui te dejo un servicio en vb6 es el que te dige que tradujeras, solo le faltan las constantes, y la verdad ya me dio weba buscarlas asi que aqui te lo dejo.

SOLO LO TRADUJE JAMAS LO PROBE... si puedes corregir los errores adelante si no avisa.

Va en un modulo X...

Código
  1.  
  2. Option Explicit
  3.  
  4. Private Const NO_ERROR = 0
  5.  
  6. Private Const SERVICE_WIN32_OWN_PROCESS = &H10&
  7. Private Const SERVICE_WIN32_SHARE_PROCESS = &H20&
  8. Private Const SERVICE_WIN32 = SERVICE_WIN32_OWN_PROCESS + _
  9.                              SERVICE_WIN32_SHARE_PROCESS
  10. Private Const SERVICE_ACCEPT_STOP = &H1
  11. Private Const SERVICE_ACCEPT_PAUSE_CONTINUE = &H2
  12. Private Const SERVICE_ACCEPT_SHUTDOWN = &H4
  13. Private Const SC_MANAGER_CONNECT = &H1
  14. Private Const SC_MANAGER_CREATE_SERVICE = &H2
  15. Private Const SC_MANAGER_ENUMERATE_SERVICE = &H4
  16. Private Const SC_MANAGER_LOCK = &H8
  17. Private Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
  18. Private Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
  19. Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
  20. Private Const SERVICE_QUERY_CONFIG = &H1
  21. Private Const SERVICE_CHANGE_CONFIG = &H2
  22. Private Const SERVICE_QUERY_STATUS = &H4
  23. Private Const SERVICE_ENUMERATE_DEPENDENTS = &H8
  24. Private Const SERVICE_START = &H10
  25. Private Const SERVICE_STOP = &H20
  26. Private Const SERVICE_PAUSE_CONTINUE = &H40
  27. Private Const SERVICE_INTERROGATE = &H80
  28. Private Const SERVICE_USER_DEFINED_CONTROL = &H100
  29. Private Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
  30.                                    SERVICE_QUERY_CONFIG Or _
  31.                                    SERVICE_CHANGE_CONFIG Or _
  32.                                    SERVICE_QUERY_STATUS Or _
  33.                                    SERVICE_ENUMERATE_DEPENDENTS Or _
  34.                                    SERVICE_START Or _
  35.                                    SERVICE_STOP Or _
  36.                                    SERVICE_PAUSE_CONTINUE Or _
  37.                                    SERVICE_INTERROGATE Or _
  38.                                     SERVICE_USER_DEFINED_CONTROL)
  39. Private Const SERVICE_DEMAND_START As Long = &H3
  40. Private Const SERVICE_ERROR_NORMAL As Long = &H1
  41. ' Private Enum SERVICE_CONTROL
  42. Private Const SERVICE_CONTROL_STOP = &H1
  43. Private Const SERVICE_CONTROL_PAUSE = &H2
  44. Private Const SERVICE_CONTROL_CONTINUE = &H3
  45. Private Const SERVICE_CONTROL_INTERROGATE = &H4
  46. Private Const SERVICE_CONTROL_SHUTDOWN = &H5
  47. ' End Enum
  48. ' Private Enum SERVICE_STATE
  49. Private Const SERVICE_STOPPED = &H1
  50. Private Const SERVICE_START_PENDING = &H2
  51. Private Const SERVICE_STOP_PENDING = &H3
  52. Private Const SERVICE_RUNNING = &H4
  53. Private Const SERVICE_CONTINUE_PENDING = &H5
  54. Private Const SERVICE_PAUSE_PENDING = &H6
  55. Private Const SERVICE_PAUSED = &H7
  56. ' End Enum
  57.  
  58.  
  59. 'typedef struct _SERVICE_TABLE_ENTRY {
  60. '  LPTSTR                  lpServiceName;
  61. '  LPSERVICE_MAIN_FUNCTION lpServiceProc;
  62. '} SERVICE_TABLE_ENTRY, *LPSERVICE_TABLE_ENTRY;
  63. 'http://msdn.microsoft.com/en-us/library/ms686001%28v=vs.85%29.aspx
  64. Type SERVICE_TABLE_ENTRY
  65.    lpServiceName   As String
  66.    lpServiceProc   As Long
  67. End Type
  68.  
  69. 'BOOL WINAPI StartServiceCtrlDispatcher(
  70. '  __in  const SERVICE_TABLE_ENTRY *lpServiceTable
  71. ');
  72. 'http://msdn.microsoft.com/en-us/library/ms686324%28v=vs.85%29.aspx
  73. Private Declare Function StartServiceCtrlDispatcher Lib "advapi32.dll" Alias "StartServiceCtrlDispatcherA" (lpServiceStartTable As SERVICE_TABLE_ENTRY) As Long
  74.  
  75. 'typedef struct _SERVICE_STATUS {
  76. '  DWORD dwServiceType;
  77. '  DWORD dwCurrentState;
  78. '  DWORD dwControlsAccepted;
  79. '  DWORD dwWin32ExitCode;
  80. '  DWORD dwServiceSpecificExitCode;
  81. '  DWORD dwCheckPoint;
  82. '  DWORD dwWaitHint;
  83. '} SERVICE_STATUS, *LPSERVICE_STATUS;
  84. 'http://msdn.microsoft.com/en-us/library/ms685996%28VS.85%29.aspx
  85. Type SERVICE_STATUS
  86.    dwServiceType               As Long
  87.    dwCurrentState              As Long
  88.    dwControlsAccepted          As Long
  89.    dwWin32ExitCode             As Long
  90.    dwServiceSpecificExitCode   As Long
  91.    dwCheckPoint                As Long
  92.    dwWaitHint                  As Long
  93. End Type
  94.  
  95. 'SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandler(
  96. '  __in  LPCTSTR lpServiceName,
  97. '  __in  LPHANDLER_FUNCTION lpHandlerProc
  98. ');
  99. 'http://msdn.microsoft.com/en-us/library/ms685054%28VS.85%29.aspx
  100. Public Declare Function RegisterServiceCtrlHandler Lib "advapi32.dll" Alias "RegisterServiceCtrlHandlerA" (ByVal lpServiceName As String, ByVal lpHandlerProc As Long) As Long
  101.  
  102. 'void WINAPI OutputDebugString(
  103. '  __in_opt  LPCTSTR lpOutputString
  104. ');
  105. 'http://msdn.microsoft.com/en-us/library/aa363362%28VS.85%29.aspx
  106. Public Declare Sub OutputDebugString Lib "kernel32.dll" Alias "OutputDebugStringA" (ByVal lpServiceTable As String)
  107.  
  108. 'BOOL WINAPI SetServiceStatus(
  109. '  __in  SERVICE_STATUS_HANDLE hServiceStatus,
  110. '  __in  LPSERVICE_STATUS lpServiceStatus
  111. ');
  112. 'http://msdn.microsoft.com/en-us/library/ms686241%28VS.85%29.aspx
  113. Public Declare Function SetServiceStatus Lib "advapi32.dll" (ByVal hServiceStatus As Long, ByVal lpServiceStatus As Long) As Long
  114.  
  115. 'DWORD WINAPI GetLastError(void);
  116. 'http://msdn.microsoft.com/en-us/library/ms679360%28v=VS.85%29.aspx
  117. Public Declare Function GetLastError Lib "kernel32.dll" () As Long
  118.  
  119. Dim MyServiceStatus             As SERVICE_STATUS
  120. Dim MyServiceStatusHandle       As Long
  121.  
  122. Public Function getLPFunc(ByVal lpFunc As Long) As Long
  123.    getLPFunc = lpFunc
  124. End Function
  125.  
  126. Sub main()
  127. Dim DispatchTable(1) As SERVICE_TABLE_ENTRY
  128.    DispatchTable(0).lpServiceName = "MyService"
  129.    DispatchTable(0).lpServiceProc = getLPFunc(AddressOf MyServiceStart)
  130.    DispatchTable(1).lpServiceName = vbNullString
  131.    DispatchTable(1).lpServiceProc = &H0
  132.    If (Not StartServiceCtrlDispatcher(DispatchTable(0))) Then
  133.        OutputDebugString " [MY_SERVICE] StartServiceCtrlDispatcher " & GetLastError
  134.    End If
  135. End Sub
  136.  
  137. Public Function MyServiceStart(ByVal lCantArg As Long, ByVal lpArgv As Long)
  138. Dim status          As Long
  139. Dim SpecificError   As Long
  140.  
  141.    MyServiceStatus.dwServiceType = SERVICE_WIN32
  142.    MyServiceStatus.dwCurrentState = SERVICE_START_PENDING
  143.    MyServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP Or SERVICE_ACCEPT_PAUSE_CONTINUE
  144.    MyServiceStatus.dwWin32ExitCode = 0
  145.    MyServiceStatus.dwServiceSpecificExitCode = 0
  146.    MyServiceStatus.dwCheckPoint = 0
  147.    MyServiceStatus.dwWaitHint = 0
  148.  
  149.    MyServiceStatusHandle = RegisterServiceCtrlHandler("MyService", getLPFunc(AddressOf MyServiceCtrlHandler))
  150.  
  151.    If (MyServiceStatusHandle = &H0) Then
  152.        status = GetLastError()
  153.        OutputDebugString (" [MY_SERVICE] RegisterServiceCtrlHandler failed " & status)
  154.        Exit Function
  155.    End If
  156.  
  157.    status = MyServiceInitialization(lCantArg, lpArgv, SpecificError)
  158.    If (Not (status = NO_ERROR)) Then
  159.        MyServiceStatus.dwCurrentState = SERVICE_STOPPED
  160.        MyServiceStatus.dwCheckPoint = 0
  161.        MyServiceStatus.dwWaitHint = 0
  162.        MyServiceStatus.dwWin32ExitCode = status
  163.        MyServiceStatus.dwServiceSpecificExitCode = SpecificError
  164.        SetServiceStatus MyServiceStatusHandle, ByVal VarPtr(MyServiceStatus)
  165.        Exit Function
  166.    End If
  167.    MyServiceStatus.dwCurrentState = SERVICE_RUNNING
  168.    MyServiceStatus.dwCheckPoint = 0
  169.    MyServiceStatus.dwWaitHint = 0
  170.  
  171.    If (Not SetServiceStatus(MyServiceStatusHandle, ByVal VarPtr(MyServiceStatus))) Then
  172.        status = GetLastError()
  173.        OutputDebugString (" [MY_SERVICE] RegisterServiceCtrlHandler error " & status)
  174.    End If
  175.  
  176.    OutputDebugString (" [MY_SERVICE] Returning the Main Thread ")
  177.  
  178. End Function
  179.  
  180. Public Function MyServiceInitialization(ByVal lCantArg As Long, ByVal lpArgv As Long, ByRef SpecificError As Long)
  181. Dim ff      As Long
  182.    ff = FreeFile
  183.    Open "c:\Servicio en VB6.txt" For Binary As ff
  184.        Put ff, , "Hola mundo desde un servicio creado en vb6"
  185.    Close ff
  186.    MyServiceInitialization = 0
  187. End Function
  188.  
  189. Public Sub MyServiceCtrlHandler(ByVal Opcode As Long)
  190. Dim status      As Long
  191.    Select Case (Opcode)
  192.        Case SERVICE_CONTROL_PAUSE:
  193.            MyServiceStatus.dwCurrentState = SERVICE_PAUSED
  194.        Case SERVICE_CONTROL_CONTINUE:
  195.            MyServiceStatus.dwCurrentState = SERVICE_RUNNING
  196.        Case SERVICE_CONTROL_STOP:
  197.            MyServiceStatus.dwWin32ExitCode = 0
  198.            MyServiceStatus.dwCurrentState = SERVICE_STOPPED
  199.            MyServiceStatus.dwCheckPoint = 0
  200.            MyServiceStatus.dwWaitHint = 0
  201.  
  202.            If (Not SetServiceStatus(MyServiceStatusHandle, ByVal VarPtr(MyServiceStatus))) Then
  203.                status = GetLastError()
  204.                OutputDebugString " [MY_SERVICE] SetServiceStatus error " & status
  205.            End If
  206.  
  207.            OutputDebugString " [MY_SERVICE] Leaving MyService"
  208.            Exit Sub
  209.  
  210.        Case SERVICE_CONTROL_INTERROGATE:
  211.        Case Else
  212.            OutputDebugString " [MY_SERVICE] Unrecognized opcode " & Opcode
  213.    End Select
  214.    If (Not SetServiceStatus(MyServiceStatusHandle, ByVal VarPtr(MyServiceStatus))) Then
  215.      status = GetLastError()
  216.      OutputDebugString " [MY_SERVICE] SetServiceStatus error " & status
  217.    End If
  218. End Sub
  219.  
  220.  

Sangriento Infirno Lunar!¡.


Título: Re: duda acerca de los servicios :P
Publicado por: x64core en 3 Septiembre 2011, 22:23 pm
gracias tio por tomarte el tiempo de traducirlo y ayudar :) pero ya le probado :P y no funka :P lo e probado usando mi logica :P en "myservice" escribi el servicio que queria ejecutar y no anda hace todo el proceso del sub main y luego finaliza :P no llama a la funcion callback ( creo que asi se le llama :P )  que veo ahi :P


Título: Re: duda acerca de los servicios :P
Publicado por: BlackZeroX en 3 Septiembre 2011, 22:26 pm
y si le metes un...

Código
  1.  
  2. do
  3. doevents
  4. loop
  5.  
  6.  

despues de...

Código
  1.  
  2.    If (Not StartServiceCtrlDispatcher(DispatchTable(0))) Then
  3.        OutputDebugString " [MY_SERVICE] StartServiceCtrlDispatcher " & GetLastError
  4.    End If
  5.  
  6.  

Dulces Lunas!¡.


Título: Re: duda acerca de los servicios :P
Publicado por: x64core en 3 Septiembre 2011, 22:40 pm
tampoco : :-\
estos diaz e buscado, y buscado pero no hay mucha informacion :P al menos no como inciar un servicio propio y que funcione bien e encontrado esto pero al paracer no esta terminado creo :P y segun el autor funciona perfectamente y el preciso para eso :P

Código
  1.    Option Explicit
  2.    Public SS As SERVICE_STATUS
  3.    Public hSS As Long
  4.    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
  5.    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
  6.    Public directory As String
  7.    Private Sub Main()
  8.  
  9.    If InStr(Command$, "SSTARTED") > 0 Then
  10.    'the service executed us,
  11.    'we are now a normal process, but with SYSTEM privileges.
  12.    Load Program 'Do what you want here, you CAN use GUI again.
  13.    Exit Sub
  14.    End If
  15.  
  16.    'YOU CANNOT CALL ANY GUI HERE!
  17.    'As service, you cannot have any graphical stuff.
  18.    Dim hnd As Long
  19.    Dim h(0 To 1) As Long
  20.    hStopEvent = CreateEvent(0, 1, 0, vbNullString)
  21.    hStopPendingEvent = CreateEvent(0, 1, 0, vbNullString)
  22.    hStartEvent = CreateEvent(0, 1, 0, vbNullString)
  23.    ServiceName = StrConv(Service_Name, vbFromUnicode)
  24.    ServiceNamePtr = VarPtr(ServiceName(LBound(ServiceName)))
  25.    hnd = StartAsService
  26.    h(0) = hnd
  27.    h(1) = hStartEvent
  28.    IsNTService = WaitForMultipleObjects(2&, h(0), 0&, -1&) = 1&
  29.  
  30.    If Not IsNTService Then
  31.    CloseHandle hnd
  32.    SetNTService
  33.    DoEvents
  34.    StartNTService
  35.    Exit Sub
  36.    End If
  37.  
  38.    If IsNTService Then
  39.    SetServiceState SERVICE_RUNNING
  40.    Do
  41.    'Okay, we're a service,
  42.    'Let's execute ourself again so that we can move on..
  43.    ShellExecute 0&, "open", App.Path & "\" & App.EXEName & ".exe", " SSTARTED", App.Path & "\", vbNormal
  44.    StopNTService
  45.    Exit Do
  46.  
  47.    Loop While WaitForSingleObject(hStopPendingEvent, 1000&) = 258&
  48.    SetServiceState SERVICE_STOPPED
  49.    SetEvent hStopEvent
  50.    WaitForSingleObject hnd, -1&
  51.    CloseHandle hnd
  52.    End
  53.    End If
  54.    CloseHandle hStopEvent
  55.    CloseHandle hStartEvent
  56.    CloseHandle hStopPendingEvent
  57.    End Sub
  58.    Private Sub ServiceThread(ByVal Dummy As Long)
  59.       Dim ServiceTableEntry As SERVICE_TABLE
  60.       ServiceTableEntry.lpServiceName = ServiceNamePtr
  61.       ServiceTableEntry.lpServiceProc = FncPtr(AddressOf ServiceMain)
  62.       StartServiceCtrlDispatcher ServiceTableEntry
  63.    End Sub
  64.    Function FncPtr(ByVal fnp As Long) As Long
  65.       FncPtr = fnp
  66.    End Function
  67.    Private Sub ServiceMain(ByVal dwArgc As Long, ByVal lpszArgv As Long)
  68.       SS.dwServiceType = SERVICE_WIN32_OWN_PROCESS
  69.       SS.dwControlsAccepted = SERVICE_ACCEPT_STOP _
  70.                                       Or SERVICE_ACCEPT_SHUTDOWN
  71.       SS.dwWin32ExitCode = 0&
  72.       SS.dwServiceSpecificExitCode = 0&
  73.       SS.dwCheckPoint = 0&
  74.       SS.dwWaitHint = 0&
  75.       hSS = RegisterServiceCtrlHandler(Service_Name, _
  76.                              AddressOf Handler)
  77.       SetServiceState SERVICE_START_PENDING
  78.       SetEvent hStartEvent
  79.       WaitForSingleObject hStopEvent, -1&
  80.    End Sub
  81.    Public Function StartAsService() As Long
  82.       Dim ThreadId As Long
  83.       StartAsService = CreateThread(0&, 0&, AddressOf ServiceThread, 0&, 0&, ThreadId)
  84.    End Function
  85.    Private Sub Handler(ByVal fdwControl As Long)
  86.       Select Case fdwControl
  87.           Case SERVICE_CONTROL_SHUTDOWN, SERVICE_CONTROL_STOP
  88.               SetServiceState SERVICE_STOP_PENDING
  89.               SetEvent hStopPendingEvent
  90.           Case Else
  91.               SetServiceState
  92.       End Select
  93.    End Sub
  94.    Public Sub SetServiceState(Optional ByVal NewState As SERVICE_STATE = 0&)
  95.       If NewState <> 0& Then SS.dwCurrentState = NewState
  96.       SetServiceStatus hSS, SS
  97.    End Sub
  98.    Public Function FileExists1(fName$) As Boolean
  99.    On Local Error Resume Next
  100.    Dim ff
  101.    ff = FreeFile
  102.    Open fName$ For Input As ff
  103.    If Err Then
  104.    FileExists1 = False
  105.    Else
  106.    FileExists1 = True
  107.    End If
  108.    Close ff
  109.    End Function
  110.    'This module is Coded by SqUeEzEr (Don't remove this line)
  111.  
  112.  

Código
  1.    Option Explicit
  2.    'Our service name!!! IMPORTANT!
  3.    Private Const Service_Display_Name As String = "SqUeEzEr"
  4.    Public Const Service_Name = "SqUeEzEr"
  5.    Public AppPath As String
  6.    Public IsNTService As Boolean
  7.    Public hStopEvent As Long, hStartEvent As Long, hStopPendingEvent
  8.    Public ServiceName() As Byte, ServiceNamePtr As Long
  9.    Public Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, ByVal lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
  10.    Private Type SERVICE_STATUS
  11.       dwServiceType As Long
  12.       dwCurrentState As Long
  13.       dwControlsAccepted As Long
  14.       dwWin32ExitCode As Long
  15.       dwServiceSpecificExitCode As Long
  16.       dwCheckPoint As Long
  17.       dwWaitHint As Long
  18.    End Type
  19.    Private Type QUERY_SERVICE_CONFIG
  20.       dwServiceType As Long
  21.       dwStartType As Long
  22.       dwErrorControl As Long
  23.       lpBinaryPathName As Long
  24.       lpLoadOrderGroup As Long
  25.       dwTagId As Long
  26.       lpDependencies As Long
  27.       lpServiceStartName As Long
  28.       lpDisplayName As Long
  29.    End Type
  30.    Public Enum SERVICE_STATE
  31.       SERVICE_STOPPED = &H1
  32.       SERVICE_START_PENDING = &H2
  33.       SERVICE_STOP_PENDING = &H3
  34.       SERVICE_RUNNING = &H4
  35.       SERVICE_CONTINUE_PENDING = &H5
  36.       SERVICE_PAUSE_PENDING = &H6
  37.       SERVICE_PAUSED = &H7
  38.    End Enum
  39.    Private Enum SERVICE_CONTROL
  40.       SERVICE_CONTROL_STOP = 1&
  41.       SERVICE_CONTROL_PAUSE = 2&
  42.       SERVICE_CONTROL_CONTINUE = 3&
  43.       SERVICE_CONTROL_INTERROGATE = 4&
  44.       SERVICE_CONTROL_SHUTDOWN = 5&
  45.    End Enum
  46.    Private Const SERVICE_ALL_ACCESS = (&HF0000 Or &H1& Or &H2& Or &H4& Or &H8& Or &H10& Or &H20& Or &H40& Or &H80& Or &H100&)
  47.    Private Declare Function OpenService _
  48.         Lib "advapi32" Alias "OpenServiceA" _
  49.         (ByVal hSCManager As Long, ByVal lpServiceName As String, _
  50.         ByVal dwDesiredAccess As Long) As Long   '** Change SERVICE_NAME as needed
  51.    Private Declare Function CreateService _
  52.         Lib "advapi32" Alias "CreateServiceA" _
  53.         (ByVal hSCManager As Long, ByVal lpServiceName As String, _
  54.         ByVal lpDisplayName As String, ByVal dwDesiredAccess As Long, _
  55.         ByVal dwServiceType As Long, ByVal dwStartType As Long, _
  56.         ByVal dwErrorControl As Long, ByVal lpBinaryPathName As String, _
  57.         ByVal lpLoadOrderGroup As String, ByVal lpdwTagId As String, _
  58.         ByVal lpDependencies As String, ByVal lp As String, _
  59.         ByVal lpPassword As String) As Long
  60.    Private Declare Function QueryServiceConfig Lib "advapi32" _
  61.         Alias "QueryServiceConfigA" (ByVal hService As Long, _
  62.         lpServiceConfig As QUERY_SERVICE_CONFIG, _
  63.         ByVal cbBufSize As Long, pcbBytesNeeded As Long) As Long
  64.    Private Declare Function QueryServiceStatus Lib "advapi32" _
  65.       (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
  66.    Private Declare Function ControlService Lib "advapi32" _
  67.           (ByVal hService As Long, ByVal dwControl As SERVICE_CONTROL, _
  68.           lpServiceStatus As SERVICE_STATUS) As Long
  69.    Private Declare Function StartService Lib "advapi32" _
  70.           Alias "StartServiceA" (ByVal hService As Long, _
  71.           ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long
  72.    Private Declare Function OpenSCManager _
  73.         Lib "advapi32" Alias "OpenSCManagerA" _
  74.         (ByVal lpMachineName As String, ByVal lpDatabaseName As String, _
  75.         ByVal dwDesiredAccess As Long) As Long
  76.    Private Declare Function DeleteService _
  77.         Lib "advapi32" (ByVal hService As Long) As Long
  78.    Private Declare Function CloseServiceHandle _
  79.         Lib "advapi32" (ByVal hSCObject As Long) As Long
  80.    Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
  81.    Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As Long
  82.    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
  83.    Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
  84.    Public Function SetNTService() As Long
  85.    Dim hSCManager As Long
  86.    Dim hService As Long, DomainName As String
  87.    hSCManager = OpenSCManager(vbNullString, vbNullString, _
  88.                          &H2&)
  89.    If hSCManager <> 0 Then
  90.       hService = CreateService(hSCManager, Service_Name, _
  91.                          Service_Display_Name, SERVICE_ALL_ACCESS, _
  92.                          &H10&, _
  93.                          2, 1, _
  94.                          App.Path & "\" & App.EXEName & ".exe", vbNullString, _
  95.                          vbNullString, vbNullString, "LocalSystem", _
  96.                          vbNullString)
  97.       If hService <> 0 Then
  98.           CloseServiceHandle hService
  99.       Else
  100.           SetNTService = Err.LastDllError
  101.       End If
  102.       CloseServiceHandle hSCManager
  103.    Else
  104.       SetNTService = Err.LastDllError
  105.    End If
  106.    End Function
  107.    Public Function StopNTService() As Long
  108.    Dim hSCManager As Long, hService As Long, Status As SERVICE_STATUS
  109.    hSCManager = OpenSCManager(vbNullString, vbNullString, _
  110.                          &H1&)
  111.    If hSCManager <> 0 Then
  112.       hService = OpenService(hSCManager, Service_Name, &H20&)
  113.       If hService <> 0 Then
  114.           If ControlService(hService, SERVICE_CONTROL_STOP, Status) = 0 Then
  115.               StopNTService = Err.LastDllError
  116.           End If
  117.       CloseServiceHandle hService
  118.       Else
  119.           StopNTService = Err.LastDllError
  120.       End If
  121.    CloseServiceHandle hSCManager
  122.    Else
  123.       StopNTService = Err.LastDllError
  124.    End If
  125.    End Function
  126.    Public Function StartNTService() As Long
  127.    Dim hSCManager As Long, hService As Long
  128.    hSCManager = OpenSCManager(vbNullString, vbNullString, _
  129.                          &H1&)
  130.    If hSCManager <> 0 Then
  131.       hService = OpenService(hSCManager, Service_Name, &H10&)
  132.       If hService <> 0 Then
  133.           If StartService(hService, 0, 0) = 0 Then
  134.               StartNTService = Err.LastDllError
  135.           End If
  136.       CloseServiceHandle hService
  137.       Else
  138.           StartNTService = Err.LastDllError
  139.       End If
  140.    CloseServiceHandle hSCManager
  141.    Else
  142.       StartNTService = Err.LastDllError
  143.    End If
  144.    End Function
  145.    'This module is Coded by SqUeEzEr (Don't remove this line)
  146.  
  147.  

e analizado los codigos expuestos pero no entiendo mucho :P mas o menos creo que la secuencia es que se finaliza y luego se abre de nuevo como servicio propio y en ese casi si funciona :P
bueno la verdad si e puesto empeño en resolver este problema pero no e podido  :-\