I've been trying to create a DLL and link the DLL to my program but every time I try my program can't find the function. The DLL loads fine but the function cant be found.
Program:
#include <iostream>
#include <windows.h>
using namespace std;
typedef void (*HelloPtr)();
int main() {
HelloPtr hello;
HINSTANCE hDll = LoadLibrary("dll.dll");
if(hDll)
{
hello = (HelloPtr)GetProcAddress(hDll, "hello");
if(hello) {
hello();
} else {
// Error code here
}
}
return 0;
}
dllmain.cpp
#include "dll.h"
#include <windows.h>
DLLIMPORT void hello()
{
MessageBox(NULL, "Hey", "", MB_OK);
}
DllClass::DllClass()
{
}
DllClass::~DllClass ()
{
}
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
/* Returns TRUE on success, FALSE on failure */
return TRUE;
}
dll.h
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport) void hello(void)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllexport) void hello(void)
#endif /* Not BUILDING_DLL */
class DLLIMPORT DllClass
{
public:
DllClass();
virtual ~DllClass(void);
// Says hello world
DLLImport void hello(void);
private:
};
#endif /* _DLL_H_ */
I'd like to know what I'm doing wrong so I can document it and learn.
Thanks