|
프로그램이 여러번 실행되는 것을 막기위해서 아래와 같이 프로그래밍했습니다.
프로그램이 이미 실행되어 있으면 파라미터를 PostMessage에서 HWND_BROADCAST 옵션을 주어서
기존 실행 어플에 메시지를 전달했는데, 문제는 똑같은 메시지가 꼭 16번씩 들어온다는 거죠.
디버그를 돌려서 보니까 Msg.hwnd 값만 바뀌면서 똑같은 Msg.message 값으로 정확하게 16번 들어옵니다. 왜 그런지 도대체 모르겠네요.
꼭 좀 봐 주세요. 소스는 아래에 첨부합니다.
그럼, 즐거운 주말 되시길... 샬롬~
// MyApp
extern UINT MsgID;
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
AnsiString AppName = "MyApp";
HANDLE hMutex;
MsgID = RegisterWindowMessage(AppName.c_str());
hMutex = CreateMutex(NULL, true, AppName.c_str());
if (hMutex != 0)
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
if (!ParamStr(1).IsEmpty())
{
ATOM Atom = GlobalAddAtom(ParamStr(1).c_str());
PostMessage(HWND_BROADCAST, MsgID, Atom, 0);
}
ReleaseMutex(hMutex);
hMutex = 0;
Application->Terminate();
return 0;
}
}
ReleaseMutex(hMutex);
Application->Initialize();
Application->Title = AppName;
Application->CreateForm(__classid(TFormMain), &FormMain);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
}
// Main.cpp
UINT MsgID;
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
Application->OnMessage = AppMessage;
}
void __fastcall TFormMain::AppMessage(tagMSG &Msg, bool &Handled)
{
char Atom[MAX_PATH];
if (Msg.message == MsgID)
{
GlobalGetAtomName(Msg.wParam, Atom, MAX_PATH);
GlobalDeleteAtom(Msg.wParam);
AnsiString sData = String(Atom);
if (!sData.IsEmpty())
ShowMessage(sData);
Handled = true;
}
}
|