Set custom EntryPoint for a simple application - c++

I have this simple hello world c++ application:
#include <windows.h>
#include <iostream>
using namespace std;
void test()
{
cout << "Hello world" << endl;
}
I want to use test as my custom entry point. So far I tried to set Linker -> Advanced -> Entrypoint to test But I got lots of lnk2001 errors. Is it possible to somehow remove any main() wmain() WinMain() and use my function with just Visual studio settings?

Using a custom entry point in a Windows app bypasses the entire CRT startup and global C++ initializations. Because of that, it requires not using the CRT, and turning off compiler features that depend on the CRT such as /GS buffer checks and other /RTC run-time error checks.
The following is an example of a minimal app with the custom entry point test.
#include <sdkDdkVer.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
// compile with /GS- lest
// LNK2001: unresolved external symbol #__security_check_cookie#4
//#pragma strict_gs_check(off)
// turn off /RTC*
#pragma runtime_checks("", off)
#pragma comment(linker, "/nodefaultlib /subsystem:windows /ENTRY:test")
int __stdcall test(void)
{
OutputDebugStringA("custom /entry:test\n");
ExitProcess(0);
}
More insight can be found in Raymond Chen's WinMain is just the conventional name for the Win32 process entry point.

Related

Creating a C++ .Net Core wrapper for native library results in error LNK2028

