Sharing data between DLL and program

Started by
4 comments, last by BraXi 11 years, 6 months ago
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;
}

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 );
}
// this should be 2
printf( "DLLCode.testVal = %i\n", DLLCode.testVal );
return 0;
}
Advertisement
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:

//Shared Variable
int test;

//------------------------------------------------------

//IN DLL

__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;
}

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

do_stuff(); //outputs 0 and 5

test = 9999;
set_test(777);
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.
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://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

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.

@wqking

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.
You misunderstood me...

I meant to change this line
typedef libExport_t (*GetAPI_t) (exeExport_t);
to this
typedef libExport_t

* (*GetAPI_t) (exeExport_t);

So the function return libExport_t * rather than libExport_t.

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

And change the function body to return the pointer.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

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.

Thank you wqking ;) I owe you one smile.png

Complete code and projects for VC++2010 are here: http://www.braxi.org/stuff/dll_library_cpp.rar

This topic is closed to new replies.

Advertisement