我有一个 mfc 项目,它是由 Windows 服务生成的进程。由于某种原因,该过程在开始之前就死了。全局值被创建,但进程不会启动 _tmain。从 VC6 迁移到 VS2012 时出现此问题。
这是一个代码示例,我可以在这一行放置一个断点并停止,CWinApp theApp;但我不能在 _tmain 的第一行停止。该程序只是找不到入口点并且存在。
// prog.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    try {
        SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
        int nRetCode = 0;
        // initialize MFC and print and error on failure
        if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
        {
            // TODO: change error code to suit your needs
            nRetCode = 1;
        }
        else
        {           
            //Some propietry code which  runs here
        }
        return nRetCode;
    }
    catch(...) {
        return 147;
    }
}
最初我认为这个问题是由于 VS2012 附带的 MFC 引起的。然而,我注意到我们在移动之前的开发版本具有相同的影响。这似乎很奇怪,因为以前的版本具有相同的代码,并且它找到入口点就好了。
我可以通过执行以下操作来启动程序:
// prog.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
using namespace std;
class MyApp : public CWinApp {
public:
    MyApp() : CWinApp(_T("VCP")){}
    int init(LPTSTR  CommandLine);
    virtual int Run()
    {
        return init(m_lpCmdLine);
    }
};
MyApp theApp;
//int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
int MyApp::init(LPTSTR CommandLine)
{
    int argc = 2;
    TCHAR* argv[] = {_T(""),_T("")};
    argv[1]= CommandLine;
    try {
        SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
        int nRetCode = 0;
        // initialize MFC and print and error on failure
        int r=1;
        if (r>1)//(!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
        {
            // TODO: change error code to suit your needs
            nRetCode = 1;
        }
        else
        {
            // some propietry code
        }
        return nRetCode;
    }
    catch(...) {
        return 147;
    }
}
总而言之,我有 3 个版本的代码。一个运行良好的发布版本的代码。不同 Visual Studio 上的两个开发版本,具有相同的找不到入口点的影响。一个新的 mfc 项目包含与错误代码类似的代码,它会找到 _tmain。
我的问题是:
- 为什么会这样? 
- 如何使用 _tmain 运行?