//DLL Code
#include <stdio.h>
extern "C"
{
__declspec(dllexport) void DisplayHelloFromDLL()
{
printf("Hello from DLL !\n");
}
}
//Program Accessing DLL
#include<windows.h>
#include<iostream>
#include<conio.h>
typedef void (*DisplayHelloFromDLLFuncPtr)();
using namespace std;
int main()
{
HINSTANCE hGetProcIDDLL = LoadLibrary("L:\\C_Learning\\Library\\MyLib\\Debug\\MyLib.dll");
if (!hGetProcIDDLL)
{
cout << "\nCould Not The Library";
return EXIT_FAILURE;
}
else
{
cout << "\nDLL is Loaded";
}
DisplayHelloFromDLLFuncPtr LibMainEntryPoint=(DisplayHelloFromDLLFuncPtr)GetProcAddress(hGetProcIDDLL, "DisplayHelloFromDLL");
if (!DisplayHelloFromDLL)
{
cout << "\nCould not locate the function";
return EXIT_FAILURE;
}
cout << DisplayHelloFromDLL();
return EXIT_SUCCESS;
_getch();
return 0;
}
Code executes till cout statement in else condition.
Receive Error while compiling for Function in DLL.
Error received 'DisplayHelloFromDLL': undeclared identifier
Ran Depends.exe which confirms about the function availability in DLL address space.
DLL and Sample Program is compiled with 32-bit environment.
6.Program sole purpose is to call function C DLL and print Hello From DLL message.
Any Suggestions ?
You named the variable holding the "DisplayHelloFromDLL" function pointer as "LibMainEntryPoint":
DisplayHelloFromDLLFuncPtr LibMainEntryPoint=(DisplayHelloFromDLLFuncPtr)GetProcAddress(hGetProcIDDLL, "DisplayHelloFromDLL");
but then you try to use it with different name (DisplayHelloFromDLL):
if (!DisplayHelloFromDLL) ...
Be consistent with the variable names, and the code should work.
change it to:
DisplayHelloFromDLLFuncPtr DisplayHelloFromDLL=(DisplayHelloFromDLLFuncPtr)GetProcAddress(hGetProcIDDLL, "DisplayHelloFromDLL");
Related
I want to create a C++ program that loads a dll(a.dll). It should run in
combination with wine in Linux. In the dll a function foo will be called
which takes two strings and an integer. foo returns an integer. The dll
is located in the same directory as the exe. Unfortunately the dll file
is not found and I don't know why. I have also tried "./a.dll" under
Linux. Can anyone give me some advice on this?
#include <iostream>
#include <string>
#include "windows.h"
#include <tchar.h>
typedef int(*func_ptr)(std::string, std::string, int);
int main()
{
func_ptr function1 = NULL;
HMODULE hGetProcIDDLL = LoadLibrary(L"a.dll");
if (hGetProcIDDLL != NULL)
{
std::cout << "Found!";
}
else
{
std::cout << "File not found!";
return 0;
}
function1 = (func_ptr)GetProcAddress(hGetProcIDDLL, "foo");
if (function1 != NULL)
{
std::cout << function1("input-file.txt","output-file.txt",0);
}
else
{
std::cout << "Function not found";
}
FreeLibrary(hGetProcIDDLL);
std::cin.get();
return(0);
}
So I was trying to make a program that dynamically creates a dll file and loads it into the program and calls it.
Here is my code for
createFile:
void createFile(std::string data, std::string name) {
std::ofstream file;
name += ".cpp";
file.open(name,std::ofstream::out);
if (file.is_open()) {
file << data;
std::cout << "File saved successfully" << std::endl;
file.close();
}
else {
std::cout << "Error opening file" << std::endl;
exit(1);
}
}
compile:
void compile(std::string name) {
std::string temp;
temp = "c++.exe " + name + ".cpp -o dll/" + name + ".dll -shared -fPIC";
system(temp.c_str());
}
loadFunc:
typedef void (*function)(const char*);
void loadFunc(LPCSTR name, LPCSTR func) {
HMODULE temp;
function __cdecl myproc;
temp = LoadLibraryA(name);
if (temp != NULL) {
std::cout << "\nDll is loaded";
myproc = (function) GetProcAddress(temp, func);
if (myproc !=NULL)
{
std::cout << "\nFunction is loaded\n";
(myproc)("Somthing here");
}
// Free the DLL module.
FreeLibrary(temp);
}
}
The problem is when I compile a dll using visual studio 2019 it works just fine, but if I compile a dll file using gcc or c++ or g++ it doesn't load the dll. So I tried using dependency walker to see the difference between two dll files(one made with gcc and another with visual studio). I saw the file structures were different. dll made with gcc had larger size than the one made with visual studio. Can anyone help me with this?
Edit:
So after doing some trial and error, I have found that only dll files that have C libraries work. I don't know why. I tried using iostream earlier, but it always failed to load and on the other hand when I used stdio.h it worked fine.
dllfile.cpp(with iostream):
#ifdef __cpluplus
extern "C" {
#endif
#include <iostream>
__declspec(dllexport) void print(const char *x){
std::cout<<x<<std::endl;
}
#ifdef __cplusplus
}
#endif
dllfile.cpp(with stdio):
#ifdef __cpluplus
extern "C" {
#endif
#include <stdio.h>
__declspec(dllexport) void print(const char *x){
printf("%s",x);
}
#ifdef __cplusplus
}
#endif
I'm trying to dynamically load a dll and call a function from it at runtime. I have succeeded in getting a working pointer with GetProcAddress, but the program crashes if the function from the dll uses the stdlib. Here's the code from the executable that loads the dll:
#include <iostream>
#include <windows.h>
typedef int (*myFunc_t)(int);
int main(void) {
using namespace std;
HINSTANCE dll = LoadLibrary("demo.dll");
if (!dll) {
cerr << "Could not load dll 'demo.dll'" << endl;
return 1;
}
myFunc_t myFunc = (myFunc_t) GetProcAddress(dll, "myFunc");
if (!myFunc) {
FreeLibrary(dll);
cerr << "Could not find function 'myFunc'" << endl;
return 1;
}
cout << "Successfully loaded myFunc!" << endl;
cout << myFunc(3) << endl;
cout << myFunc(7) << endl;
cout << myFunc(42) << endl;
cout << "Successfully called myFunc!" << endl;
FreeLibrary(dll);
return 0;
}
Here's code for the dll that actually works:
#include <iostream>
extern "C" {
__declspec(dllexport) int myFunc(int demo) {
//std::cout << "myFunc(" << demo << ")" << std::endl;
return demo * demo;
}
}
int main(void) {
return 0;
}
(Note that the main method in the dll code is just to appease the compiler)
If I uncomment the line with std::cout however, then the program crashes after the cout << "Sucessfully loaded myFunc!" << endl; line but before anything else gets printed. I know there must be some way to do what I want; what do I need to change for it to work?
As discussed in the comments, it turns out that the compiler's demands for a main function were hints that I was inadvertently making a an exe that decptively used the file extension dll, not an actual dll (because I didn't quite understand the compiler options I was using), which in some way messed up the dynamic loading of that assembly.
Im trying to create a C DLL to extend my Lua script which I am using in a C++ application.
My DLL code is in "mylib.h":
#include <iostream>
#include "lua.hpp"
extern "C"
{
static int l_hello(lua_State *L) {
std::cout << "sup lol?" << std::endl;
return 0;
}
static const struct luaL_Reg mylib[] = {
{ "l_hello", l_hello },
{ NULL,NULL }
};
__declspec(dllexport) int luaopen_mylib(lua_State *L)
{
std::cout << "loading my lib" << std::endl;
luaL_newlib(L, mylib);
return 1;
}
}
I build the code mentioned above and put the DLL "mylib.dll" in the application debug folder. When I run the app the program crashes on
local mylib = require "mylib"
with the error "error loading module 'mylib' from file 'C:\Users\Username\Documents\Visual Studio 2015\Projects\Project1Tests\Debug\mylib.dll':
The specified module cant be found (translated from another language)."
How do I solve this problem?
I have been asked to use MSFileReader from Thermo Fisher to read RAW files and do some peak picking for mass spectrometry data. I can load the DLL included in the package, but cannot access the functions. I have version 3.0. I am using Visual Studio Community 2015 as my compiler. My code is below.
#include <Windows.h>
#include <iostream>
#include <string>
double SampleWt;
double *pd = &SampleWt;
typedef std::string(*MYPROC)(int);
int main()
{
MYPROC ProcAdd;
HINSTANCE hinstLib = LoadLibrary(L"C:\\Nathan\\DanforthPrj\\MZmine- 2.16\\lib\\vendor_lib\\thermo\\MSFileReaderLib.dll");
if (!hinstLib)
{
std::cout << "\ncould Not Load the Library" << std::endl;
return EXIT_FAILURE;
}
else {
std::cout << "\nSuccess" << std::endl;
}
ProcAdd = (MYPROC)GetProcAddress(hinstLib, "Open");
if (NULL != ProcAdd)
{
std::cout << "\nfinaly" << std::endl;
}
//Resolve the function address 6
FreeLibrary(hinstLib);
return EXIT_SUCCESS;
}
Using a similar process, I can use other DLL files. What could be going wrong? I don't receive any error messages. The name of the function I am tryng to load is simply "Open".