|
-
July 19th, 2009, 07:11 PM
#1
dll in dev-cpp
Hello,
I'm trying to make a dll in dev-cpp. The software loading the dll is a legacy program and has no knowledge of the function that it needs to call. Therefore the dll will need to call the function when loaded. I've been working in iterations to work my way up to that goal and have made a basic dll in dev-cpp with a tester program.
Anyways so I compile the dll and the tester program successfully and try to call dlltest.exe
The output says it loaded the dll successfully but no messagebox pops up.
What am I doing wrong?
Thank you,
~Thrawn89
Forgive me if this code doesn't get parsed into tags but I do not know the command to do so:
--------------------------------------------------------------
//dlltest.cpp
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include "dll.h"
//This will test out loading a dll.
void handler(int errCode);
int main(int argc, char** argv)
{
HMODULE lib = LoadLibrary( argv[1] );
if(!lib)
{
printf("LoadLibrary Failed Loading %s.\n", argv[1]);
handler(GetLastError());
}
printf("LoadLibrary loaded %s. Handle is %d\n", argv[1], lib);
while(1);
return 0;
}
/* Takes a system exit code, prints it, then exits */
void handler(int errCode)
{
CHAR buff[1024];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, errCode, 0, buff, 1024, 0);
printf("Error(%d): %s\n", errCode, buff);
exit(errCode);
}
-----------------------------------------------
//dllmain.cpp
/* Replace "dll.h" with the name of your header */
#include <windows.h>
#include <cstdio>
#include <cstdlib>
#include "dll.h"
DWORD WINAPI HelloWorld (LPVOID)
{
MessageBox(NULL, "Hello World!\n", "Hello", MB_OK);
return 1;
}
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:
CreateThread(NULL, 0, HelloWorld, NULL, 0, NULL);
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
---------------------------------------
//dll.h
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
extern "C"{
DWORD WINAPI HelloWorld (LPVOID);
}
#endif /* _DLL_H_ */
-
July 21st, 2009, 10:05 PM
#2
Re: dll in dev-cpp
The problem was that DLLMain was not being called. Putting extern "C" in front of the BOOL fixed it.
~Thrawn89
-
July 30th, 2009, 11:49 PM
#3
-
August 9th, 2009, 02:26 PM
#4
Re: dll in dev-cpp
BOOL APIENTRY Dllmain (HINSTANCE hInst /* Library instance handle. */ ,
The problem was that DLLMain was not being called. Putting extern "C" in front of the BOOL fixed it.
FYI, DllMain is already declared extern "C" in SDK headers. And the name must be exactly DllMain but not Dllmain or DLLMain
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|