Function pointer returning null

Hello!

I'm trying to employ DLLs by loading them at run time, but have run into a snag that I have not yet solved.

Essentially, one of my function pointers looks a bit like this (ignoring string type):

1
2
3
4
5
6
7
8
9
10
11
string (*GetString)() = 0;

HINSTANCE hinstLib = LoadLibrary("my.dll");

if (hinstLib == NULL)
{
	cout << "Failed to load DLL." << endl;
	return;
}

GetString = (string(__cdecl*)(void))GetProcAddress(hinstLib, "getString");


The code inside the DLL itself looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef ASEXPORT
#define DLLIMPORTOREXPORT dllimport
#else
#define DLLIMPORTOREXPORT dllexport
#endif

#ifdef __cplusplus    // If used by C++ code, 
extern "C" {          // we need to export the C interface
#endif

_declspec(DLLIMPORTOREXPORT) string getString() return{"MyString"};

#ifdef __cplusplus    // If used by C++ code, 
}
#endif 



However GetString keeps returning a null pointer. I'm finding this a little hard to debug.

Any suggestions of what to be looking at?

Many thanks for your time,

Luke
Windows probably can't find the DLL. Make sure it's either in the working directory or in a directory in the PATH environment variable.

By the way, line 11 of your second snippet shouldn't compile.
Last edited on
You may well be right, and I'll look into it more- but then it should be finding it without difficulty - because the application actually generates a list of .dll files in the working directory which is where it gets the name of the DLL in LoadLibrary (the code above is a smaller, simplified snippet of a considerably larger application).

I've already rewritten the DLL to look more like this:

Header:
1
2
3
4
5
6
7
8
9
10
11
#include <string>

using namespace std;

#ifndef ASEXPORT
#define DLLIMPORTOREXPORT dllimport
#else
#define DLLIMPORTOREXPORT dllexport
#endif

extern "C"_declspec(DLLIMPORTOREXPORT) string getString();


Cpp
1
2
3
4
5
6
7
8
9
10
#include "stdafx.h"
#include "amplify.h"

#define ASEXPORT


string getString()
{
	return "myString";
}



Thanks again,

Luke
An update:

It's finding the DLL - but the function pointer is still not working. I really don't know what left to do.
Last edited on
Topic archived. No new replies allowed.