So I been trying to code a bunch of DLL stuff, I've done it before, but for some reason this time it's not working. It crashes.
Now, bear in mind that to simplify the problem, I've created a simple application that will run one function. But even that crashes!
How is this possible? Is it a vista thing? I tried both CodeBlocks and DevCpp.
Main PROGRAM:
Code:
#include <iostream>
#include <windows.h>
typedef int (*DLLFUNCTION)(int);
HINSTANCE g_hDLLinst;
int main(){
DLLFUNCTION Message = NULL;
std::cout << "Running DLL" << std::endl;
g_hDLLinst = LoadLibrary("test.dll");
std::cout << "Library Loaded" << std::endl;
if(g_hDLLinst != NULL){
Message = (DLLFUNCTION)GetProcAddress(g_hDLLinst, "Message");
}
std::cout << "Message Loaded" << std::endl;
int x = Message(5);
if(x == 5){
std::cout << "Message Worked" << std::endl;
}
FreeLibrary(g_hDLLinst);
return 0;
}
DLL:
MAIN.H:
Code:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#define DLL_EXPORT __declspec(dllexport)
#ifdef __cplusplus
extern "C"
{
#endif
int DLL_EXPORT DLLMessage(int x);
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
MAIN.CPP
Code:
#include "main.h"
// a sample exported function
int DLL_EXPORT DLLMessage(int x){
MessageBox(0, "DLL Worked", "DLL Message", MB_OK | MB_ICONINFORMATION);
return x;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved){
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
I put both the dll and the exe into the same folder, and it does:
Running DLL
Library Loaded
Message Loaded
Crash.......