青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆 - 42  文章 - 3  trackbacks - 0
<2009年10月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用鏈接

留言簿(2)

隨筆檔案

文章檔案

網頁收藏

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

例子是一個mfc的對話框,用vc調試器查看了一個程序從生成初始化到接受消息的流程。從產生到結束的基本流程是這樣的:
KERNEL32->WinMainCRTStartup()->_tWinMain(開始)->AfxWinMain(開始)->AfxGetThread()->AfxWinInit()->InitApplication()->InitInstance()->DoModal()->RunModalLoop()->ExitInstance()->AfxWinMain(結束)->_tWinMain(結束)

1、KERNEL32
kernel32.dll是Windows9x/Me中非常重要的32位動態鏈接庫文件,屬于內核級文件。它控制著系統的內存管理、數據的輸入輸出操作和中斷處理,當Windows啟動時,kernel32.dll就駐留在內存中特定的寫保護區域,使別的程序無法占用這個內存區域。

2、WinMainCRTStartup()函數
  程序默認的基地址(EXE文件默認為0x400000,DLL默認為x10000000),操作系統裝載一個程序時總是試著先從這個基地址開始。一般Win32的程序,EXE的入口為WinMain,DLL的入口為DllEntryPoint。默認情況下,通過一個C的運行時庫函數來實現:控制臺程序采用mainCRTStartup (或wmainCRTStartup)去調用程序的main (或wmain)函數;Windows程序采用WinMainCRTStartup (或 wWinMainCRTStartup)調用程序的WinMain (或 wWinMain,必須采用__stdcall調用約定);DLL采用_DllMainCRTStartup調用DllMain函數(必須采用__stdcall調用約定)。
它負責:
  * 檢索指向新進程的完整命令行指針;
  * 檢索指向新進程的環境變量的指針;
  * 對c/c++運行時的全局變量進行初始化;
  * 對c運行期的內存單元分配函數(比如malloc,calloc)和其他低層I/O例程使用的內存棧     進行初始化。
  * 為C++的全局和靜態類調用構造函數。
  當這些初始化工作完成后,該啟動函數就調用wWinMain函數進入應用程序的執行。
當wWinMain函數執行完畢返回時,wWinMainCRTStartup啟動函數就調用c運行期的exit()函
數,將返回值(nMainRetVal)傳遞給它。
  之后exit()便開始收尾工作:
  * 調用由_onexit()函數調用和注冊的任何函數。
  * 為C++的全局和靜態類調用析構函數;
  * 調用操作系統的ExitProcess函數,將nMainRetVal傳遞給它,這使得操作系統能夠撤銷     進程并設置它的exit  代碼。
最小體積的win32程序:(不要編譯缺省庫)
#pragma comment (linker, "/SUBSYSTEM:WINDOWS")
#pragma comment (linker, "/NODEFAULTLIB")
int WinMainCRTStartup()
{
 return 0;
}

3、WinMain()函數
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 LPTSTR lpCmdLine, int nCmdShow)
{
 // call shared/exported WinMain
 return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}

4、AfxWinMain()函數
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 LPTSTR lpCmdLine, int nCmdShow)
{
 ASSERT(hPrevInstance == NULL);

 int nReturnCode = -1;
 CWinThread* pThread = AfxGetThread();
 CWinApp* pApp = AfxGetApp();

 // AFX internal initialization
 if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
  goto InitFailure;

 // App global initializations (rare)
 if (pApp != NULL && !pApp->InitApplication())
  goto InitFailure;

 // Perform specific initializations
 if (!pThread->InitInstance())
 {
  if (pThread->m_pMainWnd != NULL)
  {
   TRACE0("Warning: Destroying non-NULL m_pMainWnd\n");
   pThread->m_pMainWnd->DestroyWindow();
  }
  nReturnCode = pThread->ExitInstance();
  goto InitFailure;
 }
 nReturnCode = pThread->Run();

InitFailure:
#ifdef _DEBUG
 // Check for missing AfxLockTempMap calls
 if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
 {
  TRACE1("Warning: Temp map lock count non-zero (%ld).\n",
   AfxGetModuleThreadState()->m_nTempMapLock);
 }
 AfxLockTempMaps();
 AfxUnlockTempMaps(-1);
#endif

 AfxWinTerm();
 return nReturnCode;
}

