Dynamic loading DLL in C++ for Linux Platform - c++

I am trying a Create a Linux C++ project using the same header and .cpp files from a Windows C++ project using Visual Studio. I am using below function to load a DLL dynamically in Windows
HINSTANCE hGetProcIDDLL = LoadLibraryA(sDllPath.c_str());
GetPluginInfoList GetInfoList = (GetPluginInfoList)GetProcAddress(hGetProcIDDLL, "GetPluginInfoList");
I think these functions hail from <windows.h>
When it comes to Linux C++ project I am not getting those functionalities.
For Linux C++, what is the replacement for HINSTANCE and LoadLibraryA?

I am posting my answer here. Thanks everyone for the support
typedef CPluginInfoList(*GetPluginInfoList)(void);
#if _WINDLL
HINSTANCE hGetProcIDDLL = LoadLibraryA(sDllPath.c_str());
#else
void* hGetProcIDDLL = dlopen(sDllPath.c_str(), RTLD_LAZY);
#endif
#if _WINDLL
GetPluginInfoList GetInfoList = (GetPluginInfoList)GetProcAddress(hGetProcIDDLL, "GetPluginInfoList");
#else
GetPluginInfoList GetInfoList = (GetPluginInfoList)dlsym(hGetProcIDDLL, "GetPluginInfoList");
#endif
GetInfoList(); //Function Call

Related

Win32 API Sample Code gives Compiling error "error C2065: 'HINSTANCE': undeclared identifier"

I was following the guide on Microsoft about Windows programming https://learn.microsoft.com/en-us/windows/win32/learnwin32/learn-to-program-for-windows and after learning the concepts and the naming conventions I got to the coding part.
this the code I copied from the guide.
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);
but when I try to compile this it gives me an error about WINAPI and HINSTANCE undefined
I know that windows.h contains child files that I think can't be included individually but when I try to include windef.h after windows.h as it contains the typedef of WINAPI it gives alot of other errors but the WINAPI error is gone.
I am using VS2017 version 15.9.36 With Windows 10 SDK (10.0.17134.0)
Visual Studio installed packages
I am so new to windows programming and idk what am I doing wrong I hope I provided enough info.

Using a win32 DLL library on a Universal Windows App C++

I was able to compile a dll library based by following the instructions indicated from this tutorial http://tutplusplus.blogspot.com/2011/04/c-tutorial-create-dll-in-vc-20102008.html. I compiled it and there are no problems. I also tested my dll library on an empty win32 console application solution and it compiled fine by adding the header, source file and changing the linker settings. The problem is I can't use the dll library this time on a Universal Windows App solution. I added the header and source files as well as changed the linker settings.
When I compiled the project solution it asked me to include pch.h to the source file of my library so I added the line #include "pch.h" on top.
After that, other compiler errors came up.
'CreateFile': identifier not found and 'SetCommState': identifier not found
I believe the compiler for my current project solution recompiled everything again without considering the dll library's compilation. How can I make the project solution just compile its own source and header files?
Lib Header:
#pragma once
#include <Windows.h>
namespace nmspace
{
class SerialPort
{
public:
static __declspec(dllexport) int connect();
};
}
Lib Source:
#include <iostream>
using namespace std;
#include "SimpleH.h"
#include <Windows.h>
namespace nmspace
{
int SerialPort::connect()
{
int error = 0;
DCB dcb;
memset(&dcb, 0, sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = 57600;
dcb.Parity = NOPARITY;
dcb.fParity = 0;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
HANDLE m_serialPortHandle = CreateFile(L"COM7", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL, NULL);
if (m_serialPortHandle != INVALID_HANDLE_VALUE) {
if (!SetCommState(m_serialPortHandle, &dcb))
error = 2;
}
else
error = 1;
return error;
}
}
You won't be able to use this DLL because it uses SetCommState function that is only available for desktop applications. You should lookup functions that you are trying to use in a list of desktop APIs available for UWP applications.
As for CreateFile it is not a function, but a macro expanding to CreateFileA or CreateFileW depending on project Unicode settings. You should use CreateFile2 instead which is available for both Desktop and UWP applications.
The reason is very simple. WinRT doesn't support the complete WinApi! You must use the WinRT function from the UWP Namespace
Just read the docs to CreateFile. In the documentations you find under requirements:
Minimum supported client Windows XP [desktop apps only] Minimum
supported server Windows Server 2003 [desktop apps only]
This tells you that CreateFile isn't allowed in WinRT.
AFAIK accessing a serial port from WinRT isn't possible. Just google. Here is an answer that states this. Same here

Embarcadero Builder C++ XE5 data execution prevention compiler

Hope this isn't an obvious issue. I've recently run in exceptions due to a lack of Data Execution Prevention (DEP) support in our 32-bit exe on a Windows 2008 R2 server. Adding the exe to the DEP exclusion list, solved the issue as a workaround.
I would like to compile with support for DEP, but can't find any indication on how to do this in Builder XE5 c++. Is this possible? I have found some vague suggestions for Delphi, but nothing definitive.
Any ideas?
AFAIK, C++Builder doesn't have the same DEP options that Delphi has. You will have to either
use an external PE editor to modify the PE flags of your compiled EXE file.
call SetProcessDEPPolicy() at runtime, such as at the top of your main()/Winmain() function:
void EnableDEP()
{
const DWORD PROCESS_DEP_ENABLE = 0x00000001;
typedef BOOL WINAPI (*LP_SPDEPP)(DWORD);
LP_SPDEPP SetProcessDEPPolicy = (LP_SPDEPP) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "SetProcessDEPPolicy");
if (SetProcessDEPPolicy != NULL)
SetProcessDEPPolicy(PROCESS_DEP_ENABLE);
}
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
EnableDEP();
...
}

