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

## 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 on other sites
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 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 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 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 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 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)#endifPLUGINFN	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.

##### Share on other sites
in my joystick project, i get this error

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

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 10
• 11
• 11
• ### Forum Statistics

• Total Topics
631775
• Total Posts
3002281
×