头文件包含:
#include <Windows.h>
#include <WinBase.h>
代码实现:
QString strFileMap = "onlyonevideomanager";
TCHAR tmpsrcpath[1024];
ZeroMemory(tmpsrcpath, 1024*sizeof(TCHAR));
int iSrcLength = strFileMap.toWCharArray(tmpsrcpath);
tmpsrcpath[iSrcLength+0] = TCHAR('\0');
QString strWarnText = QString::fromLocal8Bit("VideoManager已经运行!");
TCHAR tmpWarnText[1024];
ZeroMemory(tmpWarnText, 1024*sizeof(TCHAR));
iSrcLength = strWarnText.toWCharArray(tmpWarnText);
tmpWarnText[iSrcLength+0] = TCHAR('\0');
QString strWarn = "Error";
TCHAR tmpWarn[1024];
ZeroMemory(tmpWarn, 1024*sizeof(TCHAR));
iSrcLength = strWarn.toWCharArray(tmpWarn);
tmpWarn[iSrcLength+0] = TCHAR('\0');
HANDLE hMap=CreateFileMapping((HANDLE)0xffffffff,NULL,
PAGE_READWRITE,0,128,tmpsrcpath);
if(hMap==NULL)
{
return 1;
}
else if((GetLastError()==ERROR_ALREADY_EXISTS))
{
CloseHandle(hMap);
MessageBox(NULL,tmpWarnText,tmpWarn,MB_OK|MB_ICONSTOP);
return 1;
}