problem with win 32 API programming - c++

i just tried to compile and run a simple example which was found on Programming Windows - Win32 API by Charles Petzold here is the code :
#include <windows.h>
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MessageBox (NULL, TEXT ("Hello, Windows!"), TEXT ("HelloMsg"), 0) ;
return 0 ;
}
and i got the following error :
Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib(crtexe.obj)
i am using Visual Studio 2010 on Windows 7. how to solve this problem?
is it because i am running some outdated piece of code in new OS if the win32 API for Windows 7 has been changed can anybody suggest me any resources to learn about win32 API for windows 7 with C/C++ or even assembly

There's no doubt this is a duplicate question but i'll answer anyway.
You have created the wrong project type. You need to create a Win32 Project not a Win32 Console Application.

Related

Linker error unresolved external symbol with only wWinMain MSVC 2019

This is the only code I have:
#include <windows.h>
int APIENTRY wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR pCmdLine,
_In_ int nCmdShow)
{
OutputDebugStringA("Hello\n");
return 0;
}
The wWinMain function with the exact same signature works in a separate project I created with the "Windows Desktop Application" (or something) template.
However I am getting a LNK2019 with the message:
unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
coming from MSVCRTD.lib(exe_main.obj)
I am using Visual Studio 2019 Community Edition with MSVC 2019 on Windows 10. I created an "Empty Project" and only have a "main.cpp" file in the "Source Files" directory.
Can anyone help me try to diagnose what may be going wrong here? I am having no luck reading msdn/searching the web for the solution, it is very frustrating as I am just trying to get off the ground here.
Thank you.
Set Subsystem to Windows in linker settings (for all configurations and targets).
Visual Studio can compile for different Windows subsystems, each requiring its own program entrypoint. An error saying "_main not found" suggests your subsystem is set to Console, since the linker is looking for a main() function.
If you have a WinMain() entrypoint (or wWinMain() for Unicode mode), it means you're targeting the Windows subsystem.

DHCP client API C++ MSDN example

Here is a simple example for DHCP client API, https://msdn.microsoft.com/en-us/library/windows/desktop/aa363345(v=vs.85).aspx
I'm working in Visual Studio 2012 with console application c++, it has a failure when building:
1>------ Build started: Project: ConsoleApplication2, Configuration: Debug Win32 ------
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain#16 referenced in function ___tmainCRTStartup
1>D:\visual work place\ConsoleApplication2\Debug\ConsoleApplication2.exe : fatal error LNK1120: 1 unresolved externals
Can anybody know how to solve this problem?, thanks
There are lots of hits when Google-ing this error. Here's one.
There's a mismatch between your C code and your VStudio project type.
Your app's entrypoint is probably int main(int argc, char **argv) (this is one of its most general forms) which in MS world is corresponding to a Console application.
But MS has defined other application types. One of them is a GUI (window based) which doesn't require a console. For that one MS defined an entrypoint as:
int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow). You can find more details about it on MSDN.
When building the application's executable, the linker must know what type the application has. That is specified by the /SUBSYSTEM setting.
That is set when you create your (Visual C++) project based on your choice:
Win32 Console Application
Win32 Project
And I think the latter is the default.
In order to fix things, you need to change your linker setting to match your code (well there could be the other way around too, but that would be more complicated). In order to do that, in VStudio IDE go to your Project Properties -> Linker -> System -> SubSystem and change it from Windows (/SUBSYSTEM:WINDOWS) to Console (/SUBSYSTEM:CONSOLE).

visual programming

