advapi32.SetServiceStatus() fails with error 6 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: advapi32.SetServiceStatus() fails with error 6 (/thread-14267.html) |
advapi32.SetServiceStatus() fails with error 6 - crustydave - Nov-22-2018 Error 6 is invalid handle, however, the handle appears to be good, I believe the error is coming from the second parameter, if I make the pointer None, then it doesn't fail (but obviously doesn't do anything either). C:\WINDOWS\system32>python -V Python 3.6.6 #typedef struct _SERVICE_STATUS { # DWORD dwServiceType; # DWORD dwCurrentState; # DWORD dwControlsAccepted; # DWORD dwWin32ExitCode; # DWORD dwServiceSpecificExitCode; # DWORD dwCheckPoint; # DWORD dwWaitHint; #} SERVICE_STATUS, *LPSERVICE_STATUS; class _SERVICE_STATUS(Structure): #_pack_ = 4 _fields_ = [ ("dwServiceType", DWORD), ("dwCurrentState", DWORD), ("dwControlsAccepted", DWORD), ("dwWin32ExitCode", DWORD), ("dwServiceSpecificExitCode", DWORD), ("dwCheckPoint", DWORD), ("dwWaitHint", DWORD) ] LPSERVICE_STATUS = POINTER(_SERVICE_STATUS) global m_oServiceStatus m_oServiceStatus = _SERVICE_STATUS(0, 0, 0, 0, 0, 0, 0) def status_report(dwCurrentState, dwWin32ExitCode, dwWaitHint): global g_dwCheckPoint global g_isService global m_oServiceStatus try: # Fill in the SERVICE_STATUS structure. m_oServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS m_oServiceStatus.dwCurrentState = dwCurrentState m_oServiceStatus.dwWin32ExitCode = dwWin32ExitCode m_oServiceStatus.dwWaitHint = dwWaitHint if dwCurrentState == SERVICE_START_PENDING: m_oServiceStatus.dwControlsAccepted = 0 else: m_oServiceStatus.dwControlsAccepted = 1 if (dwCurrentState == SERVICE_STOPPED) or (dwCurrentState == SERVICE_RUNNING): m_oServiceStatus.dwCheckPoint = 0 else: g_dwCheckPoint += 1 m_oServiceStatus.dwCheckPoint = g_dwCheckPoint status = advapi32.SetServiceStatus(g_hServiceStatus, pointer(m_oServiceStatus)) if 0 == status: dwStatus = winKernel.GetLastError() #logging.info("SetServiceStatus(" + str(g_hServiceStatus) + ", status=" + str(dwStatus) + ")") logging.info("status_report(" + str(g_hServiceStatus) + ", " + str(dwCurrentState) + ", " + str(dwWin32ExitCode) + ", " + str(dwWaitHint) + ")") if g_isService: # Report the status of the service to the SCM. ptrServiceStatus = LPSERVICE_STATUS(m_oServiceStatus) logging.info("m_oServiceStatus struct: " + str(m_oServiceStatus) + ", ref: " + str(byref(m_oServiceStatus))) logging.info(" " + "ptr: " + str(str(pointer(m_oServiceStatus))) + " PTR: " + str(ptrServiceStatus)) status = advapi32.SetServiceStatus(g_hServiceStatus, pointer(m_oServiceStatus)) if 0 == status: dwStatus = winKernel.GetLastError() except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() logging.error("status_report " + str(e) + " line: " + str(exc_tb.tb_lineno)) return dwStatus dwStatus = status_report(SERVICE_START_PENDING, NO_ERROR, 3000); logging.info("service_main: status_report(" + str(g_hServiceStatus) + "), status=" + str(dwStatus)) log_service_status(m_oServiceStatus) Quote:INFO status_report(-842466752, 2, 0, 3000) I must be missing something obvious, but I don't see it. I tried different _pack_ to the structure, but with no improvement. Thanks in advance, -Dave I should add that the service is actually running at this point, I just can't transition it out of Starting state with this issue. RE: advapi32.SetServiceStatus() fails with error 6 - crustydave - Nov-23-2018 I have resolved the issues, please refer to my post on StackOverflow. |