我有一個由Windows服務產生的進程的mfc項目。出於某種原因,該過程在開始之前就會死亡。全局值被創建,但該過程不會啓動_tmain。從VC6移至VS2012時引發此問題。C++ _tmain不會開始運行
這是一個代碼示例,我可以放置一個斷點並停在此行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個版本的代碼。代碼的發佈版本工作得很好。在不同視覺工作室上的兩個開發版本,其具有與找不到入口點相同的影響。一個新鮮的mfc項目包含類似於錯誤代碼的代碼,並且它找到了_tmain。
我的問題是:
這究竟是爲什麼?
如何使用_tmain運行?
你可以嘗試包含tchar.h嗎?它定義_tmain – Enigma
我沒有編譯問題與tchar,雖然它沒有幫助 – qballer