Ok, that i do understand. So i would use a pointer to store the data from the object, and then to read it? Nevermind, but i have one last question. how can i provide a function as an argument to a call? Here is the code i had to make a dll that runs a thread (just as an example):
Code:
//borrowed most of this from a tutorial site
/* testLibLoad.cpp
* Auth: earl at earlh dot com
* This works under Win2K; dunno about anything else
*/
#include <windows.h>
#include <stdio.h>
void *threadtest (void)
{
printf ("Hello, world\n");
system ("pause");
return NULL;
}
int main (int argc, char** argv)
{
HINSTANCE hDLL;
hDLL = LoadLibrary("TestDLL.dll"); //load the library
if (hDLL == NULL)
{
fprintf(stderr, "Unable to load library.\n");
exit(-1);
}
//search for our function:int libPrintHelloWorld(void)
int (*funcPtr)(void (*)); //the function pointer
funcPtr = (int (*)(void (*))) GetProcAddress(hDLL, "runthread");
if (funcPtr == NULL)
{
fprintf(stderr, "Unable to find Message\n");
exit(-2);
}
//everything is working, so call the function
int retVal = funcPtr (threadtest ());
//cleanup by freeing the library
BOOL result = FreeLibrary(hDLL);
if (result == FALSE)
{
fprintf(stderr, "Unable to free library\n");
exit(-3);
}
printf("Everything worked!\n");
system ("pause");
return 0;
}
And here is the code for my dll:
Code:
#include <windows.h>
#include <process.h>
int sum (int a, int b)
{
return a + b;
}
int Message (char *mbText, char *mbTitle)
{
MessageBox (NULL, mbText, mbTitle, MB_OK);
return 0;
}
int runthread (void (*to_run)(void *), void *arg)
{
_beginthread (to_run, 0, arg);
return 0;
}
the thread starts correctly, but then it does something funny. i am using Visual c++ 2008 edition, and i get this runtime error:
debug assertion failed!
Program: DllRuntimeLoaderTest
File: thread.c
Line: 113
Expression: initialcode != NULL
Why is it doing that? And what is the correct way to pass a call the address of a function?