C++ 2013 Nov

1. Really wonderful ask about " using namespace std; "

 http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

 

Google C++ Style Guide

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

 

2.http://blog.sina.com.cn/s/blog_4d3a204e010008xe.html

 

3. Visual Studio unicode problem . 

http://stackoverflow.com/questions/10501634/warning-c4819-how-to-find-the-character-that-has-to-be-saved-in-unicode

You can use Notepad++ to find all Unicode characters in a file using a regular expression:

  1. Open your file in Notepad++.
  2. Ensure that you select UTF-8 from the Encoding menu.
  3. Open the search box (use CTRL-F or go to the Search menu and select Find...).
  4. Under Search Mode, select the radio button for Regular expression.
  5. Enter [^\x00-\x7F] in the Find what box and hit the Find Next button to see what you get.

After you find the Unicode character(s), you can remove/change them, change the encoding back to ANSI, and save the file.

You don't have to use Notepad++, of course. The RegEx will work in other text editors, e.g., Sublime Text.

 

4. How to compile  FFMpeg ?

http://blog.360converter.com/archives/825

 5. MFC why no WinMain ?

http://blog.sina.com.cn/s/blog_9ffcd5dc01014o61.html

 

C++入口函数的讨论

(2012-05-23 23:42:27)
标签:

it

分类: C/CPP

使用MFC编程的程序员刚开始都会提出这样一个问题:我的程序是从哪儿开始执行的?回答是: 从WinMain()开始执行的。提出这样的问题是由于在他们所编写的MFC应用中看不到WinMain()函数。这个函数是隐藏在MFC框架中,MFC 的设计者将它作得很通用(这主要得益于Window的消息驱动的编程机制,使得作一个通用的WinMain()很容易),因此在一般情况下,无需更改 WinMain()的代码,MFC的设计者也不提倡程序员修改WinMain()的代码。在MFC,实际实现WinMain()的代码是 AfxWinMain()函数(根据其前缀Afx就知道这是一个全局的MFC函数)。

 

  一个Win32应用程序(或进程)是由一个或多 个并发的线程组成的,其中第一个启动的线程称为主线程,在Window下,一般将线程分成两大类,界面线程和工作线程,工作线程就是一般的线程,它没有窗 口,没有消息队列等,界面线程拥有一个或多个窗口,拥有一个消息队列和其他专属于界面线程的元素。在讨论AfxWinMain()之前,首先要简略提一下 MFC中的两个重要的类,CWinThreadCWinAppCWinThread是用来封装界面线程的类,CWinApp是从CWinThread 派生而来的。在CWinThread中,有两个很重要的虚拟函数InitInstance()和ExitInistance(),MFC的程序员应该对这 两个函数应该很熟悉。在CWinApp中,增加了另外一个虚拟函数InitApplication(),讨论AfxWinMain()的主要目的是看这些 函数是如何被调用的。

  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:

       AfxWinTerm();

       return nReturnCode;

}

 

   在上面的代码中,AfxGetThread()返回的是当前界面线程对象的指针,AfxGetApp()返回的是应用程序对象的指针,如果该应用程序 (或进程)只有一个界面线程在运行,那么这两者返回的都是一个全局的应用程序对象指针,这个全局的应用程序对象就是MFC应用框架所默认的theApp对 象(每次使用AppWizard生成一个SDIMDI应用程序时,AppWizard都会添加CYourApp theApp这条语句,AfxGetApp()返回的就是这个theApp的地址)。

CWinApp::InitApplication(), CWinThread::InitInstance(), CWinThread::ExitInstance()的调用情况。

CWinThread::PumpMessage() line 848 + 30 bytes

CWnd::RunModalLoop(unsigned long 4) line 3489 + 19 bytes

CDialog::DoModal() line 539 + 12 bytes

CIntelligentFtpApp::InitInstance() line 65 + 11 bytes

AfxWinMain(HINSTANCE__ * 0x00400000, HINSTANCE__ * 0x00000000, char * 0x00151f26, int 1) line 39 + 11 bytes

 

   WinMain(HINSTANCE__ * 0x00400000, HINSTANCE__ * 0x00000000, char * 0x00151f26, int 1) line 30

WinMainCRTStartup() line 198 + 54 bytes

 KERNEL32! 7c816fd7()    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ensungm/archive/2009/10/11/4653299.aspx

MFC程序入口分析

 

先从一个最小的MFC程序说起。
// The minimal MFC program 

