首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Visual Studio中正确设置exe的入口点?

如何在Visual Studio中正确设置exe的入口点?
EN

Stack Overflow用户
提问于 2012-01-09 23:38:11
回答 3查看 30.6K关注 0票数 7

我已经将入口点设置为WinMain,但当我运行应用程序时,它会启动并且不显示,然后我必须使用任务管理器将其关闭。下面是WinMain()的代码:

代码语言:javascript
复制
#include <Windows.h>

// forward declarations
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); 

// The entry point into a windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int windowStyle )
{
....

我没有使用C++的经验,我不知道这是在做什么,除了缩小我的exe,这就是我想要实现的。

编辑:我想做的是创建一个非常小的窗口exe来了解演示编码器是如何工作的。因此,我正在考虑创建一个小的c++窗口应用程序,它提供了一个可以附加SlimDX的窗口句柄(如果我可以静态地将最终的c++ dll链接到C#应用程序,但我还没有做到这一点)我的BasicWindow.exe减少到了6,656字节。因此,我正在尝试我能找到的任何东西,以使大小降到<3k。

2012年1月10日,我在VS2010下重新构建了minicrt (可从http://www.benshoof.org/blog/small-programs/获得),并将其添加为额外的依赖项,从而取得了一些成功。我不能像建议的那样忽略所有默认库,但我现在有了一个可执行文件大小为4,096字节的窗口应用程序。我想说,这是一些重大的成功。我现在在打击范围内。从现在开始,每减少一次,SlimDX就会有更多的空间。考虑到我写过的c++应用程序只有控制台应用程序和一个基本的窗口,我很高兴:)我很幸运,我知道!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-10 00:30:18

一个典型的应用程序不应该搞乱链接器的Entry point设置。应该在标准运行时库( windows子系统的unicode应用程序的wWinMainCRTStartup )中包含的函数上设置入口点。这个函数做一些事情,比如正确初始化CRT和创建全局对象。通过将入口点重新路由到您的CRT,您将获得未定义的行为,除非您确切地知道您在做什么,并且以某种方式在您自己的WinMain中实现WinMain初始化。在我看来,由此产生的大小减少将是负责任的,整个事情几乎不值得冒险。

票数 7
EN

Stack Overflow用户

发布于 2012-01-09 23:42:44

当您将入口点设置为WinMain时,window不会给您的程序提供控制台窗口,因为WinMain是用于具有不需要控制台窗口的GUI的程序。您的程序确实在运行,尽管没有GUI,您看不到任何事情发生。

票数 2
EN

Stack Overflow用户

发布于 2012-01-10 14:04:05

作为参考,下面是完整的程序:

代码语言:javascript
复制
#include <Windows.h>

// forward declarations
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); // WindowProcedure function

// The entry point into a windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int  windowStyle )
{
// create and register a local window class > exit on failure
WNDCLASSEX wcx;
HINSTANCE zhInstance = GetModuleHandle(NULL);
wcx.cbSize = sizeof(WNDCLASSEX);                // size of structure
wcx.style = CS_HREDRAW | CS_VREDRAW;            // redraw if size changes
wcx.lpfnWndProc = WndProc;                      // window procedure
wcx.cbClsExtra = 0;                             // no extra class memory
wcx.cbWndExtra = 0;                             // no extra windows memory
wcx.hInstance = zhInstance;                     // handle to instance (owner)
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);    // predefined icon 
wcx.hCursor = LoadCursor (NULL, IDC_ARROW);     // predefined arrow
wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//(HBRUSH) (COLOR_WINDOW + 1);// white background brush
wcx.lpszMenuName = NULL;                        // name of menu resource
wcx.lpszClassName = TEXT("BasicWindow");        // name of window class
wcx.hIconSm = (HICON)LoadImage(zhInstance,              // small class icon 
    MAKEINTRESOURCE(5),
    IMAGE_ICON, 
    GetSystemMetrics(SM_CXSMICON), 
    GetSystemMetrics(SM_CYSMICON), 
    LR_DEFAULTCOLOR); 

if (!RegisterClassEx(&wcx))
{
    MessageBoxA(0, "Error registering window class!","Error",MB_ICONSTOP | MB_OK);
    DWORD result = GetLastError();
    return 0;
}

// create window > exit on failure
HWND hwnd; // the window handle
hwnd = CreateWindowEx(
    WS_EX_STATICEDGE, 
    wcx.lpszClassName, 
    TEXT("Basic Window"), 
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    320,
    240,
    NULL,
    NULL,
    zhInstance,
    NULL);

if (hwnd == NULL)
{
    MessageBoxA(0,"Error creating window!","Error",MB_ICONSTOP | MB_OK);
    return 0;
}

// show window
ShowWindow(hwnd, SW_MAXIMIZE);
// send a WM_PAINT message to the window
UpdateWindow(hwnd);

// enter message loop, break out of loop if there is an error (result = -1)
MSG msg;    // for storing the windows messages
int status; // for storing the status of the windows message service where -1 = error, 0 = WM_QUIT, n = any other message)
while ((status = GetMessage(&msg,(HWND) NULL,0,0)) != 0 && status != -1)
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return msg.wParam;
    UNREFERENCED_PARAMETER(lpCmdLine);
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CLOSE:
    DestroyWindow(hWnd);
    break;
case WM_DESTROY:
    PostQuitMessage(0);
default:
    return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8790923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档