Window Subclassing终止外部应用程序

时间:2015-01-02 18:35:51

标签: dll window code-injection subclassing

我正在尝试子类化外部应用程序的菜单。我使用SetWindowLong函数来子类化Windows程序,它运行良好,但在做了我想做的事后,它终止了应用程序,我不知道为什么。

这是我注入的功能:

DWORD __stdcall AddNewMenuItem(){

  HWND hwndAna = NULL;
  HMENU hMenu = NULL;

  hwndAna = FindWindowA("NameOfTheMainWindow,NULL);


  if(hwndAna != INVALID_HANDLE_VALUE){
    hMenu = GetMenu(hwndAna);

     if(hMenu != NULL){

    HMENU hMenuFile = CreatePopupMenu();
    AppendMenu(hMenuFile, MF_STRING , (UINT_PTR)ID_Load , L"Show Info");
    AppendMenu(hMenuFile, MF_STRING , (UINT_PTR)ID_VWS , L"Go to Site");
    AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMenuFile, L"Site");

    DrawMenuBar(hwndAna);
    OriginalProcedure = SetWindowLongA(hwndAna, GWL_WNDPROC, (LONG)MyProc);
      return 1;
    }
  }

  return 0;
}

这是我自己的Windows程序:

LRESULT APIENTRY MyProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam){
  switch(iMsg)
  {
     case WM_COMMAND:
         switch LOWORD(wParam)
         {
            case ID_Load:

            MessageBoxA(NULL,"this is info", "Information!", MB_OK|MB_ICONINFORMATION);
            break;  

            case ID_VWS:
            ShellExecute(NULL, L"Open", L"http://www.gotowebsite.com/", NULL, NULL, SW_SHOWNORMAL);
            break;

            }
    break;
   }

    return CallWindowProc((WNDPROC)OriginalProcedure, hwnd, iMsg, wParam, lParam);  

}

这些是ID:

#define ID_VWS 0x8801
#define ID_Load 0x8802

所有代码都对消息做出反应,我的意思是它成功地附加了菜单,当我点击它显示消息框等的子菜单时,但在此之后,它终止了应用程序。不知何故,它在ID消息后返回WM_CLOSE消息,我无法管理它。感谢您的帮助。

0 个答案:

没有答案