Hey Guys,
I am trying to call a function in my DDL file

char DLL[1024];
	_getcwd(DLL, 1024);
	strcat(DLL, "\\LoremIpsum.dll");

	HMODULE Test = LoadLibrary(DLL);

	PluginEntry Entry = (PluginEntry)GetProcAddress(Test, "PluginEntry");
	
	Entry();

	FreeLibrary(Test);

yet on Entry(); I get an access violation in 0xC0000005. What am I doing wrong?

Thanks

Recommended Answers

All 4 Replies

A few things to check:

1) Verify that your library is correctly loaded, i.e. Test != NULL 2) Verify that your function pointer is correctly obtained, i.e. Entry != NULL 3) Make sure that the "PluginEntry" function (in your DLL) is declared with extern "C" 4) Verify that your calling conventions match for both the declaration of the "PluginEntry" function (in your DLL) and for the function pointer type PluginEntry.

Post your code for the DLL function and the definition of the function pointer type PluginEntry if you are unsure how to test the last two points above, we can tell you if it is correct or not.

Entry seems to be equal to NULL when I step through it in debugger

The code for my DLL is just:

#include <windows.h>

__declspec(dllexport) void __stdcall PluginEntry()
{
	MessageBoxA(0, "Plugin entry point!", "", 0);
}

Thanks for your help

You forgot the "extern "C"" part. This is important because the C++ compiler mangles the names of functions (to be able to attach to the function name certain information required in C++, such as the types and number of the parameters the function takes). In C, there is no need for that (function's in C are not type-safe nor overloadable). So, when you ask to get the address of the function named "PluginEntry", the loader will not find it because the C++ compiler that generated your DLL will actually have changed that name to something else (e.g. "__XFS34W!PluginEntry34ZX@24!" or something resembling that). So, in order to tell the C++ compiler not to mangle the name of the function (and use "C" rules for external symbols), you add:

#include <windows.h>

extern "C" __declspec(dllexport) void __stdcall PluginEntry()
{
	MessageBoxA(0, "Plugin entry point!", "", 0);
}

Also, make sure that your PluginEntry function-pointer type is declared with the same calling-convention (stdcall) as:

typedef void __stdcall (*PluginEntry)();
commented: Thanks for professional, accurate, quick and easy to understand feedback! +1

I have opened my DLL with PE Explorer and it seems that the export is now being named "_PluginEntry@0"
So if I change this line

//from this (expected, should work)
PluginEntry Entry = (PluginEntry)GetProcAddress(Test, "PluginEntry");

//with export C
PluginEntry Entry = (PluginEntry)GetProcAddress(Test, "_PluginEntry@0");

//without export C
PluginEntry Entry = (PluginEntry)GetProcAddress(Test, "?PluginEntry@@YGXXZ");

It now works, yay. But any ideas why the export is being named _PluginEntry@0 rather than just PluginEntry.?

Thanks for your help :)

EDIT: From some research I belive that the _PluginEntry@0 is because the symbol is declared but can't be resolved by the linker to the symbol table? Is this correct? And if so do you have suggestion to fix it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.