5、AfxGetThread()函數
CWinThread* AFXAPI AfxGetThread()
{
 // check for current thread in module thread state
 AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
 CWinThread* pThread = pState->m_pCurrentWinThread;

 // if no CWinThread for the module, then use the global app
 if (pThread == NULL)
  pThread = AfxGetApp();

 return pThread;
}

6、AfxWinInit()函數
BOOL AFXAPI AfxWinInit(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 LPTSTR lpCmdLine, int nCmdShow)
{
 ASSERT(hPrevInstance == NULL);

 // handle critical errors and avoid Windows message boxes
 SetErrorMode(SetErrorMode(0) |
  SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX);

 // set resource handles
 AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
 pModuleState->m_hCurrentInstanceHandle = hInstance;
 pModuleState->m_hCurrentResourceHandle = hInstance;

 // fill in the initial state for the application
 CWinApp* pApp = AfxGetApp();
 if (pApp != NULL)
 {
  // Windows specific initialization (not done if no CWinApp)
  pApp->m_hInstance = hInstance;
  pApp->m_hPrevInstance = hPrevInstance;
  pApp->m_lpCmdLine = lpCmdLine;
  pApp->m_nCmdShow = nCmdShow;
  pApp->SetCurrentHandles();
 }

 // initialize thread specific data (for main thread)
 if (!afxContextIsDLL)
  AfxInitThread();

 return TRUE;
}

7、InitApplication() 函數
BOOL CMy1App::InitApplication()
{
 // TODO: Add your specialized code here and/or call the base class
 AfxMessageBox("InitApplication");
 return CWinApp::InitApplication();
}
BOOL CWinApp::InitApplication()
{
 if (CDocManager::pStaticDocManager != NULL)
 {
  if (m_pDocManager == NULL)
   m_pDocManager = CDocManager::pStaticDocManager;
  CDocManager::pStaticDocManager = NULL;
 }

 if (m_pDocManager != NULL)
  m_pDocManager->AddDocTemplate(NULL);
 else
  CDocManager::bStaticInit = FALSE;

 return TRUE;
}

8、InitInstance()函數
AfxWinMain函數里面的if (!pThread->InitInstance())會調用程序CMy1App的InitInstance函數:
BOOL CMy1App::InitInstance()
{
 AfxEnableControlContainer();
AfxMessageBox("InitInstance");
 // Standard initialization
 // If you are not using these features and wish to reduce the size
 //  of your final executable, you should remove from the following
 //  the specific initialization routines you do not need.

#ifdef _AFXDLL
 Enable3dControls();   // Call this when using MFC in a shared DLL
#else
 Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

 CMy1Dlg dlg;//調用CMy1Dlg的構造函數
 m_pMainWnd = &dlg;
 int nResponse = dlg.DoModal();
 if (nResponse == IDOK)
 {
  // TODO: Place code here to handle when the dialog is
  //  dismissed with OK
 }
 else if (nResponse == IDCANCEL)
 {
  // TODO: Place code here to handle when the dialog is
  //  dismissed with Cancel
 }

 // Since the dialog has been closed, return FALSE so that we exit the
 //  application, rather than start the application's message pump.
 return FALSE;
}

9、AfxWinMain()函數里面的主要的循環體,在這里:
        if (!pThread->InitInstance())//主要是模式對話框調用,對話框關閉以后InitInstance函數才會結束
 {
  if (pThread->m_pMainWnd != NULL)
  {
   TRACE0("Warning: Destroying non-NULL m_pMainWnd\n");
   pThread->m_pMainWnd->DestroyWindow();
  }
  nReturnCode = pThread->ExitInstance();
  goto InitFailure;
 }
 nReturnCode = pThread->Run();//非模式對話框和一般程序調用這個循環

