Handmade Hero»Forums»Code
107 posts
Problem loading a funtion from DLL without Lib file
Edited by C_Worm on Reason: Initial post
Hello!

Im trying to load a function from a DLL only, however GetProcAddress() returns NULL.
I just wanted to try loading it in another way than what casey did with all that:
#define -> typedef thingy... :)


Person.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>

struct Person
{
	char *name;
	int age;
};

extern "C" 
void printAge(Person *p);

typedef void PFNPRINTAGE(Person *p);

void printAgeSTUB(Person *p)
{
}


Person.cpp
1
2
3
4
5
6
7
#include "Person.h"

extern "C" 
void printAge(Person *p)
{
	printf("\nAge of Person: %d\n", p->age);
}


main.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
#include <windows.h>
#include <stdlib.h>
#include <string.h>

#include "Person.h"

int main() 
{

	Person p1 = {};
	p1.age = 11;

	HMODULE PersonDLL = LoadLibrary("Person.dll");
	PFNPRINTAGE *printAge = printAgeSTUB;

	if(PersonDLL)
	{
		
		printAge = (PFNPRINTAGE*)GetProcAddress(PersonDLL, "printAge");
		DWORD err = GetLastError();
		printAge(&p1);
	}



	FreeLibrary(PersonDLL);

	return 0;




b.bat

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@echo off

IF NOT EXIST ..\build mkdir ..\build

pushd ..\build

cl -nologo -Zi ..\src\Person.cpp -LD
cl -nologo -Zi ..\src\playGround.cpp /link -incremental:no user32.lib kernel32.lib winmm.lib gdi32.lib

popd

del *.cpp~ *.bat~ *.un~ *.txt~
Mārtiņš Možeiko
2559 posts / 2 projects
Problem loading a funtion from DLL without Lib file
You did not instruct linker to export function from dll file.
You can do in two ways. One with compiler directive:
1
2
3
extern "C"
__declspec(dllexport)
void printAge(Person *p);


Another with linker argument:
1
cl.exe ... /link /export:printAge


You can verify if DLL has this symbol exported by using dumpbin.exe utility:
1
dumpbin /exports Person.dll

If it does not show your "printAge", that means something was not done properly.
107 posts
Problem loading a funtion from DLL without Lib file
mmozeiko
You did not instruct linker to export function from dll file.
You can do in two ways. One with compiler directive:
1
2
3
extern "C"
__declspec(dllexport)
void printAge(Person *p);


Another with linker argument:
1
cl.exe ... /link /export:printAge


You can verify if DLL has this symbol exported by using dumpbin.exe utility:
1
dumpbin /exports Person.dll

If it does not show your "printAge", that means something was not done properly.



So you MUST create a .lib file aswell, else you can't load function from DLL??

__declspec(dllexport) seems to create the .lib file
Mārtiņš Možeiko
2559 posts / 2 projects
Problem loading a funtion from DLL without Lib file
Edited by Mārtiņš Možeiko on
You don't need to use .lib if you are loading .dll at runtime. Linker always creates it if you have exported symbols. So you can use .lib to link to dll at link time, if you want. It's your choice what to do.
107 posts
Problem loading a funtion from DLL without Lib file
mmozeiko
You don't need to use .lib if you are loading .dll at runtime. Linker always creates it if you have exported symbols. So you can use .lib to link to dll at link time, if you want. It's your choice what to do.


Okay, thanks!! :)