服务控制管理器启动过程与cmd /双击启动过程之间的区别

时间:2014-07-02 03:28:40

标签: version-control createprocess nlb

操作系统:Windows Server 2008; IDE:visual studio 2005

我有一个应用程序A.我使用函数CreateProcess在A中创建了一个NLB / WLBS进程。现在我想通过命令'wlbs start / stop [cluster ip]启动或停止远程集群节点:[remote node ip]'。此命令由我的应用程序A中的CreateProcess函数调用。当我从服务控制管理器(SCM)启动A时,它无法启动/停止远程集群节点。当我从cmd启动A或双击它时,它可以启动/停止远程群集节点。

谁能帮助我搞清楚?这是一些关键代码:

服务安装部分:

    BOOL CNTService::Install()
{
    // Open the Service Control Manager
    SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
                                     NULL, // ServicesActive database
                                     SC_MANAGER_ALL_ACCESS); // full access
    if (!hSCM) return FALSE;

    // Get the executable file path
    char szFilePath[_MAX_PATH];
    ::GetModuleFileName(NULL, szFilePath, sizeof(szFilePath));

    // Create the service
    SC_HANDLE hService = ::CreateService(hSCM,
                                         m_szServiceName,
                                         m_szServiceName,
                                         SERVICE_ALL_ACCESS,
                                         SERVICE_WIN32_OWN_PROCESS |SERVICE_INTERACTIVE_PROCESS,
                                         SERVICE_DEMAND_START,        // start condition
                                         SERVICE_ERROR_NORMAL,
                                         szFilePath,
                                         NULL,
                                         NULL,
                                         NULL,
                                         NULL,
                                         NULL);
    if (!hService) {
      DWORD dwErr = GetLastError();
      switch(dwErr)
      {
      case ERROR_ACCESS_DENIED:
        break;
      case ERROR_CIRCULAR_DEPENDENCY:
        break;
      case ERROR_DUP_NAME:
        break;
      case ERROR_INVALID_HANDLE:
        break;
      case ERROR_INVALID_NAME:
        break;
      case ERROR_INVALID_PARAMETER:
        break;
      case ERROR_INVALID_SERVICE_ACCOUNT:
        break;
      case ERROR_SERVICE_EXISTS:
        break;
      default:
        break;
      }
      NvUtility::DisplayError("CreateService");
      SetLastError(dwErr);
      ::CloseServiceHandle(hSCM);
      return FALSE;
    }

    // make registry entries to support logging messages
    // Add the source name as a subkey under the Application
    // key in the EventLog service portion of the registry.
    char szKey[256];
    HKEY hKey = NULL;
    strcpy(szKey, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\");
    strcat(szKey, m_szServiceName);
    if (::RegCreateKey(HKEY_LOCAL_MACHINE, szKey, &hKey) != ERROR_SUCCESS) {
        ::CloseServiceHandle(hService);
        ::CloseServiceHandle(hSCM);
        return FALSE;
    }

    // Add the Event ID message-file name to the 'EventMessageFile' subkey.
    ::RegSetValueEx(hKey,
                    "EventMessageFile",
                    0,
                    REG_EXPAND_SZ, 
                    (CONST BYTE*)szFilePath,
                    strlen(szFilePath) + 1);     

    // Set the supported types flags.
    DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
    ::RegSetValueEx(hKey,
                    "TypesSupported",
                    0,
                    REG_DWORD,
                    (CONST BYTE*)&dwData,
                     sizeof(DWORD));
    ::RegCloseKey(hKey);

    LogEvent(EVENTLOG_INFORMATION_TYPE, EVMSG_INSTALLED, m_szServiceName);

    // tidy up
    ::CloseServiceHandle(hService);
    ::CloseServiceHandle(hSCM);
    return TRUE;
}

创建过程分区:

    result  NvCs::ExecWlbsCommand(const TCHAR *cmd)
{
    result res = errUnknown;
    PROCESS_INFORMATION pi = {NULL};
    STARTUPINFO si = {sizeof(si)};
    ZeroMemory(&pi, sizeof(pi));
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);

    #if defined NDEBUG
    si.wShowWindow = SW_HIDE;
    #else
    si.wShowWindow = SW_SHOWNORMAL;
    #endif

    // wlbs.exe is in the system directory
    TCHAR cmdLine[MAX_PATH] = TEXT("");
    UINT cb = ::GetSystemDirectory(cmdLine, sizeof cmdLine);
    // This path does not end with a backslash unless the system directory is the root directory
    if ((cb > 3) && (cb < sizeof cmdLine)) _tcscat(cmdLine, TEXT("\\"));

    _tcscat(cmdLine, "WLBS.EXE ");
    _tcscat(cmdLine, cmd);

    if (::CreateProcess(NULL, cmdLine, 0, 0, FALSE, CREATE_NEW_CONSOLE, 0, 0, &si, &pi))
    {
        DWORD dwWait = ::WaitForSingleObject(pi.hProcess, wlbsTimeout);
        if (WAIT_OBJECT_0 == dwWait)
        {
            DWORD wlbsExitCode = 0XFFFFFFFF;
            if (::GetExitCodeProcess(pi.hProcess, &wlbsExitCode) && (1 == wlbsExitCode))
                res = success;
            DebugMsg("wlbsExitCode = %d\n", wlbsExitCode);
        } else {
            res = errUnexpected;
            DebugMsg("Trouble while executing WLBS command");
        }
        ::CloseHandle(pi.hProcess);
        ::CloseHandle(pi.hThread);
    }

  if(m_b_simul_wlbs)
    res = success;
  DebugMsg("ExecWlbsCommand(%s), res = %d\n", cmdLine, res);
    return res;
}

0 个答案:

没有答案