• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BraXi

Sharing data between DLL and program

5 posts in this topic

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.

[CODE]
//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;
}
[/CODE]
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;

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

//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
[/code]

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

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
0

Share this post


Link to post
Share on other sites
@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 [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
0

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 )
to
libExport_t * GetAPI( exeExport_t import )

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

Share this post


Link to post
Share on other sites
Thank you wqking ;) I owe you one [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

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

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  
Followers 0