C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[24971] [질문] 도대체 왜 그러지요? - 똑같은 메시지가 16번 발생하네요?
신종태 [jshin] 839 읽음    2003-03-28 15:22
프로그램이 여러번 실행되는 것을 막기위해서 아래와 같이 프로그래밍했습니다.
프로그램이 이미 실행되어 있으면 파라미터를 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;
   }
}


+ -

관련 글 리스트
24971 [질문] 도대체 왜 그러지요? - 똑같은 메시지가 16번 발생하네요? 신종태 839 2003/03/28
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.