문제

I have making an attempt at writing my first program in Visual Studio, however am being troubled by an error. It says: -

    Error 3 error LNK2019: unresolved external symbol _wWinMain@16 referenced in function ___tmainCRTStartup    
E:\Documents\Programming\Software Development\Microsoft Development\Microsoft Development\MSVCRTD.lib(wcrtexew.obj) 
Microsoft Development

On researching I found similar errors, but none have helped me solve the problem. I have changed the entry point to

wWinMainCRTStartup

the character set to Unicode

the subsystem to console. The project is a win32 application. The code is as follows: -

#include <windows.h>
#include <stdio.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
    {
    MessageBox(NULL, "Hello World!", "Note", 1/*MB_OK*/);
    printf("nCmdShow = %d\n", nCmdShow);
    return 0;
}

How do I fix this issue?

P.S. I am using Visual Studio Ultimate 2013

도움이 되었습니까?

해결책

For a Unicode build your code needs to be more like this:

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPWSTR lpCmdLine, int nCmdShow)
    {
    MessageBox(NULL, L"Hello World!", L"Note", 1/*MB_OK*/);
    printf(L"nCmdShow = %d\n", nCmdShow);
    return 0;
}

At least by default this will be set to use the Windows subsystem (because the entry point is named a variant of WinMain). You can force that to the console subsystem (-subsystem:console flag to the linker) or get it to happen by default by changing the entry point to a variant of main instead:

int wmain(int argc, wchar_t **argv) { // ...

Obviously you won't be able to print nCmdShow using this though (not that it really means anything in a console program). For that matter, since you're not using the command line arguments anyway, you can simplify this somewhat to:

int wmain(){ // ....

Actually, nCmdShow is basically obsolete even for windowed programs. The first time a windowed program calls ShowWindow, it normally passes nCmdShow as the parameter. Windows, in turn, ignores the value passed in the first call to ShowWindow, and instead uses the value from the process' STARTUPINFO structure. Only in subsequent calls to ShowWindow is the parameter used (and for these subsequent calls, you're not supposed to pass nCmdShow either--you're supposed to pass one of the defined constants such as SW_SHOWNORMAL).

Reference: MSDN entry for ShowWindow

다른 팁

A Win32 application starts at WinMain. A console application starts at main. Your question implies you have a confused mixture of the two.

Just use the File, New, Project menu command to let Visual Studio build a skeleton of the type of application you have in mind.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top