#include <windows.h>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
int main(int argc, char* argv[], char* envp[])
{
HANDLE hMutex = CreateMutex(NULL, FALSE, L"ysd");
CHAR szBuffer[128];
ostream_iterator<string> output(cout, "\n");
if (GetLastError() == ERROR_ALREADY_EXISTS)
*output = string("已经有一个相同的程序在运行了");
else
*output = string("这是该程序的第一个实例!");
DWORD ProcessID = GetCurrentProcessId();
DWORD SessionID;
if (ProcessIdToSessionId(ProcessID, &SessionID))
{
sprintf_s(szBuffer, "Process '%d' runs in terminal services session '%d'", ProcessID, SessionID);
*output = string(szBuffer);
}
else
MessageBox(NULL, L"不好意思,不能得到该进程的终端服务", L"调试信息", MB_OK | MB_ICONERROR);
CloseHandle(hMutex);
hMutex = NULL;
cout << "*******************" << endl;
system("pause");
return 0;
}
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
int main(int argc, char* argv[], char* envp[])
{
HANDLE hMutex = CreateMutex(NULL, FALSE, L"ysd");
CHAR szBuffer[128];
ostream_iterator<string> output(cout, "\n");
if (GetLastError() == ERROR_ALREADY_EXISTS)
*output = string("已经有一个相同的程序在运行了");
else
*output = string("这是该程序的第一个实例!");
DWORD ProcessID = GetCurrentProcessId();
DWORD SessionID;
if (ProcessIdToSessionId(ProcessID, &SessionID))
{
sprintf_s(szBuffer, "Process '%d' runs in terminal services session '%d'", ProcessID, SessionID);
*output = string(szBuffer);
}
else
MessageBox(NULL, L"不好意思,不能得到该进程的终端服务", L"调试信息", MB_OK | MB_ICONERROR);
CloseHandle(hMutex);
hMutex = NULL;
cout << "*******************" << endl;
system("pause");
return 0;
}