So I was experimenting with DLL files and i though about sharing dynamic array of objects between DLL and EXE.
The only thing worth studying was quake 3, which loaded DLL to call game rules and manage objects, while EXE was only providing some data-access functions (for example rendering). After studying the code I've written test application in C that would load DLL file and share specified variables with program.
I was a bit confused when once set variables were unable to update, for example i've initialized myVar with value of 1 and later tried to change it 2. For some reason, value wasn't updated in EXE. I'd like to know how can i link dll dynamicaly so i can update variables in EXE with DLL.
I will attach some code here to show what i mean.
//This code is shared by both DLL and EXE in a "shared.h"
#include <iostream>
#include <windows.h>
using namespace std;
typedef struct
{
void (*TestDLLFunc)();
int testVal;
int *testArray;
} libExport_t;
typedef struct
{
int simpleVal;
} exeExport_t;
typedef libExport_t (*GetAPI_t) (exeExport_t);
libExport_t DLLCode;
exeExport_t EXECode;
// DLL code
#include "shared.h"
void TestFunc()
{
printf( "This message was sent from DLL\n" );
printf( "EXECode.testVal = %i\n", EXECode.testVal ); //this printed value 2012
// this is supposed to update array for EXE
DLLCode.testArray = new int[4]; //c++
for( int i = 0; i < 4; i++ )
DLLCode.testArray = i;
Sleep( 100 );
DLLCode.testVal = 2;
}
DLLCode.TestDLLFunc();
Sleep( 200 ); // just to make sure everything chaged
if( DLLCode.testArray != NULL )
{
for( int i = 0; i < 4; i++ )
printf( "array element is: %i\n", DLLCode.testArray );
}
// this should be 2
printf( "DLLCode.testVal = %i\n", DLLCode.testVal );
return 0;
}
Instead of trying to directly read the value of DLLCode.testVal, try writing a function that returns the value that can be called from the EXE.
I wrote a quick little test program with a variable declared in a shared header file (shared between a DLL and the EXE) and it resulted in something like this:
I don't think the problem is caused by crossing DLL and EXE value passing.
Your function GetAPI returns a VALUE of libExport_t. So you duplicate libExport_t and return the new copy to EXE.
That's why the value in EXE doesn't reflect the one in DLL, because they are totally different.
Try to return a pointer to libExport_t from GetAPI.
https://github.com/cpgf/cpgf cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
I tried to make pointer but it seems i cannot cast value of getprocaddress to a pointer, it would require diferent cast and to be honest it can't compile on VC++2010.
compiling failed with error message that it cannot be converted (i've changed my vars to pointers as well), error is highligted:
GetAPI_t *GetAPI;
[...]
GetAPI = (GetAPI_t*)GetProcAddress( library, "GetAPI" )
At the moment I'm at school but i will upload my newer code as soon as i get back.
https://github.com/cpgf/cpgf cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.