I'm trying to create a managed (.net core) C++/CLI wrapper for a native library (Srt) but when referencing methods in the native lib I'm getting 2 build errors. I've referenced the headers srt.h from the native library, tried calling a method named srt_cleanup but get this on build:
error LNK2028: unresolved token (0A00000B) "extern "C" int __cdecl srt_cleanup(void)" (?srt_cleanup##$$J0YAHXZ) referenced in function "public: void __clrcall HeliosMediaStreamCliSrt::SrtReceiver::Stop(void)" (?Stop#SrtReceiver#HeliosMediaStreamCliSrt##$$FQE$AAMXXZ)
error LNK2019: unresolved external symbol "extern "C" int __cdecl srt_cleanup(void)" (?srt_cleanup##$$J0YAHXZ) referenced in function "public: void __clrcall HeliosMediaStreamCliSrt::SrtReceiver::Stop(void)" (?Stop#SrtReceiver#HeliosMediaStreamCliSrt##$$FQE$AAMXXZ)
Sample:
// pch.h
#ifndef PCH_H
#define PCH_H
#include <srt.h>
#include <stdio.h>
#include <tchar.h>
#include <memory>
#include <map>
#include <stdlib.h>
#endif //PCH_H
// SrtReceiver.h
#pragma once
using namespace System;
namespace TestSrt {
public ref class SrtReceiver {
public:
void SrtReceiver::Stop();
private:
};
}
// SrtReceiver.cpp
#include "pch.h"
#include "SrtReceiver.h"
namespace TestSrt {
void SrtReceiver::Stop() {
srt_cleanup();
}
}
Project Configuration:
General -> Configuration Type: Dynamic Library (.dll)
Advanced -> Common Language Runtime Support: .NET Core Runtime Support (/clr:netcore)
Advanced -> .NET Core Target Framework: .NET 5.0
C/C++ -> General -> Common Language Runtime Support: NetCore
I'm pretty rusty with C++ so I'm unfamiliar with the build system and its resulting in confusing errors. I've never tried doing anything with .net core in c++ before as C# is more my speed. This looks like something to do with build properties and the compiler is looking for the wrong internal method names. How would I go about fixing this?
I simply forgot to include the paths to the .lib files in Linker -> Input -> Additional Dependencies: /path/to/srt.lib
Simple, but spent hours of google searching to no avail. Hopefully this helps out other a C++ beginners as I didn't come across it.

App Crash for C++/CLI DLL and Exe of x64 bit

Just to showcase problem I have written a simple DLL x64 of "Hello world" changed its property to CLR and Calling its Function from an Exe which is also x64 and property Changed to CLR.
But My exe prints the Output "Hello World" and crashes every time and gives FAULT Module Name KERNELBASE.dll.
When I change Property of both to NO CLR it works perfectly fine. This happens only for x64 application not observed in x86 program.
I am using Visual Studio 2017 on Windows Server 2012.
It will be very helpful if someone can guide me to correct direction.
Details of App Crash
Problem Event Name: APPCRASH
Application Name: consuming_using_clr.exe
Application Version: 0.0.0.0
Application Timestamp: 5b42fff3
Fault Module Name: KERNELBASE.dll
Fault Module Version: 6.3.9600.17055
Fault Module Timestamp: 532954fb
Exception Code: c0020001
Exception Offset: 0000000000005bf8
OS Version: 6.3.9600.2.0.0.16.7
Locale ID: 1033
Additional Information 1: 8e72
Additional Information 2: 8e72455f15a8480830570fcb3c4abf60
Additional Information 3: f8d5
Additional Information 4: f8d519c5149c6c561af747d4db7e910a
DLL dot.h file
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the DLLWINDOWSDESKTOPWIZARD_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// DLLWINDOWSDESKTOPWIZARD_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef DLLWINDOWSDESKTOPWIZARD_EXPORTS
#define DLLWINDOWSDESKTOPWIZARD_API __declspec(dllexport)
#else
#define DLLWINDOWSDESKTOPWIZARD_API __declspec(dllimport)
#endif
#using<System.dll>
#include<iostream>
using namespace System;
using namespace std;
// This class is exported from the Dll_windows_desktop_wizard.dll
class DLLWINDOWSDESKTOPWIZARD_API CDllwindowsdesktopwizard
{
public:
CDllwindowsdesktopwizard(void);
~CDllwindowsdesktopwizard(void);
void helloworld();
};
*DLL DOT CPP File *
// Dll_windows_desktop_wizard.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "Dll_windows_desktop_wizard.h"
// This is the constructor of a class that has been exported.
// see Dll_windows_desktop_wizard.h for the class definition
CDllwindowsdesktopwizard::CDllwindowsdesktopwizard(void)
{
}
CDllwindowsdesktopwizard::~CDllwindowsdesktopwizard(void)
{
}
void CDllwindowsdesktopwizard::helloworld()
{
Console::WriteLine("Hello World");
}
EXE Calling DLL Function
#include<iostream>
#include "Dll_windows_desktop_wizard.h"
using namespace System;
int main()
{
CDllwindowsdesktopwizard lv_obj;
lv_obj.helloworld();
return 0;
}
dllMain.cpp
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
Finally I got it.Came across this link https://msdn.microsoft.com/en-us/library/ccthbfk8.aspx as i was trying to remove the warning from my exe warning was
"warning:Calling managed 'DllMain': Managed code may not be run under loader lock,including the DLL entrypoint and calls reached from the DLL entrypoint"

C++ DirectX Create Device Unresolved Externals Symbol

I made a dll for DirectX tools and got stuck with a compilation error (As the title says : Unresolved External Symbol Direct3DCreate9 called from initDevice) when creating the device.
I narrowed down the code to a single line and a single function and the error persists.
#include "stdafx.h"
#include "DirectXTools.h"
#include <stdexcept>
#include <d3d9.h>
using namespace std;
namespace DirectX
{
LPDIRECT3D9 directX;
void initDevice(HWND myWindow) {
directX = Direct3DCreate9(D3D_SDK_VERSION);
}
}
I am a beginner C++ programmer so I assume the problem is very easy to solve.
The solution that some external libraries's .libs require being added through the linker in the project properties.

C++ Dll linking Curl

I am trying to create a dll which uses the curl library for a very simple function. Everything works just fine, the only problem is, that the curl linking does not seem to work properly.
I use the same linking, preprocessordefines and include directories like in my executable project where it works just fine so i am pretty sure it´s not about my linking or binary files of the libary.
Are there any special properties to link a libary to a dll?
My minimal sample code:
C++ Mainfile:
#include "main.h"
#include <Windows.h>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Info.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
namespace CurlDll
{
void CallHost::Try()
{
curlpp::Easy request;
request.setOpt(new curlpp::options::UserAgent("Mozilla/4.0"));
request.setOpt(new curlpp::options::AutoReferer(true));
request.setOpt(new curlpp::options::FollowLocation(true));
request.setOpt(new curlpp::options::Url("http://xml.utrace.de"));
request.perform();
MessageBox(0,"lololowwwwwwwwwwwl", "wqgqwwwwwgq", MB_OK |MB_ICONINFORMATION);
}
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason)
{return 1;}
Header file:
#ifdef MAIN_EXPORTS
#define MAIN_API __declspec(dllexport)
#else
#define MAIN_API __declspec(dllimport)
#endif
#include <iostream>
namespace CurlDll
{
class CallHost
{
public:
static MAIN_API void Try();
};
}
i get following linking errors #drescherjm
(47 , i will just post a few, i think that shouls be enough)
ERROR 2 error LNK2001: unresolved external Symbol
"__imp__WSAStartup#8".
ERROR 11 error LNK2001: unresolved external Symbol
"__imp__WSAGetLastError#0".
ERROR 33 error LNK2001: unresolved external Symbol
"__imp__setsockopt#20".
The linker errors are telling you that the linker cannot find definitions for these functions: WSAStartup, WSAGetLastError, setsockopt. These functions are defined in the import library Ws2_32.lib. You need to supply that import library to the linker.
This information is given in the documentation for the functions. For instance, the documentation for WSAStartup. At the bottom of the documentation topic is a table listing requirements. Note the required library, Ws2_32.lib.
The symbols WSAStartup, WSAGetLastError and setsocketopt are part of the Windows API, in Ws2_32.lib (e.g. see http://msdn.microsoft.com/en-gb/library/windows/desktop/ms742213(v=vs.85).aspx )
You should include ws2_32.lib as an additional library when you link your DLL. If you're using Visual Studio, it's likely that the search path should already find it; just add it as an additional library.
So actually, I suspect you're not using the exact same linker options compared to your .exe
If you're building a .exe or a .dll, the linker needs to ensure it can resolve ALL known symbols at link time.

Visual C++ can't open include file 'iostream'

I am new to C++. I just started! I tried a code on Visual C++ 2010 Express version, but I got the following code error message.
------ Build started: Project: abc, Configuration: Debug Win32 ------
ugo.cpp
c:\users\castle\documents\visual studio 2010\projects\abc\abc\ugo.cpp(3): fatal error C1083: Cannot open include file: 'iostream': No such file or directory
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This is the code:
// first.cpp -- displays a message
#include <iostream> // A PREPROCESSOR directive
int main(void) // Function header
{ // Start of a function body
using namespace std;
cout << "Come up and C++ me sometime.\n"; // Message
// Start a new line
cout << "Here is the total: 1000.00\n";
cout << "Here we go!\n";
return 0;
}
Replace
#include <iostream.h>
with
using namespace std;
#include <iostream>
Some things that you should check:
Check the include folder in your version of Visual Studio (in "C:\Program Files\Microsoft Visual Studio xx.x\VC\include", check for the file which you are including, iostream, make sure it's there).
Check your projects Include Directories in <Project Name> → Properties → Configuration Properties → VC++ Directories → Include Directories (it should look like this: $(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSdkDir)include;$(FrameworkSDKDir)\include;)
Make sure that you selected the correct project for this code
(menu File → New → Project → Visual C++ → Win32 Console Application)
Make sure that you don't have <iostream.h> anywhere in your code files, Visual Studio doesn't support that (in the same project, check your other code files, .cpp and .h files for <iostream.h> and remove it).
Make sure that you don't have more than one main() function in your project code files (*in the same project, check your other code files, .cpp and .h files for the* main()` function and remove it or replace it with another name).
Some things you could try building with:
Exclude using namespace std; from your main() function and put it after the include directive.
Use std::cout without using namespace std;.
I had this exact same problem in Visual Studio 2015. It looks like as of Visual Studio 2010 and later you need to include #include "stdafx.h" in all your projects.
#include "stdafx.h"
#include <iostream>
using namespace std;
The above worked for me. The below did not:
#include <iostream>
using namespace std;
This also failed:
#include <iostream>
using namespace std;
#include "stdafx.h"
You are more than likely missing $(IncludePath) within Properties → VC++ Directories → Include Directories.
Adding this should make iostream and others visible again. You probably deleted it by mistake while setting up your program.
If your include directories are referenced correctly in the VC++ project property sheet → Configuration Properties → VC++ directories → Include directories, the path is referenced in the macro $(VC_IncludePath).
In my Visual Studio 2015 this evaluates to:
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include"
using namespace std;
#include <iostream>
That did it for me.
It is possible that your compiler and the resources installed around it were somehow incomplete. I recommend re-installing your compiler: it should work after that.
I got this error when I created an 'Empty' console application in Visual Studio 2015. I recreated the application, leaving the 'Empty' box unchecked. It added all of the necessary libraries.
Make sure you have Desktop Development with C++ installed.
I was experiencing the same problem, because I only had Universal Windows Platform Development installed.
Microsoft Visual Studio is funny. When you're using the installer, you must checkbox a lot of options to bypass the .NET framework (somewhat) to make more C++ instead of C# applications, such as the CLR options under desktop development... in the Visual Studio installer.... the difference is the C++ Win32 console project or a C++ CLR console project.
So what’s the difference? Well, I'm not going to list all of the files CLR includes, but since most good C++ kernels are in Linux... So CLR allows you to bypass a lot of the Windows .NET framework because Visual Studio was really meant for you to make applications in C#.
Here’s a C++ Win32 console project!
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
return 0;
}
Now here’s a C++ CLR console project!
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine("Hello, World!");
return 0;
}
Both programs do the same thing .... the CLR just looks more frameworked class overloading methodology, so Microsoft can great its own vast library you should familiarize yourself with if so inclined.
Keywords (C++)
Other things you'll learn from debugging to add for error avoidance:
#ifdef _MRC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
If you created an environment variable with the name IncludePath, try renaming it to something else.
This name will override $(IncludePath) inside project properties.
Quick fix for small programs:
Add: #include <cstdlib>
In my case, my Visual Studio 2015 installed without selecting C++ package, and Visual Studio 2017 is installed with the C++ package. If I use Visual Studio 2015, opening a C++ project will show this error, and using Visual Studio 2017 will be no error.
I had this problem too. I used this code (before main();) in Visual Studio 2022, and it turned OK:
#include "pch.h"
#include <iostream>
using namespace std;
using namespace winrt;
using namespace Windows::Foundation;
In my case, the error occurred when I created a file in VS Code, without giving the .cpp extension. It resolved when I renamed it with the .cpp.
// first.cpp -- displays a message
#include <iostream> // a PREPROCESSOR directive
using namesapce std;
int main() // function header
{ // start of a function body
///using namespace std;
cout << "Come up and C++ me sometime.\n"; // message
// start a new line
cout << "Here is the total: 1000.00\n";
cout << "Here we go!\n";
return 0;
}