C++ Compiling windows service with visual studio - c++

hope you could help me. I got a C++ source for my service based on Microsoft example, still i get linker error compiling it:
error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
as the entry point for windows services is int _tmain(int argc, TCHAR* argv[])
In my case it's void __cdecl _tmain(int argc, TCHAR* argv[]) { ... }
There are 1 header and 1 cpp files with a class used by the service and main.cpp contaning entry point and c style service related code. Subsystem is console without any custom entry point set. Still if i add classic int main(...) to the code project compiles yet the service does not start from windows service manager returning error.
Please advise how to compile this using _tmain.

Ok, still no luck with _tmain. Unfortunately i haven't tried to define WINAPI WinMain entry point.
Still if you proceed with wmain program entry point and add code mentioned by Richard Critten for a service startup SCManager performs just fine. For a complete service sample refer to MS doc.wmain will force you to use Unicode yet it shouldn't be such a problem in 2020.
Subsystem should be set as supposed to your needs and no need to set custom entry point.
Thanks to everyone commented.
UPD You have to include tchar.h in order to use _tmain, so all types of entry points will work fine.

Related

Unresolved External _main in a console application

I have the following main function:
int main(int argv, char** argc) {
MainGame mainGame;
mainGame.run();
system("pause");
return 0;
}
it throw a LNK2019 unresolved _main at me.
now i did a bit of google-ing and found countless examples of people having accidentlyt setup win32 applications instead of the intended console app, so I checked mine in linker->system->subsystem and it read console.
Had a similar problem when using SDL. I've added #undef main after #include "SDL.h" since main was defined inside SDL for some other purpose (as DimChtz pointed out in the comments). It solved the problem.
By the way, this does not have to be SDL-specific. Some other included header or source file in your project could also be #define-ing "main", which would trigger this behavior.

linking vs 2012 c++ GDAL

I created a standard windows application with VS 2012 Pro. Just a main.cpp that looks like this:
#include "gdal_priv.h"
#include "cpl_conv.h"
int main(int argc, char* argv[])
{
GDALAllRegister();
return 0;
}
I have set my include path properly. I have gdal_i.lib in my Linker -> Input -> Additional Dependencies.
Link fails with the following message:
1>main.obj : error LNK2019: unresolved external symbol
_GDALAllRegister#0 referenced in function _main
(A scad of other symbols are missing as well, but this one should be easy.)
I used dumpbin and GDALAllRegister appears in the exported symbols. It does appear as "GDALAllRegister", not as "_GDALAllRegister#0".
I tried downloading and using the dev build, and also built myself. Same results.
I just know this is something simple, but I'm totally brain-cramping here. What have I done wrong?
Thanks.
-reilly.
I said it was simple and stupid, and it was.
I had the build manager set to build 32 bit.
Another 4 hours of my life I'll never get back.
Thanks to Manuell for his suggestion. It got me looking in a different direction and I found it.
Sorry, community. This is what comes from programming tired.
-reilly.
This kind of error is typical of a "Calling Convention" mismatch between EXE and LIB.
Check you have __cdecl (/Gd) in "Configuration Properties" -> C++ -> Advanced -> "Calling Convention"

Drive MFC Application from Win32 Console Application

