hi.
API: StartServiceCtrlDispatcher
This API use to run services. but i cant use it in vb6.
Please help me.

Dani AI

Generated

A short diagnosis and practical checklist for the StartServiceCtrlDispatcher / SetServiceStatus crash reported by (and as suggested, having the actual declarations makes troubleshooting much easier).

StartServiceCtrlDispatcher must be called by a true service process to connect the main thread to the Service Control Manager (SCM); the dispatcher table passed to that call must be well-formed and terminated so SCM can invoke ServiceMain. If the EXE is started manually (not by SCM) or the dispatch table is malformed, the service will fail to start. (learn.microsoft.com)

Common VB6 pitfalls that cause a crash when SetServiceStatus is reached

  • SetServiceStatus must be called with the SERVICE_STATUS_HANDLE returned by RegisterServiceCtrlHandler; calling it with an invalid handle or a badly-packed SERVICE_STATUS structure can return errors or crash the process. (learn.microsoft.com)
  • VB6 structure layout, string vs pointer marshaling, and callback addresses (function pointers) are frequent causes of failures. The SERVICE_TABLE_ENTRY passed to StartServiceCtrlDispatcher must be formed so the SCM sees the expected LPSTR and function-pointer values (the array must be terminated with NULL entries). (learn.microsoft.com)

Minimal VB6 checks (example)

' minimal SERVICE_STATUS + SERVICE_TABLE_ENTRY and API decls
Private Type SERVICE_STATUS
  dwServiceType As Long
  dwCurrentState As Long
  dwControlsAccepted As Long
  dwWin32ExitCode As Long
  dwServiceSpecificExitCode As Long
  dwCheckPoint As Long
  dwWaitHint As Long
End Type

Private Declare Function RegisterServiceCtrlHandler Lib "advapi32.dll" Alias "RegisterServiceCtrlHandlerA" (ByVal lpServiceName As String, ByVal lpHandlerProc As Long) As Long
Private Declare Function SetServiceStatus Lib "advapi32.dll" (ByVal hServiceStatus As Long, lpServiceStatus As SERVICE_STATUS) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long

' after calling SetServiceStatus:
If SetServiceStatus(hSvcHandle, svcStatus) = 0 Then Debug.Print "SetServiceStatus failed, GetLastError=" & Hex$(GetLastError())

Troubleshooting checklist

  • Confirm RegisterServiceCtrlHandler returns non-zero before any SetServiceStatus call and use that handle. (learn.microsoft.com)
  • Immediately call GetLastError when an API returns 0 and log it (Event Log or a file). (learn.microsoft.com)
  • Ensure SERVICE_STATUS fields are initialized (dwServiceType, dwCurrentState, checkpoint/wait-hint for pending states). (learn.microsoft.com)
  • Build the SERVICE_TABLE_ENTRY so its last table entry is {NULL, NULL} (many VB6 examples add extra Long fields or use a tiny native shim to avoid string/pointer marshaling issues). (learn.microsoft.com)

If these checks still fail, using a small native/C++ shim or a .NET service entry that calls VB6 logic (COM/child process) is often much more reliable than pushing complex callbacks and pointer packing through VB6. (learn.microsoft.com)

References: Microsoft API docs and common VB6 examples cited above.

Recommended Answers

All 5 Replies

Hmn, Read This Thread:

Hmn, Read This Thread:

hi.
i had that code in my sources. but this code have a problem.
i compile this code and put the exe file in address of my service to run.
when i click on start of my service to run this exe file , program terminate.
see left pic.

program terminate on this line.
b = SetServiceStatus(hServiceStatus, ServiceStatus)
and show me an error.
see right pic.

Please help me.

Hmn, I might be able to help more if you attach the project, or post some code (use Code Tags)

Hmn, I might be able to help more if you attach the project, or post some code (use Code Tags)

ok.
program terminate on this line :
b = SetServiceStatus(hServiceStatus, ServiceStatus)

Somebody help me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.