gpt4 book ai didi

C++ _tmain 不会开始运行

转载 作者:行者123 更新时间:2023-11-28 07:31:41 27 4
gpt4 key购买 nike

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

我的问题是:

  1. 为什么会这样?

  2. 如何使用 _tmain 运行?

最佳答案

只有当 EXE 作为控制台模式应用程序链接时,您的原始代码才能工作。对于 MFC 应用程序来说很不寻常,但它是受支持的。确实需要通过调用 AfxWinInit() 来初始化 MFC。

但显然您的 EXE 没有作为控制台模式应用程序链接,否则您的第二个代码段将无法运行。它依赖于嵌入在 MFC 中的 WinMain() 实现。 MFC 应用程序的正常完成方式。

链接器+系统,子系统设置。如果确实需要控制台模式应用程序并且您希望将自己的 main() 函数作为入口点,则需要将其设置为“控制台”。

关于C++ _tmain 不会开始运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17573938/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com