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] = i;
Sleep( 100 );
DLLCode.testVal = 2;
}
libExport_t GetAPI( exeExport_t import ) // dll's entry point
{
EXECode = import; //initialize exe functions for dll
DLLCode.TestDLLFunc = TestFunc;
DLLCode.testVal = 1;
DLLCode.testArray = NULL;
return DLLCode;
}
// EXE code
#include "shared.h"
HINSTANCE library;
void LoadTestLibrary( const char *name )
{
GetAPI_t GetAPI;
printf( "Loading %s\n", name );
library = LoadLibraryA( name );
if( library == 0 )
printf( "LoadLibraryA(\"%s\") failed\n", name );
if( ( GetAPI = (GetAPI_t)GetProcAddress( library, "GetAPI" ) ) == 0 )
{
printf( "Couldn't retrieve vaild adress\n", name );
}
EXECode.simpleVal = 2012;
DLLCode = GetAPI( EXECode );
if( !DLLCode )
printf( "DLL not loaded\n" );
}
int main()
{
LoadTestLibrary( "test.dll" );
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[i] );
}
// this should be 2
printf( "DLLCode.testVal = %i\n", DLLCode.testVal );
return 0;
}






