各位老大请问,如何让程序只运行一个实例。

litong_master 2002-08-05 11:51:33
我是用本地数据库来控制的
但是效果不太好
...全文
29 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
纪俊 2003-02-12
  • 打赏
  • 举报
回复
以上方法都可以
我用的是互斥
Bird1945 2002-08-05
  • 打赏
  • 举报
回复
方法很多:
1 用FindWindow("

2.用互斥进程,

3,用原子函数.

4,用共享段.


大家有没有用共享段的例子啊??
好象VC里可以,BCB不支持.


xiaoguoru 2002-08-05
  • 打赏
  • 举报
回复
看看我的例子吧,我已经调试过的:
TRegistry *Registry=new TRegistry;
Registry->RootKey=HKEY_LOCAL_MACHINE;
Registry->OpenKey("Software\\Inbetter\\remember",TRUE);
if(Registry->ReadString("RunFlag")=="Runing")
{
HWND hnd;
hnd=FindWindow("TfrmMain",(Registry->ReadString("Caption")).c_str());
if(hnd)
{
Application->MessageBox("媒体管理器已经运行","警告!",MB_OK+MB_ICONWARNING);
SetForegroundWindow(hnd);
ShowWindow(hnd,SW_SHOW);
return 0 ;
}
else
{
int button=Application->MessageBox("上次没有正常退出,要继续运行吗?","提示",MB_YESNO+MB_ICONINFORMATION);
if(button!=IDYES)
return 0;
}
}
else
{
Registry->WriteString("RunFlag","Runing");
Registry->WriteString("Caption","媒体管理器");
}
delete Registry;
try
{
Application->Initialize();

Application->Title = "媒体管理器";
Application->HelpFile = "D:\\程序\\MyProgram\\pHelp.chm";
Application->CreateForm(__classid(TfrmMain), &frmMain);
Application->CreateForm(__classid(TfrmPlayer), &frmPlayer);

Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
litong_master 2002-08-05
  • 打赏
  • 举报
回复
问题解决了 谢谢大家
litong_master 2002-08-05
  • 打赏
  • 举报
回复
问题解决了
detrox 2002-08-05
  • 打赏
  • 举报
回复
去卖本《C++ Builder深度历险》里面有详细说明。
利用GetWindowText() + Application->Title
或Global Data Segement
Mutex
......
总之,方法太多了
佣工7001 2002-08-05
  • 打赏
  • 举报
回复
API 函数
ATOM GlobalAddAtom(

LPCTSTR lpString // pointer to the string to add
);
lpString 为任意字符串(例如你的程序名字)
程序开始的时候调用,如果返回0值,证明已经运行了
Cbfan 2002-08-05
  • 打赏
  • 举报
回复
Project1.cpp中:

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
HANDLE runonce = CreateMutex ( NULL, false, "Form1" );
if ( WaitForSingleObject ( runonce, 20 ) != WAIT_OBJECT_0 )
{
Application->Terminate();
exit(0);
}

Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
//---------------------------------------------------------------------------
superaf 2002-08-05
  • 打赏
  • 举报
回复
不需要那样,利用进程互斥很简单就ok了。
HANDLE m_Mutex;
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
m_Mutex=CreateMutex(NULL,false,"zuoxi");
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
MessageBox(Application->Handle,"该软件已经运行了","警告信息",MB_OK+MB_ICONWARNING);
return 0;
}
。。。。。
。。。。。
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
ReleaseMutex(m_Mutex);
return 0;
}

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