************************************************
#include <afxwin.h>
class CMinApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
// 
重载的InitInstance()实现
BOOL CMinApp::InitInstance ()
{
// 
分配C++窗口对象
CFrameWnd * pFrame = new CFrameWnd();
// 
创建窗口并命名标题栏,_T()是支持UNICODE之用,
// 
不过这行代码常有人把它放在CFrameWnd构造函数中
pFrame->Create( 0, _T( "A Minimal MFC Program" ) );
// 
显示窗口
pFrame->ShowWindow( SW_SHOWDEFAULT );
pFrame->UpdateWindow ();
// 
将已经指定的框架与应用程序对象建立关联
AfxGetApp()->m_pMainWnd = pFrame;
return TRUE;
}
// 
创建唯一的也是必须的应用程序对象。由于
// 
它不存在任何函数之中,所以也是全局对象。
CMinApp MyApp;
*************************************************************
作为一个最小的MFC应用程序,你只需要在代码中初始化一个CWinApp对象,并重
载其
CWinApp::InitInstance()
方法。习惯了C/C++ DOS编程和SDK编程的人可能会觉得
奇怪,
程序的入口函数main()WinMain()在哪里呢?
其实,在MFC应用程序中,MFC维护着一个WinMain()函数,只是它被隐藏了,程序
员看不见
罢了。通过你写的MFC代码,你已经建立了一个CWinApp对象,这个全局对象的初
始化工作在
WinMain()
被调用之前就已经完成。

在北京科海培训中心,清华出版社的大部头著作"MFC开发Windows 95/NT4应用程
"
(Peter Norton, Rob McGregor, 1996)
75页上说:"当声明应用程序对象时,应用程序运行,因为应用程序类的构造函数调用Run()方法,它如同在CWinApp MyApp内一样自动启动应用程序的消息泵。其意思是说,CMinApp MyApp这句代码调用CMinApp::CMinApp(),而这个构造函数导致程序的运行。这是错误的!如果是构造函数调用WinMain(),而WinMain()有调用CMinApp::InitInstance(), 那么就是说在执行InitInstance()时构造函数
的工作还没完成,那么显然MyApp这个应用程序对象也就还不存在。试问在CMinApp::InitInstance()中既然可以用AfxGetApp()获得指向一个应用程序对象的指针,那么这个应用程序对象必然是已经初始化好了。如果初始化还在进行中,InitInstance()怎么运行呢?所以,程序不是在CMinApp MyApp时运行,而是在其之后。换句话说,WinMain()不是在CMinApp MyApp这行语句中被隐含调用,
而是在其后由MFC自动调用的。
有时大师也会犯低级错误。
下面我将逐行解释MFCWinMain(),这段代码在VC98目录下的MFC\SRC\WinMain.
cpp
中。
请记住,在这段代码能够被执行之前,我们已经有了一个CWinApp类全局对象。而
且请注意,CWinApp类是CWinThread类的派生类,而且当前的CWinApp类全局对象(这个对象有且只能有一个)能够通过调用全局函数AfxGetApp()来得到指向其的指针。
//////////////////////////////////////////////////////////////////////
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
// AfxWinMain()
就是MFC应用程序的入口函数WinMain()
{
ASSERT(hPrevInstance == NULL);
// Win32
应用程序只有一个实例,所以hPrevInstance肯定是NULL,
// 
如果不是,则肯定是出了问题。

int nReturnCode = -1;
// 
预设返回值为-1。这个返回值是用来与父进程通信的。不过并没有这个值的标

// 
使用方法。

CWinThread* pThread = AfxGetThread();
CWinApp* pApp = AfxGetApp();
// CWinApp
CWinThread的派生类,上两行代码分别初始化了一个指向CWinThre
ad
对象的
// 
指针和一个CWinApp对象。它们的初始化是通过调用AfxGetThread()AfxGet
App()
得到
// 
的,也就是说,它们分别被初始化为指向当前应用程序的唯一全局线程/应用
程序对象的
// 
指针。
// AFX internal initialization
if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
goto InitFailure;
// AfxWinInit()
函数初始化MFC程序,处理很多难以预料的问题。该函数和后面

// AfxWinTerm()
对应。
// App global initializations (rare)
if (pApp != NULL && !pApp->InitApplication())
goto InitFailure;
// 
调用pApp->InitApplication(),初始化应用程序。不过这还不够。

// Perform specific initializations
if (!pThread->InitInstance())
// 
调用pThread->InitInstance()函数,进行线程初始化。注意,InitInstance
()
通常
// 
是你的MFC代码中唯一必须重载的函数。
// 
下面的语句块是当初始化不成功时的处理。
{
if (pThread->m_pMainWnd != NULL)
{
TRACE0("Warning: Destroying non-NULL m_pMainWnd\n");
pThread->m_pMainWnd->DestroyWindow();
}
nReturnCode = pThread->ExitInstance();
// ExitInstance()
也是一个值得注意的函数
goto InitFailure;
}
nReturnCode = pThread->Run();
// pThread->Run()
是重要函数,它使得MFC进入消息循环。其细节待另文介绍。
// 
以下代码处理各种初始化失败情形
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;
}
//////////////////////////////////////////////////////////////////////
总之,MFC程序先建立应用程序对象,然后将内部准备好的WinMain()与之连接,

执行一系列动作,从而使程序运行。
上面的解释还是十分浅层次的,更具体的内容待俺学清楚之后慢慢贴出来。

posted @ 2013-11-20 21:51  centerall  阅读(338)  评论(0)    收藏  举报