两次调用CreateMutex没有产生互斥
这样是可以用的,第二次运行能弹出对话框
程序代码:int main()
{
HANDLE hMutex=CreateMutex(0,true,"four_glass");
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
MessageBox(NULL, "已有该实例", "注意:", MB_OK);
}
while(1);
return 0;
}但是这样就弹不出对话框了。
第一次成功启动以后可以在服务里看到,抓包可以看到服务开启的ping线程一直在工作。
但是第二次运行该程序,CreateMutex还是返回了一个句柄,并没有返回ERROR_ALREADY_EXISTS。
程序代码:
int main(int argc,char *argv[])
{
Register();
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ "Temp_For_Test", (LPSERVICE_MAIN_FUNCTION) ServiceMain },
{ NULL, NULL }
};
if (!StartServiceCtrlDispatcher( DispatchTable))
{
printf("error :%d\n",GetLastError());
}
while(1);
system("pause");
return 0;
void Register()
{
HANDLE hMutex=CreateMutex(0,true,"four_glass");
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
if(Debug_)
Printf_Debug('b', 1, "already running");
exit(0);
}
InstallService();//功能是创建服务,创建完成后启动服务然后用exit退出。 里面还有不明白的地方待指教
}
void InstallService(void)
{
const char ServiceName[]="Temp_For_Test";
char FilePathName[MAX_PATH];
SERVICE_STATUS_PROCESS s_Status;
SC_HANDLE h_SCManager;
SC_HANDLE h_Service;
DWORD dwBytesNeeded;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
GetModuleFileName(NULL, FilePathName, MAX_PATH); //获得自身路径
h_SCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //打开SCM
if (NULL == h_SCManager)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
exit(0);
}
else
printf("OpenSCManager succeeded\n");
//query if the service exist.
h_Service = OpenService(h_SCManager, ServiceName, SC_MANAGER_ALL_ACCESS);
if (h_Service != NULL)
{
printf("OpenService succeeded\n");
if (!QueryServiceStatusEx(h_Service, SC_STATUS_PROCESS_INFO, (LPBYTE)&s_Status, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded ) )
// handle to service // info level // address of structure // size of structure // if buffer too small
{
printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
exit(0);
}
CloseServiceHandle(h_Service);
CloseServiceHandle(h_SCManager);
return ;
}
else
{
printf("OpenService failed:%d\n",GetLastError());
}
h_Service=CreateService( //创建服务
h_SCManager, // SCM 句柄
ServiceName, // 服务名
ServiceName, // 显示的服务名
SERVICE_ALL_ACCESS, // 存取权限
SERVICE_WIN32_OWN_PROCESS, // 服务类别
SERVICE_AUTO_START, // 启动类别
SERVICE_ERROR_NORMAL, // 错误控制类别
FilePathName, // 服务的可执行文件路径
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (NULL == h_Service)
{
printf("CreateService failed (%d)\n", GetLastError());
exit(0);
}
else
printf("CreateService succeeded\n");
if(!StartService(h_Service, 0, NULL)) //启动服务
{
printf("StartService failed (%d)\n", GetLastError());
exit(0);
}
//查询状态
if (!QueryServiceStatusEx(h_Service, SC_STATUS_PROCESS_INFO, (LPBYTE)&s_Status, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded ) )
// handle to service // info level // address of structure // size of structure // if buffer too small
{
printf("QueryServiceStatusEx failed (%d)\n", GetLastError());
exit(0);
}
//这段代码没看懂,功能不知道?????????????????????????????????????
dwStartTickCount = GetTickCount();
dwOldCheckPoint = s_Status.dwCheckPoint;
// 查询状态,确定 PENDING 状态结束
while (s_Status.dwCurrentState == SERVICE_START_PENDING)
{
// 等待一段时间
dwWaitTime = s_Status.dwWaitHint / 10;
if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000;
Sleep( dwWaitTime );
// 再次查询
if (!QueryServiceStatusEx(
h_Service, // handle to service
SC_STATUS_PROCESS_INFO, // info level
(LPBYTE)&s_Status, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // if buffer too small
break;
if ( s_Status.dwCheckPoint > dwOldCheckPoint )
{
// 进程创建中
dwStartTickCount = GetTickCount();
dwOldCheckPoint = s_Status.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > s_Status.dwWaitHint)
{
// WaitHint 时间到
break;
}
}
}
if (s_Status.dwCurrentState == SERVICE_RUNNING)
printf("StartService SUCCESS.\n");
else
{
printf("\nService not started. \n");
printf(" Current State: %d\n", s_Status.dwCurrentState);
printf(" Exit Code: %d\n", s_Status.dwWin32ExitCode);
printf(" Service Specific Exit Code: %d\n",
s_Status.dwServiceSpecificExitCode);
printf(" Check Point: %d\n", s_Status.dwCheckPoint);
printf(" Wait Hint: %d\n", s_Status.dwWaitHint);
printf("error : %d\n",GetLastError());
}
CloseServiceHandle(h_Service);
CloseServiceHandle(h_SCManager);
exit(0);
}
}








