Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Sharing data between DLL and program


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 BraXi   Members   -  Reputation: 187

Like
0Likes
Like

Posted 26 September 2012 - 12:00 PM

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

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


Sponsor:

#2 caldiar   Members   -  Reputation: 702

Like
1Likes
Like

Posted 26 September 2012 - 07:17 PM

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.

#3 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 26 September 2012 - 11:52 PM

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, 26 September 2012 - 11:52 PM.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#4 BraXi   Members   -  Reputation: 187

Like
0Likes
Like

Posted 27 September 2012 - 01:34 AM

@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.

Edited by BraXi, 27 September 2012 - 01:34 AM.


#5 wqking   Members   -  Reputation: 756

Like
2Likes
Like

Posted 27 September 2012 - 01:44 AM

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.

Edited by wqking, 27 September 2012 - 01:45 AM.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#6 BraXi   Members   -  Reputation: 187

Like
1Likes
Like

Posted 27 September 2012 - 06:53 AM

Thank you wqking ;) I owe you one Posted Image

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

Edited by BraXi, 27 September 2012 - 06:53 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS