2013-07-10 59 views
1

我有一個由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。

我的問題是:

  1. 這究竟是爲什麼?

  2. 如何使用_tmain運行?

+0

你可以嘗試包含tchar.h嗎?它定義_tmain – Enigma

+0

我沒有編譯問題與tchar,雖然它沒有幫助 – qballer

回答

1

如果EXE作爲控制檯模式應用程序鏈接,您的原始代碼才能工作。對於MFC應用程序來說很不尋常,但它是受支持的。通過調用獲得MFC初始化確實需要AfxWinInit()。

但很明顯,您的EXE沒有作爲控制檯模式應用程序鏈接,或者您的第二個代碼段不起作用。它依賴於MFC內嵌的WinMain()實現。 MFC應用程序正常完成的方式。

鏈接器+系統,子系統設置。如果控制檯模式應用程序的確是有意的,並且您希望自己的main()函數成爲入口點,則需要將其設置爲「控制檯」。

+0

控制檯(/ SUBSYSTEM:CONSOLE)已被定義仍然沒有去 – qballer