I have a situation where I need to essentially run some unit tests against a MFC application.
I basically have some gtest code in Win32 Console application that needs to be able to create a instance in code of the MFC application and basically do some assertions etc...
I tried to create a Win32 console application with MFC header included. I then included the header file of my MFC applicaton class. However, whenever I try to create an instance i.e CWindowApplicationApp the_app in my console application, I receive linking error
This is some of the source code from my console application
CWindowApplicationApp the_app;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
}
return nRetCode;
}
error LNK2019: unresolved external symbol "public: __thiscall CWindowApplicationApp::CWindowApplicationApp(void)" (??0CWindowApplicationApp##QAE#XZ) referenced in function "void __cdecl `dynamic initializer for 'the_app''(void)" (??__Ethe_app##YAXXZ)
Any help? I have included the header file paths
The linker is telling you it doesn't know where to find the object code for CWindowApplicationApp's constructor.
You need to link with whatever object files define CWindowApplicationApp (typically WindowApplicationApp.obj), as well as any other object files that are referred to by WindowApplicationApp.obj (very dependent on your app structure).
You are on the wrong road. It is not possible to create an instance of an application inside another application. What will work is to add some test code into the application and rebuild it.

Having trouble embedding Lua for Windows install into C++ program

This is the first question I have found myself not being able to get to the bottom of using my normal googling/stack overflowing/youtubing routine.
I am trying to compile a minimal Lua program inside of a C++ environment just to ensure my environment is ready to development. The Lua language will be later used for User Interface programming for my C++ game.
First some basic information on my environment:
Windows 7 64-bit
Visual studio 2010
Lua for Windows 5.1 (latest build I could download from google code)
Here is the code I am trying to compile:
// UserInt.cpp : Defines the entry point for the console application.
//
#pragma comment(lib,"lua5.1.dll")
#include "stdafx.h"
#ifndef __LUA_INC_H__
#define __LUA_INC_H__
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
int _tmain(int argc, _TCHAR* argv[])
{
lua_State * ls = luaL_newstate();
return 0;
}
#endif // __LUA_INC_H__
Here is the Error I am getting:
1>UserInt.obj : error LNK2019: unresolved external symbol _luaL_newstate referenced in function _wmain
1>c:\users\deank\documents\visual studio 2010\Projects\UserInt\Debug\UserInt.exe : fatal error LNK1120: 1 unresolved externals
Things I have tried:
I have read about lua_open()(and several other functions) no longer being used so I tried the newstate function instead. I get the same error. This was more of a sanity check than anything. I am using 5.1 and not 5.2 so I do not think this really matters.
I have also read this thread Cannot link a minimal Lua program but it does not seem to help me because I am not running the same environment as that OP. I am on a simple windows 7 and visual studio environment.
The top pragma comment line was something I saw in yet another thread. I get the same error with or without it.
I have gone into my visual studio C++ directories area and added the lua include to the includes and the lua lib to the libraries.
So it seems like my program is seeing the .h and seeing the symbol. But for some reason it is not getting the .cpp implementation for the functions. This is why I was hoping including that .dll directly would help fix the problem, but it hasn't.
So, I feel like I have exhausted all of my options solving this on my own. I hope someone is able to help me move forward here. Lua looks like an awesome language to script in and I would like to get my environment squared away for development.
I hope it is just some silly error on my part. I believe I have provided as much information as I can. If you need more specifics I will update with info if I can provide it.
Edit1
Tried the solution in this Can't build lua a project with lua in VS2010, library issue suspected
That did not work either.
You'll need to have the library (.LIB) file and add that to VS. Use Project > Properties and go to Linker > Input and add the full .lib filename to the "Additional Dependencies" line. Note that the .LIB is different from the .DLL.
Personally, I prefer adding the source code to my project, over referencing the dynamic link library. The following procedure will let you do as such.
Download the source code ( http://www.lua.org/ftp/ ), uncompress it.
In Visual Studio, choose File > New > Project and choose Visual C++, Win32, "Win32 Console Application".
In your project in Visual Studio, add all the source code, except luac.c. Also delete the main() function out of the file that VS created for you. This is usually given the name of the project you specified with the .cpp file extension. You could just remove this file all-together from the project.
Build and Run.
This is the Lua console

How to solve the "unresolved external symbol" link error in visual c++?

I Have the Visual Studio C++ 2008 Express Edition.
I am trying to compile a program but I am getting the following Link Error:
1>MSVCRT.lib(wcrtexew.obj) : error LNK2001: unresolved external symbol _wWinMain#16
What I tried to do:
I found this on google:
For Visual C++ .NET: In the Advanced category of the Linker folder in the Project Properties dialog box, set the Entry Point to wWinMainCRTStartup.
It was intended to work but didn't. How do I compile this app?
The code is stupidly simple:
#include "stdafx.h"
int main( int argc, char ** argv )
{
}
There are multiple ways to solve this:
Create a console application
Change the Subsystem console now in the linker settings ( Project Settings -> Linker -> System -> SubSystem (select Console))
Change the entry point in the linker settings to mainCRTStartup (Project Settings -> Linker -> Advanced -> Entry Point)
Define int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow ); instead of int main(int argc, char const ** argv)
Change the Character Set to Use Unicode Character Set (Project Settings -> General->Character Set )
It looks like when you created your project you selected a GUI (Win32, MFC, etc) program.
Those programs have a WinMain() instead of a main().
What you want is a Console project.
According to similar questions, that error happens when main isn't defined.
Maybe for some reason it's compiling a different file?
This answer suggests that maybe your flags are inconsistent.