InitInstance函數調用了dlg.DoModal()函數,以下是DoModal()函數:
int CDialog::DoModal()
{
 // can be constructed with a resource template or InitModalIndirect
 ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
  m_lpDialogTemplate != NULL);

 // load resource as necessary
 LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
 HGLOBAL hDialogTemplate = m_hDialogTemplate;
 HINSTANCE hInst = AfxGetResourceHandle();
 if (m_lpszTemplateName != NULL)
 {
  hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
  HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
  hDialogTemplate = LoadResource(hInst, hResource);
 }
 if (hDialogTemplate != NULL)
  lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);

 // return -1 in case of failure to load the dialog template resource
 if (lpDialogTemplate == NULL)
  return -1;

 // disable parent (before creating dialog)
 HWND hWndParent = PreModal();
 AfxUnhookWindowCreate();
 BOOL bEnableParent = FALSE;
 if (hWndParent != NULL && ::IsWindowEnabled(hWndParent))
 {
  ::EnableWindow(hWndParent, FALSE);
  bEnableParent = TRUE;
 }

 TRY
 {
  // create modeless dialog
  AfxHookWindowCreate(this);
  if (CreateDlgIndirect(lpDialogTemplate,
      CWnd::FromHandle(hWndParent), hInst))//創建對話框的窗口
  {
   if (m_nFlags & WF_CONTINUEMODAL)
   {
    // enter modal loop
    DWORD dwFlags = MLF_SHOWONIDLE;
    if (GetStyle() & DS_NOIDLEMSG)
     dwFlags |= MLF_NOIDLEMSG;
    VERIFY(RunModalLoop(dwFlags) == m_nModalResult);//這里是真正的循環RunModalLoop
   }

   // hide the window before enabling the parent, etc.
   if (m_hWnd != NULL)
    SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW|
     SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
  }
 }
 CATCH_ALL(e)
 {
  DELETE_EXCEPTION(e);
  m_nModalResult = -1;
 }
 END_CATCH_ALL

 if (bEnableParent)
  ::EnableWindow(hWndParent, TRUE);
 if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)
  ::SetActiveWindow(hWndParent);

 // destroy modal window
 DestroyWindow();
 PostModal();

 // unlock/free resources as necessary
 if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL)
  UnlockResource(hDialogTemplate);
 if (m_lpszTemplateName != NULL)
  FreeResource(hDialogTemplate);

 return m_nModalResult;
}

模式對話框調用的循環函數RunModalLoop()函數如下:
int CWnd::RunModalLoop(DWORD dwFlags)
{
 ASSERT(::IsWindow(m_hWnd)); // window must be created
 ASSERT(!(m_nFlags & WF_MODALLOOP)); // window must not already be in modal state

 // for tracking the idle time state
 BOOL bIdle = TRUE;
 LONG lIdleCount = 0;
 BOOL bShowIdle = (dwFlags & MLF_SHOWONIDLE) && !(GetStyle() & WS_VISIBLE);
 HWND hWndParent = ::GetParent(m_hWnd);
 m_nFlags |= (WF_MODALLOOP|WF_CONTINUEMODAL);
 MSG* pMsg = &AfxGetThread()->m_msgCur;

 // acquire and dispatch messages until the modal state is done
 for (;;)
 {
  ASSERT(ContinueModal());

  // phase1: check to see if we can do idle work
  while (bIdle &&
   !::PeekMessage(pMsg, NULL, NULL, NULL, PM_NOREMOVE))
  {
   ASSERT(ContinueModal());

   // show the dialog when the message queue goes idle
   if (bShowIdle)
   {
    ShowWindow(SW_SHOWNORMAL);
    UpdateWindow();
    bShowIdle = FALSE;
   }

   // call OnIdle while in bIdle state
   if (!(dwFlags & MLF_NOIDLEMSG) && hWndParent != NULL && lIdleCount == 0)
   {
    // send WM_ENTERIDLE to the parent
    ::SendMessage(hWndParent, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)m_hWnd);
   }
   if ((dwFlags & MLF_NOKICKIDLE) ||
    !SendMessage(WM_KICKIDLE, MSGF_DIALOGBOX, lIdleCount++))
   {
    // stop idle processing next time
    bIdle = FALSE;
   }
  }

  // phase2: pump messages while available
  do
  {
   ASSERT(ContinueModal());

   // pump message, but quit on WM_QUIT
   if (!AfxGetThread()->PumpMessage())//主要在這里循環
   {
    AfxPostQuitMessage(0);
    return -1;
   }

   // show the window when certain special messages rec'd
   if (bShowIdle &&
    (pMsg->message == 0x118 || pMsg->message == WM_SYSKEYDOWN))
   {
    ShowWindow(SW_SHOWNORMAL);
    UpdateWindow();
    bShowIdle = FALSE;
   }

   if (!ContinueModal())
    goto ExitModal;

   // reset "no idle" state after pumping "normal" message
   if (AfxGetThread()->IsIdleMessage(pMsg))
   {
    bIdle = TRUE;
    lIdleCount = 0;
   }

  } while (::PeekMessage(pMsg, NULL, NULL, NULL, PM_NOREMOVE));
 }

