DHCP client API C++ MSDN example - c++

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

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.

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.

Unresolved external symbols __RTC_* in Windows programming tutorial

Using Visual Studio Express 2010 I made a Windows project with the options Windows Application and Empty Project. I then tried the following code snippet from the MSDN Windows tutorials:
#include <windows.h>
#include <shobjidl.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr))
{
IFileOpenDialog *pFileOpen;
// Create the FileOpenDialog object.
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr))
{
// Show the Open dialog box.
hr = pFileOpen->Show(NULL);
// Get the file name from the dialog box.
if (SUCCEEDED(hr))
{
IShellItem *pItem;
hr = pFileOpen->GetResult(&pItem);
if (SUCCEEDED(hr))
{
PWSTR pszFilePath;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
// Display the file name to the user.
if (SUCCEEDED(hr))
{
MessageBox(NULL, pszFilePath, L"File Path", MB_OK);
CoTaskMemFree(pszFilePath);
}
pItem->Release();
}
}
pFileOpen->Release();
}
CoUninitialize();
}
return 0;
}
I got the following errors:
1>------ Rebuild All started: Project: Test05, Configuration: Debug Win32 ------
1> Test05.cpp
1>Test05.obj : error LNK2019: unresolved external symbol #_RTC_CheckStackVars#8
referenced in function _wWinMain#16
1>Test05.obj : error LNK2019: unresolved external symbol __RTC_CheckEsp referenced in
function _wWinMain#16
1>Test05.obj : error LNK2001: unresolved external symbol __RTC_Shutdown
1>Test05.obj : error LNK2001: unresolved external symbol __RTC_InitBase
1>LINK : error LNK2001: unresolved external symbol _wWinMainCRTStartup
What's going on here? Best I can tell something to do with wWinMain, but it is copied directly from the site.
Compilers seem to be far more trouble for me than learning programming. I decided on Visual C++ after trying a few others (codeblocks mostly), but since Visual C++ seems to have the most support (or at least the majority of users) I figured it was better than never getting anywhere since they all are so unintuitive to a beginner.
The _RTC_xxx symbols are added when the 'Basic Runtime Checks' are used; to disable them, you can go on the project properties, and set the Configuration Properties>C/C++>All Options>Basic Runtime Checks option to 'Default'. Though, as mentioned in the other answers, it looks like you have a mismatch in the C-Runtime libraries.
It looks like you're linking againt a different verison of the runtime libraries than what you're compiling against.
Please make sure you have only one version of Visual Studio installed and in path.
If you have more than one version, try to temporarily rename root directories of other Visual Studio installations to see if this will cause any effect.
I would download the complete code sample from the link here rather than copying and pasting that code snippet you linked to. There may be important compiler/linker settings in the project files that are not being shown there. The sample is a VS 2008 solution, but I was able to upgrade it to a 2010 solution and build it. However, it gave me 'fatal linker error: could not find 'kernel32.lib'' when I tried to build it in VS 2008.

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.

problem with win 32 API programming

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.