编程之家
欢迎光临编程之家

首页 VB编程 VC编程 WEB开发 数据库

当前位置VC编程 >> 让程序只运行一个实例

让程序只运行一个实例

利用进程互斥很简单就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;
}
///////////////////////////////////////////////////////////

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;
}
//---------------------------------------------------------------------------

API 函数
ATOM GlobalAddAtom(

    LPCTSTR lpString  // pointer to the string to add
   );
lpString 为任意字符串(例如你的程序名字)
程序开始的时候调用,如果返回0值,证明已经运行了

去卖本《C++ Builder深度历险》里面有详细说明。
利用GetWindowText() + Application->Title
或Global Data Segement
Mutex
......
总之,方法太多了

看看我的例子吧,我已经调试过的:
   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;

方法很多:
1 用FindWindow("

2.用互斥进程,

3,用原子函数.

4,用共享段.


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

[来源:来自网络] [作者:admin] [日期:08-09-02] [热度:]

评论

发表评论

VB编程

VB编程 程序员可在这儿学到一些不错的VB编程知识。

进入VB编程

VC编程

WEB开发

数据库

© 2006-2008 viewarticle.com All rights reserved.
联系我们