ExitModal:
 m_nFlags &= ~(WF_MODALLOOP|WF_CONTINUEMODAL);
 return m_nModalResult;
}

一般程序調用的循環函數Run函數如下:
int CWinThread::Run()
{
 ASSERT_VALID(this);

 // for tracking the idle time state
 BOOL bIdle = TRUE;
 LONG lIdleCount = 0;

 // acquire and dispatch messages until a WM_QUIT message is received.
 for (;;)
 {
  // phase1: check to see if we can do idle work
  while (bIdle &&
   !::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE))
  {
   // call OnIdle while in bIdle state
   if (!OnIdle(lIdleCount++))
    bIdle = FALSE; // assume "no idle" state
  }

  // phase2: pump messages while available
  do
  {
   // pump message, but quit on WM_QUIT
   if (!PumpMessage())
    return ExitInstance();

   // reset "no idle" state after pumping "normal" message
   if (IsIdleMessage(&m_msgCur))
   {
    bIdle = TRUE;
    lIdleCount = 0;
   }

  } while (::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE));
 }

 ASSERT(FALSE);  // not reachable
}

==========
我認為是更加完整的應該是_DllMainCRTStartup-> WinMainCRTStartup(void)->()->_tWinMain(開始)->AfxWinMain(開始)->AfxGetThread()->AfxWinInit()->InitApplication()->InitInstance()->DoModal()->RunModalLoop()->ExitInstance()->AfxWinMain(結束)->_tWinMain(結束)

參考http://www.shnenglu.com/citywanderer/articles/8716.html
posted on 2009-10-01 22:51 鷹擊長空 閱讀(1629) 評論(0)  編輯 收藏 引用

