Load dll from folder inside .exe

Nov 20, 2017 at 7:24pm
Goodday,

i have run into a small problem. i have a simple program and dll file. Both are working perfectly. But when i try to load the dll when it is in a folder inside the folder containing the .exe file, it doesn't load.

i tried doing it with this

 
  HMODULE mydll = LoadLibrary("data\\mydll.dll");

but it did not work.
I hope somebody can help me.

,krreisys
Nov 20, 2017 at 8:59pm
If LoadLibrary fails you need to call GetLastError.
If the dll is not found try to use an absolute path
Nov 21, 2017 at 2:43pm
thank you for your reply.
I did a little bit of research and what i want to do is explicit dll linking.
This is a lot harder then implicit which is fairly easy.

the code of my dll

textvalid.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <windows.h>

using namespace std;

#define dllexport __declspec(dllexport)

class dllexport text_valid
{
public:
    bool has_invalid_chars(string input);
    bool has_space(string input);
    string remove_invalid_characters(string input);
};


textvalid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "textvalid.h"

#include <string>

using namespace std;

bool text_valid::has_invalid_chars(string input)
{
    for(int a = 0; a < input.length(); a++){if(!isalpha(input.at(a)) && !isdigit(input.at(a)) && !isspace(input.at(a))){return true; break;}}
    return true;
}

bool text_valid::has_space(string input)
{
    for(int a = 0; a < input.length(); a++){if(isspace(input.at(a))){return true; break;}}
    return false;
}

string text_valid::remove_invalid_characters(string input)
{
    string new_input;
    for(int a = 0; a < input.length(); a++)
    {
        if(isalpha(input.at(a))){new_input += input[a];}
        if(isdigit(input.at(a))){new_input += input[a];}
        if(isspace(input.at(a))){new_input += input[a];}
    }
    return new_input;
}


the code from the project that needs to access the textvalid.dll

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "program_2.h"

#include <windows.h>
#include <iostream>
#include <string>

using namespace std;

typedef string (*func_invalid_char)(string);

void program_start::load_dll()
{
    HINSTANCE loadlib = LoadLibrary(TEXT("data\\textvalid.dll"));

    if(loadlib == NULL)
    {
        cout << "LOADING LIBRARY FAILED\n"; 
    }
    else
    {
        cout << "succes!!!!" << '\n';
        func_invalid_char remove_invalid_characters = (func_invalid_char)GetProcAddress(loadlib, "_ZN10text_valid25remove_invalid_charactersENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE");
        if(remove_invalid_characters)
        {
            string iets;
            string test = "hallo!!!!"; iets = remove_invalid_characters(test);
            cout << "succes!!!!\n";
        }
        FreeLibrary(loadlib);
    }
}


when i execute this program, it compiles fine. i got the function name from dependency walker the normal function name doesn't work. I get a direct error and the program needs to shut down. It clearly finds the dll and the function because it gives the message that it is succesfull.

I hope somebody can help me.


Friendly greetings, krreisys
Nov 21, 2017 at 3:41pm
I think the problem is that you try to export a std::string from a dll.
Many people on stackoverflow.com call it a bad idea.

Why don't you use a simple interface in your dll like that:
1
2
3
4
  bool has_invalid_chars(const char *input);
  bool has_space(const char *input);
  // return the length from output
  int remove_invalid_characters(const char *input, char *output);


Nov 21, 2017 at 3:45pm
The problem is that you call remove_invalid_characters() as a free function but it is a member function of a class. Hence it needs an object of that class which will be passed as a pointer as the first parameter.
Nov 21, 2017 at 4:49pm
@thomas1965 @coder777 Thank you!
It works now! The problem was that the functions were declared inside a class.
I removed the class and now everything works fine!!!!!

Many thanks!!!!
Topic archived. No new replies allowed.