sir i have written a simple program to display a message box in visual c++ 2008 but the problem is that when i run this code a dialog box shows that "your project is out of date would you like to build it"
when i press yes it shows error
so what is the problem???
the code is here
#include <Windows.h> /* The standard "windows.h" inclusion */
int WINAPI WinMain( HINSTANCE hInstance, /* A handle to the current instance of the application. */
HINSTANCE hPrevInstance, /* A handle to the previous instance of the application. */
LPSTR lpCmdLine, /* The command line for the application, excluding the program name. */
int nCmdShow) /* Controls how the window is to be shown. */
{
/* Call to the MessageBox function */
MessageBox(NULL, "Hello, Windows API!", "Hello", MB_OK);
/* WinMain returns 0 if we exit before we enter message loop, more on that to come */
return 0;
}
Whenever i run this in visuall c++ 2008 it says project out of date, do u want to build so i click yes but then it cant
down the bottom it says
1>Linking...
1>MSVCRTD.lib(crtexe.obj) : error LNK2001: unresolved external symbol _main
1>C:\Documents and Settings**\My Documents\Visual Studio 2008\Projects\msg\Debug\msg.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Documents and Settings**\My Documents\Visual Studio 2008\Projects\msg\msg\Debug\BuildLog.htm"
1>Wrath Lands - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
VS creates UNICODE projects by default so macros like MessageBox evaluates to MessageBoxW which expects LPCWSTR arguments and not LPCSTR. Try change to: MessageBox(NULL, _T("Hello, Windows API!"), _T("Hello"), MB_OK);
The error message error LNK2001: unresolved external symbol _main is important. It looks like you've created a console project but there is no main() function defined, hence the linker error.
When you create a new Visual Studio project as a Win32 Console Application it assumes the entry point to your program will be the normal C/C++ main() function and it links with the C/C++ library startup code. If you instead create a Win32 Project it assumes the entry point will be WinMain() and links with Windows application startup code.
To avoid this problem you should begin with a Win32 Project. To fix it after the fact you could try going into Project Properties -> Linker -> System and change the SubSystem option from Console (/SUBSYSTEM:CONSOLE) to Windows (/SUBSYSTEM:WINDOWS). Note that there may be other settings that should be changed as well so I suggest you just start fresh with a new Win32 Project instead.

Link windows.h (WNDCLASSEX) code in VS2010 console application

I have a VS2010 Win32 console application. Within this console application I try to attach a window as child to another Win32 app. But I do not get the application linked (it compiles, just the linker issue AFAIK):
Error 1 error LNK2019: unresolved external symbol "long __stdcall viewproc(struct HWND__ *,unsigned int,unsigned int,long)" (?viewproc##YGJPAUHWND__##IIJ#Z) referenced in function "void __cdecl createFSXWindow(void)" (?createFSXWindow##YAXXZ) H:\Projects\VisualStudioNet2010\FSXTests\Menu Items\Menu Items\MenuItems.obj Menu Items
I have added all libs from a Win32 Windows project under Linker/include:
kernel32.lib;user32.lib;gdi32.lib;winspool.lib; comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib; odbccp32.lib;%(AdditionalDependencies)
Remark: Just to clarify, I do NOT(!) try to attach the window to the console window, but another Win32 application, so it is not about "Make a win32 console app display a window"
The issue comes in once I add the window code such as:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
....
// window
WNDCLASSEX wc;
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.lpfnWndProc = viewproc;
When I check the command line of the linker the only difference between my console app and an Win32 app is SUBSYSTEM:CONSOLE vs. SUBSYSTEM:WINDOWS
Any hints?
The linker error just tells you that you forgot to write the viewproc() function.
You declared it, the compiler is happy, but didn't implement it, the linker is unhappy. Or you wrote it but mistyped the definition. It is not a function that's implemented in one of the standard libraries, you have to provide it. The window procedure is what you use to give a window custom behavior.

Unresolved external symbol fail in VERY simple application

The simple code fails:
#include <Windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MessageBox(NULL, L"Hello World!", L"Just another Hello World program!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
Errors:
Error 1 error LNK2019: unresolved external symbol main referenced in function __tmainCRTStartup ... Projects\DX11_3\DX11_3\MSVCRTD.lib(crtexe.obj) DX11_3
Error 2 error LNK1120: 1 unresolved externals ... Projects\DX11_3\Debug\DX11_3.exe 1 1 DX11_3
What could be wrong? I've downloaded and installed Windows SDK, added det include folder to the project.
The program's entry point is where execution starts at. For a console application, this defaults to main. For a Windows application with no console, this defaults to WinMain.
The linker is searching for main, most likely because you created a console application. Go into your project settings and change the subsystem to Windows. You can find this option in Configuration Settings -> Linker -> System
Strictly and formally speaking, your program doesn't contain a function named main, so it isn't valid C++.
To enable non-standard extensions like WinMain, you will have to make sure you are creating a Windows project, or that the compiler options are set to compile a Windows program.
you are compiling your application as a "Console Application", so Visual Studio tries to find an entry point named main(). but your code defines a "GUI Application" with an entry point named WinMain().
you should edit your project settings and set the application type to "Console application" in the Linker settings.