只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美成人a视频| 欧美激情精品久久久久久黑人| 久久精品男女| 亚洲欧美精品在线| 国产一区二区三区观看| 久久久久久国产精品mv| 久久久久国产精品www| 亚洲国产va精品久久久不卡综合| 国产亚洲欧美一区在线观看| 午夜天堂精品久久久久| 亚洲一区二区三区激情| 国产精品久久久久久户外露出| 国产午夜精品视频| 久久精品视频免费播放| 久久噜噜亚洲综合| 一区二区激情视频| 午夜精品成人在线视频| 亚洲国产精品t66y| 99国产精品视频免费观看| 国产原创一区二区| 欧美黄色一区二区| 国产精品日韩欧美一区| 久久综合国产精品台湾中文娱乐网 | 欧美精品粉嫩高潮一区二区| 一区二区三区毛片| 亚洲欧美国产精品专区久久| 亚洲第一福利视频| 一道本一区二区| 一区二区在线观看av| 日韩视频一区| 精品91久久久久| 在线综合亚洲| 在线观看精品| 一区二区三区免费观看| 亚洲二区在线| 亚洲欧美日韩久久精品| 99国产精品久久久久久久| 欧美亚洲一区二区在线| 一区二区欧美激情| 久久久久久久91| 亚洲一区二区精品在线| 麻豆国产精品777777在线| 亚洲欧美另类在线观看| 欧美韩日一区二区| 久久亚洲免费| 国产日韩av一区二区| 亚洲精品一二三| 亚洲国产一区在线观看| 久久xxxx精品视频| 欧美在线看片a免费观看| 欧美精品三级在线观看| 亚洲风情在线资源站| 国产精品午夜国产小视频| 亚洲精品欧美日韩| 亚洲黄色免费| 欧美va天堂| 欧美大片免费久久精品三p| 激情久久中文字幕| 久久国产乱子精品免费女 | 久久久久久久久久看片| 亚洲欧美日韩视频二区| 欧美日韩另类视频| 欧美激情一区二区三区在线视频| 欧美精品色综合| 欧美黑人多人双交| 亚洲春色另类小说| 久久蜜桃香蕉精品一区二区三区| 亚洲国产一区二区a毛片| 久久久久久久高潮| 久久只精品国产| 亚洲高清影视| 欧美极品在线播放| 亚洲精品韩国| 亚洲欧美国产制服动漫| 国产精品视频精品| 欧美亚洲视频| 免费高清在线一区| 在线免费日韩片| 欧美电影美腿模特1979在线看| 亚洲已满18点击进入久久| 国产精品久久波多野结衣| 99亚洲视频| 欧美一区在线直播| 伊人久久婷婷| 欧美成人一品| 在线视频欧美日韩| 久久精品国产一区二区三区| 国产小视频国产精品| 久久精品一区二区三区不卡牛牛 | 国产性猛交xxxx免费看久久| 欧美一区二区三区另类| 欧美激情免费观看| 99精品欧美一区| 国产精品大全| 久久先锋资源| 中文在线资源观看网站视频免费不卡 | 日韩天堂在线观看| 亚洲欧美成人精品| 国内精品模特av私拍在线观看| 亚洲精品影视| 欧美中文在线免费| 91久久在线| 国产精品国产三级欧美二区| 久久国产黑丝| 日韩一级二级三级| 久久久久久久久久久久久女国产乱| 欧美精品一区二区三区蜜桃| 亚洲中字黄色| 亚洲国产成人精品女人久久久| 国产精品美女久久| 久久免费视频网站| 一本久道久久综合中文字幕| 久久中文久久字幕| 亚洲欧美日韩成人| 亚洲人成小说网站色在线| 国产欧美日韩| 欧美激情第10页| 欧美一区二区三区在线看| 亚洲乱码国产乱码精品精可以看 | 99在线观看免费视频精品观看| 久久久精品2019中文字幕神马| 亚洲欧美日本日韩| 亚洲日本va午夜在线电影| 国产欧美日韩高清| 欧美午夜激情视频| 欧美另类女人| 蜜臀91精品一区二区三区| 亚洲欧美日韩视频二区| 一区二区三区高清在线| 亚洲国产你懂的| 你懂的成人av| 欧美综合77777色婷婷| 亚洲一区二区三区色| 夜夜嗨av一区二区三区网站四季av | 亚洲欧美激情四射在线日| 女女同性精品视频| 久久久久久久一区二区三区| 欧美亚洲综合网| 西西裸体人体做爰大胆久久久| 国产精品日日摸夜夜添夜夜av | 久久精品日产第一区二区三区| 欧美视频在线观看 亚洲欧| 欧美1区2区| 麻豆久久精品| 老司机精品视频一区二区三区| 亚洲精品1234| 91久久线看在观草草青青| 欧美激情成人在线| 欧美电影免费观看大全| 欧美 日韩 国产 一区| 你懂的成人av| 亚洲精品久久久蜜桃| 亚洲精品一级| 亚洲一品av免费观看| 亚洲欧美中文日韩在线| 久久国产欧美日韩精品| 久久午夜精品| 欧美精品一区二区三区蜜桃| 欧美日韩高清在线一区| 国产精品va在线播放| 国产精品日韩欧美| 韩国av一区| 亚洲国产日韩欧美在线99 | 欧美日韩一二区| 欧美午夜精品久久久久久浪潮| 欧美一区二区三区日韩| 久久久中精品2020中文| 欧美成人激情在线| 国产精品va| 国内成+人亚洲+欧美+综合在线| 另类天堂av| 欧美日韩国产综合在线| 国产精品美腿一区在线看| 国产偷国产偷精品高清尤物| 国产一区二区按摩在线观看| 亚洲激情视频在线播放| 亚洲在线视频一区| 久久久久se| 亚洲三级国产| 欧美在线高清视频| 欧美激情精品久久久| 国产欧美日韩伦理| 日韩亚洲一区二区| 欧美一区影院| 亚洲精品日韩在线观看| 午夜精品一区二区三区在线视 | 狼人社综合社区| 亚洲精品国产欧美| 性欧美18~19sex高清播放| 欧美va亚洲va国产综合| 国产精品免费网站| 在线免费观看一区二区三区| 亚洲亚洲精品在线观看| 免费一级欧美片在线播放| 夜夜精品视频| 欧美高清视频在线播放| 国产亚洲欧美另类中文| 亚洲午夜国产成人av电影男同| 99精品视频免费全部在线| 久久久国产亚洲精品|