// Stest.cpp : WinMain 的实现
#include "stdafx.h"
#include "resource.h"
#include "Stest_i.h"#include <stdio.h>DWORD WINAPI ThreadFunc(LPVOID lpParam)
{
TCHAR msg[MAX_PATH] = {0};
for(int i = 0; ; i ++)
{
_tcprintf(msg, _T("i = %d"), i);
OutputDebugString(msg);
Sleep(5000);
}
return 0;
}class CStestModule : public CAtlServiceModuleT< CStestModule, IDS_SERVICENAME >
{
public :
DECLARE_LIBID(LIBID_StestLib)
DECLARE_REGISTRY_APPID_RESOURCEID(IDR_STEST, "{C456E2CD-B5C5-4A49-8AA2-8ED2CFB99C55}")
HRESULT InitializeSecurity() throw()
{
// TODO : 调用 CoInitializeSecurity 并为服务提供适当的 
// 安全设置
// 建议 - PKT 级别的身份验证、
// RPC_C_IMP_LEVEL_IDENTIFY 的模拟级别
// 以及适当的非 NULL 安全说明符。
CSecurityDescriptor sd;  
sd.InitializeFromThreadToken();  
HRESULT hr = CoInitializeSecurity( sd  
, -1  
, NULL  
, NULL  
, RPC_C_AUTHN_LEVEL_PKT  
, RPC_C_IMP_LEVEL_IMPERSONATE  
, NULL  
, EOAC_NONE  
, NULL  
);  
return S_OK;
}
HANDLE hThread;
HRESULT PreMessageLoop(int nShowCmd) throw()     
{     
HRESULT hr = S_OK;     
hr = CAtlServiceModuleT<CStestModule,IDS_SERVICENAME>::PreMessageLoop(nShowCmd);      
hr = CoResumeClassObjects();  
if (SUCCEEDED(hr))  
{  
DWORD dwThreadID;
hThread = CreateThread( 
NULL,                        // default security attributes 
0,                           // use default stack size  
(LPTHREAD_START_ROUTINE)ThreadFunc,                  // thread function 
NULL,                // argument to thread function 
0,                           // use default creation flags 
&dwThreadID);                // returns the thread identifier  // Check the return value for success.  if (hThread == NULL) 
{
MessageBox( NULL, _T("CreateThread"), _T("main"), MB_OK );
}
else 
{
//_getch();
MessageBox( NULL, _T("Error"), _T("main"), MB_OK );
CloseHandle( hThread );
}
}     
return hr;     
}  
HRESULT PostMessageLoop() throw()     
{     
HRESULT hr = S_OK;     
hr = CAtlServiceModuleT< CStestModule,IDS_SERVICENAME>::PostMessageLoop();     
if (SUCCEEDED(hr))  
{  
CloseHandle( hThread );
}         
return hr;     
}  
HRESULT RegisterAppId(bool bService=false)  
{  
HRESULT hr = S_OK;  
hr = CAtlServiceModuleT< CStestModule,IDS_SERVICENAME>::RegisterAppId(bService);  
if (!bService)  
return hr;  
SC_HANDLE hSCM = NULL;  
SC_LOCK sclLock = NULL;  
SC_HANDLE hService = NULL;  
try  
{  
hSCM = ::OpenSCManager(NULL, NULL, GENERIC_WRITE | SC_MANAGER_LOCK);  
if (hSCM == NULL)  
{  
OutputDebugString( _T("Error, OpenSCManager failed."));  
hr = E_FAIL;  
goto lblEnd;  
}  
sclLock = LockServiceDatabase(hSCM);  
if(sclLock == NULL)  
{  
OutputDebugString( _T("Error, LockServiceDatabase failed."));  
hr = E_FAIL;  
goto lblEnd;  
}  
hService = OpenService( hSCM  
, m_szServiceName  
, SERVICE_CHANGE_CONFIG  
);  
if (hService == NULL)  
{  
OutputDebugString( _T("Error, OpenService failed."));  
hr = E_FAIL;  
goto lblEnd;  
}  
if (! ChangeServiceConfig( hService  
, SERVICE_WIN32_OWN_PROCESS  
, SERVICE_AUTO_START  
, SERVICE_NO_CHANGE  
, NULL  
, NULL  
, NULL  
, NULL  
, NULL  
, NULL  
, NULL  
)   
)  
{  
OutputDebugString( _T("Error, ChangeServiceConfig failed."));  
hr = E_FAIL;  
goto lblEnd;  
}  
SERVICE_DESCRIPTION sdBuf;  
memset(&sdBuf, 0, sizeof(sdBuf));  
sdBuf.lpDescription = _T("密码平台单机版本地服务");  
if( !ChangeServiceConfig2( hService, SERVICE_CONFIG_DESCRIPTION, &sdBuf) )  
{  
OutputDebugString( _T("Error, ChangeServiceConfig2 failed."));  
hr = E_FAIL;  
goto lblEnd;  
}  
}  
catch(...)  
{  
OutputDebugString( _T("Error, Unknown exception."));  
hr = E_FAIL;  
goto lblEnd;  
}  
lblEnd:  
if (sclLock)  
UnlockServiceDatabase(sclLock);  
if (hService)  
::CloseServiceHandle(hService);  
if (hSCM)  
::CloseServiceHandle(hSCM);  
return hr;  
}   
};CStestModule _AtlModule;//
extern "C" int WINAPI _tWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, 
                                LPTSTR /*lpCmdLine*/, int nShowCmd)
{
    return _AtlModule.WinMain(nShowCmd);
}也没有成功或者失败的MessageBox弹出来 为何啊?
跪求高手指点迷精