Load a variable from DLL to .exe

Hey all,

I've made an experimental .dll (I'm new to dlls) that is supposed to export a variable to the calling .exe

I've tried this(excerpt):
DLL:extern __declspec(dllexport) int mytestvar = 125;
EXE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <windows.h>
#include <conio.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
    HINSTANCE hinstLib; 
    BOOL fFreeResult ;
 
    hinstLib = LoadLibrary(TEXT("MyDll.dll")); 

    if (hinstLib != NULL) 
    { 
  	
     GetProcAddress(hinstLib, "mytestvar");
     extern __declspec(dllimport) int* mytestvar;
     std::cout << mytestvar << std::endl;
 
     fFreeResult = FreeLibrary(hinstLib); 
    } 
	_getch();
	return 0;
}


This results in a linker error (2001, 1120).
I'm not sure how to do this, and MSDN only provides sparse examples.

Could someone help me with this, please?

Thanks in advance!
Last edited on
You need to show the full error text as generated by the linker.
3>MyDLL.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) int * mytestvar" (__imp_?mytestvar@@3PAHA)
3>F:\C++\Dll exp\Release\MyDLL.exe : fatal error LNK1120: 1 unresolved externals

Thats what I get.
Last edited on
That's a bit of a mess. Forget exporting data for a start.

I'll assume you're using a Microsoft C compiler. The export/import syntax varies across compilers. If you don't have one, you can download an Express Edition of something.

In the TEST.DLL, create a function in a .c file (not .cpp) like:
1
2
3
4
__declspec(dllimport) const char* GetText(void)
{
    return "hello world";
}


In the EXE your code should look something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <windows.h>
#include <stdio.h>

typedef const char* GetTextFunc(void);

int main()
{
    HINSTANCE h = LoadLibrary("TEST.DLL");
    if (h)
    {
        GetTextFunc* GetText = GetProcAddress(h, "GetText");
        if (GetText)
        {
            printf("%s\n", GetText());
        }
        FreeLibrary(h);
    }

    return 0;
}


I just typed this into the browser, so you'll have to fix any syntax errors.
Last edited on
First of all, thanks kbw!!

You are quite right, I am using Visual Studio 2010 Professional, Dreamspark edition.

I managed to compile the code, first as c then also as c++ by exporting the c interface (from msdn).
NOTE: It is "__declspec(dllexport)" and not dllimport!!

This is the final working c++ version:

DLL:
1
2
3
4
5
6
7
8
9
10
11
12
#ifdef __cplusplus
extern "C" {
#endif

__declspec(dllexport) const char* GetText(void)
{
    return "Hello World!";
}

#ifdef __cplusplus
}
#endif 


EXE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <windows.h>
#include <stdio.h>
#include <conio.h>

typedef const char* GetTextFunc(void);

int _tmain(int argc, _TCHAR* argv[])
{
    HINSTANCE h = LoadLibrary(TEXT("DLLx1.DLL"));
    if (h)
    {
        GetTextFunc* GetText = (GetTextFunc*)GetProcAddress(h, "GetText");
        if (GetText)
        {
            printf("%s\n", GetText());
        }
        FreeLibrary(h);
    }

	_getch();
    return 0;

}
Last edited on
Topic archived. No new replies allowed.