首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

问一下关于使用CreateMutex,该怎么处理

2012-02-04 
问一下关于使用CreateMutex如果使用HANDLEdlgHandle::CreateMutex(NULL,FALSE,_T( ProcessTest.exe ))

问一下关于使用CreateMutex
如果使用
HANDLE   dlgHandle   =   ::CreateMutex(   NULL,   FALSE,   _T( "ProcessTest.exe "));
我可以查询指定的进程是否已经存在,
那么:有没有简单的办法,利用它返回的一些东西来直接关掉当前的这个应用程序。

我知道可用遍历进程表来关掉指定的进程,只是不知道如何利用好上面这个函数。

[解决办法]
The CreateMutex function creates or opens a named or unnamed mutex object.

HANDLE CreateMutex(
LPSECURITY_ATTRIBUTES lpMutexAttributes,
BOOL bInitialOwner,
LPCTSTR lpName
);

Return Values
If the function succeeds, the return value is a handle to the newly created mutex object.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

If the mutex is a named mutex and the object existed before this function call, the return value is a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS. However, if the second caller has limited access rights, the function will fail with ERROR_ACCESS_DENIED and the caller should use the OpenMutex function.

你不可能从返回来的Handle得到创建Mutex的进程的信息。所以没有什么简单的方法。
还是照着老路走吧。
[解决办法]
第一,不一定非得什么事都在OnInitDialog里做.其实那样做很不好.以前我也这样做过.MFC对话框程序的话可以在App类的InitInstance中处理这类操作.
第二,如果你在OnInitDialog想销毁对话框,应该先调用EndDialog然后再返回TRUE.MS规定除非你要把焦点放到对话框的其他控件上.否则对WM_INITDILOG必须返回TRUE.
[解决办法]
在initializedialog中,用return还是会显示界面的,用enddialog或者exit
[解决办法]
建议搂主把OnInitDialog()中的那段代码移到App类的InitInstance()中,如果发现已经有了这个进程,就return FALSE;即可

热点排行