我已经阅读了十几页的内容,解释了如何将Win32控制台应用程序转换为Windows应用程序,而无需短暂打开和关闭控制台窗口即可运行它,但是对于起初的工作我还是个新手。
例如,在VC2010中,我对此处描述的项目属性进行了两项更改:
并将Main更改为WinMain,但当然从编译器收到了错误消息。
在其他页面之后,我还尝试创建控制台应用程序,然后在Win32应用程序向导中,将“应用程序类型”更改为Windows应用程序,但是我不知道下一步该怎么做。我尝试将int Main更改为int CALLBACK WinMain,但是当然也不起作用。
有谁可以帮助初学者吗?这是我认为是我的代码编辑的相关部分:完整的代码,对于任何想知道这是什么目的的人,都在这里:
https://www.dropbox.com/s/1h8x1k2zv0lc5d1/vPasteCPlus.txt?dl=0
#include <stdafx.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <codecvt> // for wstring_convert
#include <locale> // for codecvt_byname
using namespace std;
// helper to get path to this application
string ExePath() {
char buffer[MAX_PATH];
GetModuleFileNameA( NULL, buffer, MAX_PATH );
string::size_type pos = string( buffer ).find_last_of( "\\/" );
return string( buffer ).substr( 0, pos);
}
int main(int argc, char* argv[])
{
// get the command-line argument if any, and do various things
}
再次,对这个初学者的问题表示歉意。我对C ++的唯一经验是编写控制台应用程序,我们将不胜感激收到任何建议。
好的,因此您已经打开了Visual Studio。并且您有Solution Explorer
(如果没有,View
-> Solution Explorer
)。
首先,要制作Windows应用程序,应将入口点从main()
(C ++标准)更改为Windows特定的WinMain()
。有关更多详细说明,请参见msdn 。因此,您将main()
转到下一个(从文档中复制粘贴):
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow)
{
return 0;
}
当然,您应该包含<Windows.h>
,因为Windows提供了它自己的API来与系统一起使用(例如LPSTR
type)。简而言之,您完成了编译器编译程序所需的全部工作。您可以构建您的解决方案(Build
-> Build Solution
)...这将导致linker
错误:
error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
同样,编译器完成了您想要的所有操作,并将源文件编译为.obj
,但是,因为您的项目是为Console应用程序配置的,因此linker
需要Console应用程序的标准入口点-main
在我们的案例中找不到(解决)它,因为我们将main更改为WinMain。为了使链接器开心,您应该告诉它:
Solution Explorer
Properties
:Configuration Properties
-> Linker
->System
并设置SubSystem
为Windows
:尝试再次构建您的应用程序,瞧-您已经没有链接器错误,因为您的链接器现在知道它需要生成Windows应用程序并找到Windows入口点:WinMain()
!
关于这个:
// get the command-line argument if any, and do various things
您应该使用的lpCmdLine
参数WinMain()
。但是请小心,如果您像这样运行程序(例如,so.exe):
so.exe arg1 arg2
lpCmdLine
是arg1 arg2
字符串。您可以做很多事情来获取arg1
和arg2
asn数组(如main()argv
(带有argc
)),但是您可以探索:
和相关内容(例如Windows上的wchar_t)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句