-
[Resolved]DLL
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.......
-
Is the return value from GetProcAddress NULL? (I'm assuming you imported DLLMessage as Message, but who knows.)
-
Yes it's null, but I don't know why, I didn't do anything weird.
-
That's because you misspelled "DLLMessage" as "Message".
-
Have you ever bothered to crawl through the code one line at a time using a debugger?
Did you think about checking the pointer for NULL before trying to call it?
-
Oh thank you tabstop, I didn't notice that.
"Message" should have been "DLLMessage"
I guess it's been a long night. I was hoping it was something else though, because my other DLL program has everything right and that doesn't work either. I might have to post that as well :O.
And Salem, even if I had known that it was NULL before, or with a debugger, I still wouldn't have seen that error.
-
Seen what error?
Code:
Message = (DLLFUNCTION)GetProcAddress(g_hDLLinst, "Message");
if ( Message == NULL ) {
// DANGER!!! - CLIFF AHEAD
}
int x = Message(5); // Keep on truckin'
-
Elaborating on Salems suggestion, you should always check return values.
Code:
int
main( void )
{
HMODULE
dll = LoadLibrary( "test.dll" );
if( dll != NULL )
{
DLLFUNCTION
function = ( DLLFUNCTION )GetProcAddress( dll, "Message" );
if( function != NULL )
{
int
x = function( 5 );
if( x == 5 )
{
cout << "Success." << endl;
}
else
{
cerr << "Unexpected result!" << endl;
}
}
else
{
cerr << "GetProcAddress failed!" << endl;
}
FreeLibrary( dll );
}
else
{
cerr << "LoadLibrary failed!" << endl;
}
}