Okay, I've been playing around with those Class-Exporting DLLs one more time. This time I've implemented that Destroy-Function in the DLL.
Everyting works fine, but when delete the object cia DLL, I get an error Message:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
I've created a Test-Project for that, and here are my files:
The Test Project consist of 2 single Projects, a DLL exporting a dervied class, and an executable calling the DLL's functions.
So let's start with the DLL.
This is the DLL's Main Module (DestroyDLL.cpp):
#include "windows.h"
#include "Kathy.h"
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}
CPerson* APIENTRY CreatePerson()
{
return new CKathy();
}
void APIENTRY KillPerson(CPerson* pPerson)
{
delete pPerson;
}
Here's Kathy.h:
#include "Person.h"
class CKathy : public CPerson
{
public:
CKathy() {};
~CKathy() {};
char* SayIt();
};
And it's implementation, Kathy.cpp:
#include "Kathy.h"
char* CKathy::SayIt()
{
return "I Love You.";
}
CKathy is derived from an object called CPerson (CPerson.h):
class CPerson
{
public:
CPerson() {};
~CPerson() {};
virtual char* SayIt() = 0;
};
So much for the DLL.
The Executable consists of only 2 Files:
Person.h (see above) and DestroyEXE.cpp:
#include <stdio.h>
#include <windows.h>
#include "Person.h"
// Function Prototypes
typedef CPerson*(*PERSONPROC)();
typedef void(*KILLPROC)(CPerson*);
// Application Entry Point
int main(int argc, char* argv[])
{
// Try to load DLL
HMODULE hMod = LoadLibrary("DestroyDLL.dll");
if(hMod == NULL)
{
printf("ERROR: Could not load DestroyDLL.dll!\n");
return -1;
}
// Search for CreatePerson Function
PERSONPROC kp = (PERSONPROC)GetProcAddress(hMod, "CreatePerson");
if(kp == NULL)
{
printf("ERROR: Could not find CreatePerson!\n");
FreeLibrary(hMod);
return -1;
}
// Search for KillPerson Function
KILLPROC kill = (KILLPROC)GetProcAddress(hMod, "KillPerson");
if(kill == NULL)
{
printf("ERROR: Could not find KillPerson!\n");
FreeLibrary(hMod);
return -1;
}
// Create a Kathy for us ^_^
CPerson* pKathy = kp();
if(pKathy == NULL)
{
printf("ERROR: Kathy creation failed!\n");
FreeLibrary(hMod);
return -1;
}
// Now let her say something
printf(pKathy->SayIt());
// Okay, that's enough, kill'er
kill(pKathy);
// And now, get out
FreeLibrary(hMod);
// That's it
return 0;
}
If you want it as a complete MSVC Project, here it is:
http://www.alhexx.com/destroy.rarThe program execution goes well until the "kill(pKathy)" command,
that's where I receive that error message...
Any ideas???
- Alhexx