Windows API function not available while it should be

I'm trying to create a symbolic link using Windows's API, however the CreateSymbolicLink function doesn't seem to exist even though I've included windows.h.
I'm using Qt Creator. I am able to use other functions from the API except this one.
Can anyone explain why?
I'm on Windows 8.1.
Edit:
My code:
#include "windows.h"
bool createSymbolicLink(const int &type, const QString &linkPath, const QString &targetPath) {
DWORD windows_Type = type;
wchar_t* windows_LinkPath = new wchar_t[linkPath.length()];
linkPath.toWCharArray(windows_LinkPath);
wchar_t* windows_TargetPath = new wchar_t[targetPath.length()];
targetPath.toWCharArray(windows_TargetPath);
return CreateSymbolicLinkW(windows_LinkPath, windows_TargetPath, windows_Type) != 0;
}
The error: 'CreateSymbolicLinkW' was not declared in this scope
CreateSymbolicLink has a minimum requirement of Vista, as documented at MSDN.
This means that you need to specify by way of a conditional define that you are targeting Vista and up. You can find more documentation here:
Using the Windows Headers.
Modifying WINVER and _WIN32_WINNT.
So, you could include these defines before you include the windows header file.
#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
If this does not help then you have out of date SDK and will need to switch to a more up to date SDK.

Can't Include winhttp.h (with code::blocks/mingw) c++

I've been trying to include winhttp.h and I get this error:
Winhttp.h: No such file or directory
Mingw doesn't have it, how would I add it?
You can use runtime dynamic linking to link to the function(s) you want directly. You can't use the plain winhttp.h that ships with the Windows SDK because it contains Microsoft-specific features. You could also compile with Visual C++ 2010 Express Edition which would include the header you want.
Hope that helps :)
I have copied the header from windows and it worked fine with the following addition:
#define __in
#define __out
#define __out_bcount(x)
#define __in_ecount(x)
#define __inout
#define __out_ecount_full_opt(x)
#define __in_opt
#define __out_data_source(x)
#include <winhttp.h>
hope this helps.
GET: https://dev.eclipse.org/svnroot/technology/org.eclipse.higgins/trunk/app/org.eclipse.higgins.tcpserver/src/Third-party/VS2008/winhttp.h
dlltool -z winhttp.def --export-all-symbol winhttp.dll
dlltool -k -d winhttp.def -l libwinhttp.a
Link against the libwinhttp.a you just generated.