来大连这个城市快7年了,自认为是个执着的人对感情能一如既往但每次开始和结束只有那么短短几个小时。 
伤心了,也许这个世界上属于我的另一半在火星抑或根本不存在谁知道呢?同一条路上我走了两遍犯了两次错误,我连一个智商低于70的阿甘都不如,成功或失败一切的一切皆以尘埃落定,往事随风散不去丝丝情愁,时间蹉跎带走生命点滴,夜半细雨落人间,不知君泪为谁落。 
但问君欲何处去,仰面长叹心已固,干涸之心早已死,何故击缶惹涟漪。

解决方案 »

  1.   


    #define JADE_HEAP_USED 0x80001000 //进程堆正在使用class JadeSEH
    {
    protected:
    DWORD ExceptCode(DWORD dwCode,PEXCEPTION_POINTERS lpException);
    };class JadeScManager:protected JadeSEH
    {
    public:
    JadeScManager(LPCTSTR lpMachineName,LPCTSTR lpDatabaseName);
    JadeScManager();
    ~JadeScManager();
    public:
    class JadeService:protected JadeSEH
    {
    public:
    JadeService();
    ~JadeService();
    public:
    DWORD QuerySvrCfg(LPQUERY_SERVICE_CONFIG *lpSvrCfg,DWORD &dwBytesNeeded,DWORD &dwCount);
    DWORD QuerySvrDescription(LPTSTR *lpszDsp,DWORD &dwBytes);
    DWORD QuerySvrFailureActions(LPSERVICE_FAILURE_ACTIONS *lpfActs,DWORD &dwBytes);
    DWORD QueryDependentCount(DWORD dwState,DWORD &dwCount,DWORD &dwMemNeed); #if _WIN32_WINNT < 0x0500
    inline DWORD QueryServiceStatus(LPSERVICE_STATUS lpServiceStatus);
    #endif

    DWORD EnumDependentInfo(DWORD dwState,LPENUM_SERVICE_STATUS lpService
    ,DWORD &dwMem);

    DWORD ReleaseQuery(LPQUERY_SERVICE_CONFIG lpSvrCfg);
    DWORD ReleaseQuery(LPTSTR lpszDsp);
    DWORD ReleaseQuery(LPSERVICE_FAILURE_ACTIONS lpfActs); DWORD ChangeSvrCfg(DWORD dwSvrType,DWORD dwStartType,DWORD dwErrorControl
    ,LPCTSTR lpBinaryPathName,LPCTSTR lpLoadOrderGroup
    ,LPDWORD lpdwTagId,LPCTSTR lpDependencies
    ,LPCTSTR lpSvrStartName,LPCTSTR lpPassword
    ,LPCTSTR lpDisplayName);

    DWORD SetSvrDescription(LPCTSTR lpszDsp);
    DWORD SetSvrFailureActions(LPSERVICE_FAILURE_ACTIONS lptgFAct);
    private:
    friend JadeScManager;
    SC_HANDLE m_hSvr;
    LPQUERY_SERVICE_CONFIG m_lpSvrCfg;
    LPSERVICE_DESCRIPTION m_lpSvrDcp;
    LPSERVICE_FAILURE_ACTIONS m_lpSvrFAct;
    DWORD m_dwError;
    };
    public:
    DWORD QueryWin32Service(DWORD dwSvrType,DWORD dwState,DWORD &dwBytesNeeded,DWORD &dwCount,
    LPENUM_SERVICE_STATUS *lpBase);
    DWORD OpenService(JadeService **lpSvr,LPCTSTR lpSvrName,DWORD dwAccess);

    DWORD ReleaseService(JadeService *lpSvr); DWORD ReleaseQuery(LPENUM_SERVICE_STATUS lpBase);

    DWORD StopService(JadeService &lpSvr,BOOL fStopDepends,DWORD dwTimeout);

    inline DWORD Lock(void);

    inline DWORD Unlock(void); inline DWORD CreateService(JadeService &lpSvr,LPCTSTR lpSvrName,LPCTSTR lpDspName,
    DWORD dwDsrAccess,DWORD dwSvrType,DWORD dwStartType,DWORD dwErrorControl,
    LPCTSTR lpBinaryPathName,LPCTSTR lpLoadOrderGroup,LPDWORD lpdwTagId,
    LPCTSTR lpDependencies,LPCTSTR lpServiceStartName,LPCTSTR lpPassword);
    inline DWORD DeleteSvr(JadeService &lpSvr); inline DWORD StartService(JadeService &lpSvr,DWORD dwNumServiceArgs
    ,LPCTSTR* lpServiceArgVectors);

    inline DWORD GetServiceKeyName(JadeService &lpSvr,LPCTSTR lpDispName
    ,CString &szKeyName);
    private:
    SC_HANDLE m_hMag; //
    DWORD m_dwError; //
    LPENUM_SERVICE_STATUS m_lpBase;
    SC_LOCK m_scLock;
    };
      

  2.   


    #if _WIN32_WINNT < 0x0500
    inline DWORD JadeScManager::JadeService::QueryServiceStatus(LPSERVICE_STATUS lpServiceStatus)
    {
    return ( 0 == ::QueryServiceStatus(m_hSvr,lpServiceStatus)?GetLastError():0);
    }
    #endif
    inline DWORD JadeScManager::GetServiceKeyName(JadeService &lpSvr,LPCTSTR lpDispName
    ,CString &szKeyName)
    {
    DWORD dwBufLen = 0,dwRetVal = 0;
    LPSTR lpKey = NULL;
    ::GetServiceKeyName(lpSvr.m_hSvr,lpDispName,NULL,&dwBufLen);
      if( ERROR_INSUFFICIENT_BUFFER != (dwRetVal = GetLastError()))
    return dwRetVal;
    lpKey = (LPSTR)malloc(dwBufLen);
    if(!lpKey) return -1;
    ZeroMemory(lpKey,dwBufLen);
    if(!::GetServiceKeyName(lpSvr.m_hSvr,lpDispName,lpKey,&dwBufLen))
    {
    free(lpKey);
    return GetLastError();
    }
    szKeyName = lpKey;
    free(lpKey);
    return 0;
    }inline DWORD JadeScManager::StartService(JadeService &lpSvr,DWORD dwNumServiceArgs
    ,LPCTSTR* lpServiceArgVectors)
    {
    BOOL bSuc = FALSE;
    bSuc = ::StartService(lpSvr.m_hSvr,dwNumServiceArgs,lpServiceArgVectors);
    return bSuc == TRUE?0:GetLastError();
    }inline DWORD JadeScManager::Lock(void)
    {
    if(m_hMag == NULL)
    return m_dwError;
    m_scLock = LockServiceDatabase(m_hMag);
    return m_scLock == NULL?GetLastError():0;
    }inline DWORD JadeScManager::Unlock(void)
    {
    return  0 == UnlockServiceDatabase(m_scLock)?GetLastError():0;
    }inline DWORD JadeScManager::CreateService(JadeService &lpSvr,LPCTSTR lpSvrName,LPCTSTR lpDspName,
    DWORD dwDsrAccess,DWORD dwSvrType,DWORD dwStartType,DWORD dwErrorControl,
    LPCTSTR lpBinaryPathName,LPCTSTR lpLoadOrderGroup,LPDWORD lpdwTagId,
    LPCTSTR lpDependencies,LPCTSTR lpSvrStartName,LPCTSTR lpPassword)
    {
    lpSvr.m_hSvr = ::CreateService(m_hMag,lpSvrName,lpDspName,dwDsrAccess,dwSvrType,dwStartType,dwErrorControl,
      lpBinaryPathName,lpLoadOrderGroup,lpdwTagId,lpDependencies,lpSvrStartName,
      lpPassword);
    return lpSvr.m_hSvr == NULL?GetLastError():0;
    }inline DWORD JadeScManager::DeleteSvr(JadeService &lpSvr)
    {
    return 0 == DeleteService(lpSvr.m_hSvr)?GetLastError():0;
    }
      

  3.   

    #if !defined(JADE_NEW_RETURN)
    #inlcude "MiscFunction.h"
    #endif
    #include "JadeScManager.h"
    #pragma comment(lib,"Advapi32")JadeScManager::JadeScManager():m_hMag(NULL),m_lpBase(NULL),m_dwError(0),m_scLock(NULL)
    {
    m_hMag = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
    if(m_hMag == NULL)
    m_dwError = GetLastError();
    }JadeScManager::JadeScManager(LPCTSTR lpMachineName,LPCTSTR lpDatabaseName)
      :m_hMag(NULL),m_lpBase(NULL),m_dwError(0),m_scLock(NULL)
    {
    m_hMag = OpenSCManager(lpMachineName,lpMachineName,SC_MANAGER_ALL_ACCESS);
    if(m_hMag == NULL)
    m_dwError = GetLastError();
    }JadeScManager::~JadeScManager()
    {
    if(m_hMag != NULL)
    CloseServiceHandle(m_hMag);
    if(m_lpBase != NULL)
    {
    if(HeapFree(GetProcessHeap(),0,(void*)m_lpBase))
    m_lpBase = NULL;
    }
    }DWORD JadeScManager::QueryWin32Service(DWORD dwSvrType,DWORD dwState,DWORD &dwBytesNeeded,
      DWORD &dwCount,LPENUM_SERVICE_STATUS *lpBase)
    {
    if(m_hMag == NULL)
    return m_dwError;
    if(m_lpBase != NULL)
    {
    *lpBase = m_lpBase;
    return JADE_HEAP_USED;
    }
    DWORD dwResumeHandle = 0,dwReturned = 0;
    HANDLE hHeap = GetProcessHeap();
    __try
    {
    m_lpBase = (LPENUM_SERVICE_STATUS)HeapAlloc(hHeap,HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS
    ,sizeof(ENUM_SERVICE_STATUS));
    m_dwError = EnumServicesStatus(m_hMag,dwSvrType,dwState,m_lpBase,sizeof(ENUM_SERVICE_STATUS),
         &dwBytesNeeded,&dwReturned,&dwResumeHandle);
    //绝大部都应该是0
    if(m_dwError == 0)
    {
    m_dwError= GetLastError();
    if(m_dwError == 234)
    {
    //表示分配的空间过短
    m_lpBase = (LPENUM_SERVICE_STATUS)HeapReAlloc(hHeap,HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS,
    (void*)m_lpBase,dwBytesNeeded);
    }
    else
    {
    *lpBase = NULL;
    dwBytesNeeded = 0;
    dwCount = 0;
    if(HeapFree(hHeap,0,m_lpBase))
    m_lpBase = NULL;
    return m_dwError;
    }
    }
    m_dwError = EnumServicesStatus(m_hMag,dwSvrType,dwState,m_lpBase,dwBytesNeeded,
       &dwBytesNeeded,&dwCount,&dwResumeHandle);
    if(m_dwError == 0)
    {
    *lpBase = NULL;
    dwBytesNeeded = 0;
    dwCount = 0;
    if(HeapFree(hHeap,0,m_lpBase))
    m_lpBase = NULL;
    return (m_dwError = GetLastError()); //操作有误
    }
    //
    *lpBase = m_lpBase;
    }
    __except(ExceptCode(GetExceptionCode(),GetExceptionInformation()))
    {
    lpBase = NULL;
    dwBytesNeeded = 0;
    dwCount = 0;
    if(m_lpBase != NULL)
    {
    if(HeapFree(hHeap,0,m_lpBase))
    m_lpBase = NULL;
    }
    return GetExceptionCode();
    }
    return 0;
    }DWORD JadeScManager::ReleaseQuery(LPENUM_SERVICE_STATUS lpBase)
    {
    __try
    {
    if(m_lpBase != NULL)
    {
    if(HeapFree(GetProcessHeap(),0,(void*)m_lpBase))
    {
    m_lpBase = NULL;
    lpBase = NULL;
    }
    else
    return GetLastError();
    }
    }
    __except(ExceptCode(GetExceptionCode(),GetExceptionInformation()))
    {

    return GetExceptionCode();
    }
    return 0;
    }