简体   繁体   中英

How do I correctly set the entry point for an exe in Visual Studio?

I have set the entry point to WinMain but when I run the app it starts and doesn't display, I then have to shut it with task manager. Here's the code upto WinMain() :

#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 )
{
....

I'm not experienced in C++, and I don't know what this is doing, except making my exe smaller, which is what I'm trying to achieve.

edit : What I'm trying to do is create a very small window exe to understand how demo coder's work. So I'm thinking of creating a small c++ window app that provides a window handle to which I can attach SlimDX (if i can statically link the final c++ dll to a C# app, but I'm not there yet) I have my BasicWindow.exe down to 6,656 bytes. So I'm experimenting with anything I can find to get that size down to <3k.

[2012.Jan.10] Well I've had some success by rebuilding minicrt (available from http://www.benshoof.org/blog/small-programs/ ) under VS2010 and adding it as an additional dependency. I couldn't Ignore All Default Libraries as suggested, but I now have a Windowed application with an exe size of 4,096 bytes. I'd say that's some significant success. I'm within striking distance now. Every reduction from here on in, is more room for SlimDX. I'm pretty happy considering the only c++ apps I've ever written are console apps and a basic window :) I've been lucky I know !

A typical application should not mess up with Entry point setting of linker. Entry point should be set on a function included in the standard runtime library (which is wWinMainCRTStartup for unicode application for windows subsystem). This function does stuff like the proper initialization of CRT and creation of global objects. By rerouting entry point to your WinMain you will get undefined behavior unless you know precisely what you are doing and somehow implementing CRT initialization in your own WinMain . In my opinion the resulting size decrease will be negliable and the whole affair is hardly worth the risk.

When you set the entry point to WinMain , Windows doesn't give your program a console window, because WinMain is for programs with GUIs that don't need a console window. Your program is indeed running, though with no GUI you don't see anything happen.

I had the same issue. I wasn't satisfied with the answer marked here . So, I started digging in more and found that WS_VISIBLE was missing from CreateWindowEx function.

WS_OVERLAPPEDWINDOW with WS_VISIBLE makes the code work perfectly.

And regarding the file size, I was able to bring down the size of the code for x64 build to 3,072 bytes 1,600 bytes using Visual Studio Community Edition 2013 running on Windows 7 Ultimate SP1 x64. All this, without using minicrt.lib, you mentioned above.

Here's a screenshot of the executable properties.

属性窗口显示文件大小

For reference, here is the full program :

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM