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".
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);
}
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.
//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");
I'm currently learning c++ from scratch, I've previously developed apps with C# using Visual Studio but I'm a total noob with C++, I'm trying to make a small console exe that releases and renews the ip while practicing using headers and differents .cpp files.
The issue is that when I run the local windows debugger from visual studio 2015 the code runs perfectly and does everything I'm trying to. But when I build and try to run the .exe file it goes into an infinite loop stating endlessly the output from the std::cout <<"Realizando ipconfig Release", I have localized the issue to when it tries to run the system("ipconfig /release"). Why does this happen? and How can I fix it?
This is the header
#pragma once
#ifndef HeaderIp
#define HeaderIp
int Release();
int Renew();
#endif // !HeaderIp
This is the release.cpp
#include <stdlib.h>
int Release()
{
if (system(nullptr)==0)
{
return 0;
}
else
{
system("ipconfig /release");
return 1;
}
}
This is the renew.cpp
#include <stdlib.h>;
int Renew()
{
if (system(nullptr)==0)
{
return 0;
}
else
{
system("ipconfig /renew");
return 1;
}
}
and finally this is the ipconfig.cpp
#include <iostream>
#include <stdlib.h>
#include "HeaderIp.h"
#include <stdio.h>
int main()
{
std::cout << "Realizando ipconfig Release" << std::endl;
int i = 0; //Bit de informacion de status de CPU
i = Release();
if (i == 0)
{
std::cout << "Error al liberar IP" << std::endl;
system("pause");
exit;
}
else
{
std::cout << "Ip Liberado correctamente" << std::endl;
}
i = Renew();
if (i == 0)
{
std::cout << "Error al renovar IP" << std::endl;
system("pause");
exit;
}
else
{
std::cout << "Ip renovado correctamente" << std::endl;
}
system("pause");
return 0;
}
It's unusual, and indeed generally discouraged to use system in C++. If you were to manage DHCP leases using IpReleaseAddress and IpRenewAddress, instead, you might find your problem disappears.
You can use std::cin.sync() instead of system("pause"), too.
exit; probably doesn't do what you want it to, as you're only referring to the function, you're not calling it, so... it'll do nothing.
The semicolon in #include <stdlib.h>; is an error, and you should be including <cstdlib> and <cstdio> rather than <stdlib.h> and <stdio.h>.
I have written a dll that uses an abstract interface to allow access to the c++ class inside. When I load the library dynamically at run-time using the LoadLibrary() function using a simple console created in eclipse using g++ and call a function from within the dll I get the correct values returned. However when writing the same console program using qt-creator qt5 with the g++ compiler I get completely different results that are not correct.
All of this was written under Windows 7 64-bit but using the x86 side of it for the programs.
The code that calls the dll from eclipse looks as follows:
HMODULE hMod = ::LoadLibrary("libTestDll.dll");
if (hMod) {
cout << "Library Loaded" << endl;
CreateITestType create = (CreateITestType) GetProcAddress(hMod,
"GetNewITest");
ITest* test = create();
std::cout << test->Sub(20, 5) << std::endl;
std::cout << test->Sub(20, 3) << std::endl;
std::cout << test->Add(20, 5) << std::endl;
std::cout << test->Add(13, 4) << std::endl;
DeleteITestType del = (DeleteITestType) GetProcAddress(hMod,
"DeleteITest");
del(test);
test = NULL;
FreeLibrary(hMod);
}
This returns:
Library Loaded
15
17
25
17
The code that calls the dll from qt looks as follows:
HMODULE hMod = LoadLibrary(TEXT("libTestDll.dll"));
if(hMod)
{
CreateITestType create = (CreateITestType)GetProcAddress(hMod, "GetNewITest");
DeleteITestType destroy = (DeleteITestType)GetProcAddress(hMod, "DeleteITest");
ITest* test = create();
std::cout << test->Sub(20, 5) << std::endl;
std::cout << test->Sub(20, 3) << std::endl;
std::cout << test->Add(20, 5) << std::endl;
std::cout << test->Add(13, 4) << std::endl;
destroy(test);
test = NULL;
FreeLibrary(hMod);
}
And This returns:
1
-17
25
24
Both programs have the imports:
#include <windows.h>
#include <iostream>
#include "TestDll.h"
And finally the functions are implemented as follows:
int Test::Add(int a, int b)
{
return (a+b);
}
int Test::Sub(int a, int b)
{
return (a-b);
}
My question is where is the difference coming from seeing as the two programs are identical in both code and compiler, and how can this be fixed?
Did you also rebuild the DLL with qt-creator qt5 with the g++ compiler? If not, then what you've discovered is that if you don't use the exact same compiler, compiler options and settings, defines, and pretty much every other aspect of the build system, C++ interfaces are not typically ABI compatible.