Jump to content
  • Advertisement
Sign in to follow this  
souskesiahh

passing values from exe to dll c++

This topic is 3019 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

so I have this executable which takes values from a joystick and outputs those values (i have the code). I want to pass those values to a dll, which is read in a program (i have the code for the dll). how can I pass those values from the exe to the dll? (by the way, i'm a kinda beginner)

Share this post


Link to post
Share on other sites
Advertisement
Well for starters is this a DLL of which you created or is it third party? A couple of things to know here is there are a few ways of exporting functions, the __declspec(dllexport) way and the .def definition file way. To know which functions are being exported from a Dll you can use the command line and run Dumpbin. You will need to look into decorated names and "C" style naming which is not decorated. There is a fair bit to go over and most of it can be found on msdn . You will also want to look into LoadLibrary(Ex) and function pointers if you are not already familiar. I am not exactly sure how much of this is language specific to C++ and Windows(I'm pretty sure there are no other OS using the Dll system??) systems as I haven't spent much time elsewhere.

Share this post


Link to post
Share on other sites
Hm, it could be simple - you can add the DLL to your project (where your exe is ) , compile,include the needed header file and then just call the relevant functions with parameters..
But your question is very general - to get better answer be more specific about programming environment, OS and type of DLLs

Share this post


Link to post
Share on other sites
well, I'm working with visual studio .NET, C++, win7. What I'm trying to do here is plugin development for MotionBuilder. I got some code here, it's an application that reads values from the joystick, and displays them in a windows form.
Also, I have sample code(templates) that came with MotionBuilder plugin sdk. What I'd like, is to pass those values from the joystick, into the MotionBuilder plugin.

Share this post


Link to post
Share on other sites
The specific topic you're looking for is IPC (inter-process communication). If all you need is to pass is a couple of primitive values, a shared data segment is simple and easy. If your program loads a copy of the DLL at the same time as this other program which loads it as a plugin, you'd simply have something like this:

#pragma data_seg (".SHARED")
int g_joyx = 0;
int g_joyy = 0;
#pragma data_seg()
#pragma comment(linker, "/section:.SHARED,RWS")

in your DLL code. Now, whenever you change g_joyx or g_joyy in any process that has this DLL loaded, it will change for all processes. If there's potential for two processes to be writing to this value at the same time, you'll have to set up some write protection (using a mutex or similar, for example).

Export a couple functions to read/write these values from the DLL and you'd be set.

On the other hand, if you need to transfer more complex information (such as non-primitives or anything that involves a pointer), you'd want to use shared memory. Boost makes this really easy to do.

Share this post


Link to post
Share on other sites
thx for your help, but since i'm very beginner in C++, I need some clarification.

Quote:
in any process that has this DLL loaded

does that mean I have to include that MotionBuilder plugin dll source code file to my joystic project (joystick project is the cpp which reads from the joystick)?

also, what do you mean by "exporting functions"?

Share this post


Link to post
Share on other sites
Quote:
Original post by souskesiahh
thx for your help, but since i'm very beginner in C++, I need some clarification.

Quote:
in any process that has this DLL loaded



does that mean I have to include that MotionBuilder plugin dll source code file to my joystic project

No, it merely needs a copy of the DLL in memory solely for the purpose of changing the shared data segment in other instances of the DLL. Write your plugin DLL, then create a header file describing exported functions. It might look something like this:

#ifdef MYPLUGIN_EXPORTS
#define PLUGINFN __declspec(dllexport)
#else
#define PLUGINFN __desclspec(dllimport)
#endif

PLUGINFN int MyPlugin_GetJoyX();
PLUGINFN int MyPlugin_GetJoyY();
PLUGINFN void MyPlugin_SetJoyX(int x);
PLUGINFN void MyPlugin_SetJoyY(int y);


Build your plugin DLL with MYPLUGIN_EXPORTS defined. Go back to your joystick project and include the header file you created somewhere where you want to change shared values. Go to your linker settings and make sure the import library (probably named MyPlugin.lib) created by your plugin project is included as input.

Now, whenever you want to change the x and y values in your plugin from your joystick program, you just need to set them using MyPlugin_SetJoyX/Y.

This article might help.

Share this post


Link to post
Share on other sites
in my joystick project, i get this error
error C3861: 'MyPlugin_SetJoyX': identifier not found
error C3861: 'MyPlugin_SetJoyY': identifier not found


Here's what I did. I copy pasted your code in a header that I created in my MotionBuilder plugin project. Then, I defined MyPlugin_SetJoyX and MyPlugin_SetJoyY right after the #includes on top of my .cxx file of my plugin project (i also declared the 2 int variables that i mess with after the #includes)
save, compile, fine.

Then, I opened my joystick project, added the header file, and called those 2 functions from within joystick.cpp, and it gives me those 2 errors.

Share this post


Link to post
Share on other sites
According to the compiler, you didn't define those functions or it has no idea what you're talking about when you refer to them. You #include'd that header in the joystick cpp file before you tried to use them, right?

Share this post


Link to post
Share on other sites
omg I forgot to include the header in my cpp file o_0

but now i get a weird error..

error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl MyPlugin_SetRotX(double)" (__imp_?MyPlugin_SetRotX@@YAXN@Z) referenced in function "long __cdecl UpdateInputState(struct HWND__ *)" (?UpdateInputState@@YAJPAUHWND__@@@Z)


by the way, I changed the function names for rotation, and the variables to double

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!