Sign in to follow this  

Sharing data between DLL and program

Recommended Posts

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] = 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" );

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;

Share this post

Link to post
Share on other sites
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:

[code]//Shared Variable
int test;



__declspec(dllexport) void do_things()
test = 0;
std::cout << test << std::endl; //results in 0 as expected.

test = 5;
std::cout << test << std::endl; //results in 5 as expected.

__declspec(dllexport) int get_test()
return test;

__declspec(dllexport) void set_test(int number)
test = number;

test = 9;
std::cout << test << std::endl; //results in 9

do_stuff(); //outputs 0 and 5

test = 9999;
std::cout << test << std::endl; //results in 9999
std::cout << get_test() << std::endl; //results in 777

So, if the variable lives on the EXE side, you want to make a function in the DLL to send the address of the variable to the DLL to work on.

I can't help but feel that I'm forgetting to mention something important. I'm sure somebody will follow this post up with some additional info.

Share this post

Link to post
Share on other sites
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. Edited by wqking

Share this post

Link to post
Share on other sites

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 [u][b]= (GetAPI_t*)[/b][/u]GetProcAddress( library, "GetAPI" )

At the moment I'm at school but i will upload my newer code as soon as i get back. Edited by BraXi

Share this post

Link to post
Share on other sites
You misunderstood me...

I meant to change this line
typedef libExport_t (*GetAPI_t) (exeExport_t);
to this
typedef libExport_t [size=5][b]*[/b][/size] (*GetAPI_t) (exeExport_t);

So the function return libExport_t * rather than libExport_t.

Also change the prototype,
libExport_t GetAPI( exeExport_t import )
libExport_t * GetAPI( exeExport_t import )

And change the function body to return the pointer. Edited by wqking

Share this post

Link to post
Share on other sites
Thank you wqking ;) I owe you one [img][/img]

Complete code and projects for VC++2010 are here: Edited by BraXi

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this