单实例程序方法
来源:互联网 发布:赛尔号胶囊玩具淘宝 编辑:程序博客网 时间:2024/06/06 05:17
单实例程序
这种情况是打开同类型的文件,然后不想再开一个程序的实例,最好就是让它单实例,然后把同类型文件通过消息传给程序的第一个实例。
办法:
最早看的文章是使用winmain中的参数hPrevInstance,后来在32位中这个参数一直为0,所以不能用了。
好的解决办法是使用mutex互斥。
步骤:
1.首先尝试打开mutex,如果不存在这个mutex,那么就是第一个实例。
2。创建mutex,如果它不存在
3. 在程序返回后,然后释放掉release mutex。
4.如果mutex存在,这表明是程序的第二个实例,终止该实例通过winmain()函数的return。
然后将改程序放在前台,然后终止。
传数据到初始化实例
解决方法是:使用WM_COPYDATA消息
使用WM_COPYDATA消息
方法是使用WPARAM传入消息,LPARAM传入tagCOPYDATASTRUCT数据结构
这种情况是打开同类型的文件,然后不想再开一个程序的实例,最好就是让它单实例,然后把同类型文件通过消息传给程序的第一个实例。
办法:
最早看的文章是使用winmain中的参数hPrevInstance,后来在32位中这个参数一直为0,所以不能用了。
好的解决办法是使用mutex互斥。
步骤:
1.首先尝试打开mutex,如果不存在这个mutex,那么就是第一个实例。
2。创建mutex,如果它不存在
3. 在程序返回后,然后释放掉release mutex。
4.如果mutex存在,这表明是程序的第二个实例,终止该实例通过winmain()函数的return。
WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int){ try { // Try to open the mutex. HANDLE hMutex = OpenMutex( MUTEX_ALL_ACCESS, 0, "MyApp1.0"); if (!hMutex) // Mutex doesn’t exist. This is // the first instance so create // the mutex. hMutex = CreateMutex(0, 0, "MyApp1.0"); else // The mutex exists so this is the // the second instance so return. return 0; Application->Initialize(); Application->CreateForm( __classid(TForm1), &Form1); Application->Run(); // The app is closing so release // the mutex. ReleaseMutex(hMutex); } catch (Exception &exception) { Application-> ShowException(&exception); } return 0;}
然后将改程序放在前台,然后终止。
if (!hMutex) hMutex = CreateMutex(0, 0, "MyApp1.0");else { HWND hWnd = FindWindow( 0, "File Association Example"); SetForegroundWindow(hWnd); return 0;}
传数据到初始化实例
解决方法是:使用WM_COPYDATA消息
使用WM_COPYDATA消息
方法是使用WPARAM传入消息,LPARAM传入tagCOPYDATASTRUCT数据结构
typedef struct tagCOPYDATASTRUCT { DWORD dwData; DWORD cbData; PVOID lpData;} COPYDATASTRUCT, *PCOPYDATASTRUCT;
windows为保证消息没被处理之前,数据要保证一直存在,必须使用 SendMessage() 发WM_COPYDATA 消息. 不能用 PostMessage().if (strlen(cmdLine) != 0) { COPYDATASTRUCT cds; cds.cbData = strlen(cmdLine) + 1; cds.lpData = cmdLine; SendMessage(hWnd, WM_COPYDATA, 0, (LPARAM)&cds);}
处理WM_COPYDATA消息String S = (char*)Message.CopyDataStruct->lpData;
下边是
BCB代码#include <vcl.h>#pragma hdrstopUSERES("FileAssociation.res");USEFORM("MainU.cpp", Form1);WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR cmdLine, int){ try { // Try to open the mutex. HANDLE hMutex = OpenMutex( MUTEX_ALL_ACCESS, 0, "MyApp1.0"); // If hMutex is 0 then the mutex doesn't exist. if (!hMutex) hMutex = CreateMutex(0, 0, "MyApp1.0"); else { // This is a second instance. Bring the // original instance to the top. HWND hWnd = FindWindow( 0, "File Association Example"); SetForegroundWindow(hWnd); // Command line is not empty. Send the // command line in a WM_COPYDATA message. if (strlen(cmdLine) != 0) { COPYDATASTRUCT cds; cds.cbData = strlen(cmdLine); cds.lpData = cmdLine; SendMessage( hWnd, WM_COPYDATA, 0, (LPARAM)&cds); } return 0; } Application->Initialize(); Application->CreateForm( __classid(TForm1), &Form1); Application->Run(); ReleaseMutex(hMutex); } catch (Exception &exception) { Application->ShowException(&exception); } return 0;}
阅读全文
0 0
- 单实例程序方法
- 创建单实例程序
- 单实例运行程序
- 单实例程序
- ruby 单实例程序
- 程序单实例运行
- windows程序单实例
- JAVA程序单实例运行的实现方法
- WPF 单实例方法
- 使程序单实例运行
- 保证程序单实例运行
- WINCE程序单实例运行
- c++程序单实例运行
- c++程序单实例运行
- C++的单实例方法
- 利用CreateMutex创建单实例程序
- 单实例 程序 例子 修改hosts
- 创建专有命名空间单实例程序
- maven的安装与使用
- Myeclipse:No projects are available for deployment to this server!
- Linux 中C/C++ search path(头文件搜索路径)
- AndroidStudio添加自动解析Json数据插件
- JAVA 多态、final关键字、抽象类和方法、 day10
- 单实例程序方法
- 基于cdh570的phoenix编译
- BootStrap简介及应用要点
- UVA 11995 I Can Guess the Data Structure!<STL数据结构使用>
- 自己搭建git服务器
- 如何比较PixelCNN与DCGAN两种Image generation方法?
- VS 编译出错
- weak alias
- 安卓自定义View进阶:Path之玩出花样(PathMeasure)