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

隨筆 - 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://m.shnenglu.com/citywanderer/articles/8716.html
posted on 2009-10-01 22:51 鷹擊長空 閱讀(1621) 評論(0)  編輯 收藏 引用
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            免费成人美女女| 欧美一区二区视频在线| 99riav1国产精品视频| 亚洲国产日韩欧美在线99| 在线一区免费观看| 久久久久久久999精品视频| 久久精品二区| 欧美成人午夜激情在线| 欧美国产三区| 欧美老女人xx| 国产精品一区二区在线观看不卡 | 最新亚洲电影| 91久久精品一区二区三区| 亚洲理论电影网| 亚洲自拍电影| 麻豆精品在线观看| 国产精品美女久久福利网站| 一区二区在线观看视频在线观看| 日韩午夜在线| 久久青青草综合| 亚洲精品裸体| 久久精品人人爽| 欧美日韩国产精品成人| 韩国欧美一区| 亚洲免费视频网站| 欧美国产一区二区| 午夜精品福利一区二区三区av| 免费久久精品视频| 国产精品一区二区久久| 久久精品国产免费| 亚洲激情综合| 欧美视频日韩| 亚洲第一网站| 欧美在线视频网站| 亚洲精品欧洲精品| 久久亚洲欧美| 国产欧美日韩视频在线观看| 亚洲一区二区三| 亚洲乱码视频| 99re在线精品| 欧美不卡三区| 在线观看91精品国产入口| 亚洲一区二区在线免费观看视频| 欧美v国产在线一区二区三区| 亚洲校园激情| 欧美亚洲不卡| 亚洲一区三区电影在线观看| 欧美成人小视频| 欧美一区三区三区高中清蜜桃| 欧美日韩国产va另类| 国产日韩视频| 在线亚洲精品| 欧美福利在线| 欧美www视频| 国产一区二区三区四区在线观看| 亚洲人成在线观看网站高清| 老鸭窝91久久精品色噜噜导演| 一区二区三区久久久| 欧美精品一区二区三区蜜桃| 亚洲成色www8888| 欧美伊久线香蕉线新在线| 国产精品久久77777| 亚洲片在线观看| 久久免费视频在线观看| 国产一区二区三区久久久| 欧美日韩在线免费视频| 国产一区二区三区久久| 久久免费偷拍视频| 久久精彩免费视频| 极品av少妇一区二区| 久久这里只有| 欧美bbbxxxxx| 亚洲午夜视频在线| 亚洲欧美综合国产精品一区| 国内伊人久久久久久网站视频 | 欧美高清不卡| 欧美国产在线电影| 亚洲午夜精品一区二区| 欧美日韩视频第一区| 久久久www成人免费毛片麻豆| 国内偷自视频区视频综合| 亚洲欧美日韩网| 亚洲国产第一| 国产日韩精品综合网站| 国产精品久久77777| 欧美视频二区36p| 99re66热这里只有精品4| 亚洲开发第一视频在线播放| 欧美怡红院视频一区二区三区| 欧美资源在线| 日韩一级不卡| 裸体丰满少妇做受久久99精品| 激情综合色丁香一区二区| 免费亚洲网站| 欧美午夜视频网站| 欧美一区1区三区3区公司| 久久久久国产精品厨房| 99精品欧美一区二区三区 | 在线观看91精品国产麻豆| 亚洲欧洲一区二区三区| 国产精品久久久久一区| 久久青草欧美一区二区三区| 欧美日韩亚洲成人| 免费亚洲电影| 国产欧美精品日韩精品| 欧美大色视频| 国产日韩欧美不卡| 亚洲日本欧美| 在线观看视频欧美| 亚洲视频免费在线| 亚洲欧洲日本在线| 性感少妇一区| 欧美成人蜜桃| 久久久福利视频| 欧美视频在线观看一区二区| 欧美二区视频| 激情欧美一区二区三区在线观看| 亚洲永久精品大片| 野花国产精品入口| 欧美福利在线| 欧美大尺度在线观看| 狠狠色丁香婷婷综合| 亚洲自拍三区| 在线视频欧美一区| 欧美精品粉嫩高潮一区二区| 久久色中文字幕| 国产视频久久网| 亚洲在线成人| 亚洲欧美区自拍先锋| 欧美日韩一区二区高清| 亚洲私人黄色宅男| 久久久精品视频成人| 欧美中文字幕不卡| 国产伦精品一区二区三区四区免费| 亚洲免费av电影| 99精品国产高清一区二区 | 欧美日韩国产电影| 国产精品视频久久| 亚洲一区视频| 欧美日韩国产小视频在线观看| 欧美高清免费| 亚洲国产视频a| 农夫在线精品视频免费观看| 欧美本精品男人aⅴ天堂| 亚洲第一在线视频| 免费h精品视频在线播放| 亚洲第一主播视频| 亚洲精品黄网在线观看| 欧美国产精品v| 日韩亚洲国产欧美| 午夜日本精品| 一区二区三区在线观看国产| 久久视频在线免费观看| 亚洲国产婷婷香蕉久久久久久| 日韩视频在线你懂得| 欧美日韩亚洲综合在线| 亚洲一区免费网站| 久久噜噜噜精品国产亚洲综合| 1024日韩| 极品少妇一区二区| 香蕉久久夜色| 欧美一区二区三区免费在线看| 99精品欧美一区| 最新69国产成人精品视频免费| 欧美va天堂在线| 亚洲精品一区二区三区婷婷月| 欧美日韩大片| 亚洲一区激情| 欧美高清不卡在线| 亚洲女人小视频在线观看| 国产尤物精品| 欧美另类专区| 欧美在线观看视频在线 | 国产亚洲一本大道中文在线| 久久国产综合精品| 欧美成人一区二免费视频软件| 中文精品99久久国产香蕉| 国产日韩欧美精品| 欧美暴力喷水在线| 欧美一区二区视频网站| 欧美韩日高清| 欧美一级日韩一级| 国产精品国色综合久久| 一本色道久久综合精品竹菊 | 9色精品在线| 欧美一区激情视频在线观看| 先锋影院在线亚洲| 亚洲精品欧美精品| 久久久噜噜噜久噜久久| 国产精品高潮呻吟久久| 亚洲专区一区二区三区| 欧美午夜宅男影院| 夜夜嗨av一区二区三区网站四季av| 尤物九九久久国产精品的分类| 欧美一区二区| 午夜在线播放视频欧美| 激情成人综合| 亚洲精品一区二区三区蜜桃久| 欧美精品自拍| 亚洲免费av网站|