Sign in to follow this  

passing values from exe to dll c++

This topic is 2801 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
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
Your DLL project should produce a LIB file when it compiles. Go to your DLL project properties, under Linker->Advanced->Import library there should be something along the lines of $(TargetName).lib (add it if not). Go to your joystick project properties under Linker->Input and make sure that library is included under 'additional dependencies'. You might also need specify the directory the lib file is in under Linker->General->Additional Library Dependencies.

Share this post


Link to post
Share on other sites
actually, I did those things before. let me explain what i did, in case i did something wrong.

for the plugin, the import library is "../../../../../bin\$(PlatformName)/plugins/ordeviceinput.lib". that's in the Autodesk folder (Autodesk makes MotionBuilder). i didn't write that part tho, it was already in the project

so what I did, I copied the lib file in my visual studio/VC/lib/ folder.

then, in my joystick project, I put ordeviceinput.lib in my linker->input->additional dependencies.

----

so now, after reading your last post, I just tried copying ordeviceinput.lib in my joystick project, and adding the joystick project folder in linker->general->additional library directores, but i still get the same errors (3 of them, corresponding to the 3 functions i added)


EDIT: hmmm i just checked the header file in my plugin project, and the line #define PLUGINFN __declspec(dllexport) is grayed out. maybe that's the problem? like, it doesn't export it?

[Edited by - souskesiahh on April 8, 2010 11:13:09 AM]

Share this post


Link to post
Share on other sites
Did you rebuild the plugin project (with MYPLUGIN_EXPORTS defined) once you added the header file and relevant function code? THAT is the lib file you need, not some random one you found in a directory. You use those kind of libs to access functions written by other people in DLLs you don't have code for.

And yes, in your joystick project #define PLUGINFN __declspec(dllexport) should be grayed out and PLUGINFN __declspec(dllimport) should be defined because you're not exporting those functions so another program can use them, you're importing them so you can use them.

Here's a very simple project:

MyPlugin.h (included in both your joystick project and DLL)
#pragma once

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

PLUGINFN int Plugin_GetValue();
PLUGINFN void Plugin_SetValue(int v);

MyPlugin.cpp (main piece of your DLL):
// MyPlugin.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

#define MYPLUGIN_EXPORTS
#include "../IPC_TEST/MyPlugin.h" // include the header we wrote before, but now we'll use it for exports

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

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
){
return TRUE;
}

// Define plugin functions
PLUGINFN int Plugin_GetValue() { return g_PLUGIN_VALUE; }
PLUGINFN void Plugin_SetValue(int v) { g_PLUGIN_VALUE = v; }


Now you compile that and you get a LIB file. Using your header in conjunction with this LIB file will allow you to access those plugin functions.

Test Project:
#include "stdafx.h"
#include "MyPlugin.h" // <-- header file that describes what our Plugin exports
#include <iostream>

int _tmain(int argc, _TCHAR* argv[]) {
if (Plugin_GetValue() == 10) {
Plugin_SetValue(20);
} else Plugin_SetValue(10);

using std::wcout;
using std::wcin;

wchar_t input = '\0';

while (true) {
wcout << "Current value of MyPlugin: " << Plugin_GetValue() << std::endl;
wcout << "Press a key to check again, or q/Q to quit\n";
wcin >> input;

if ((input == 'q') || (input == 'Q'))
break;
}

return 0;
}

Make sure your test project is built with the LIB file produced by the above dll project linked.

Now we'll share that plugin value across two separate processes. Run an instance of the EXE you just created. It'll show you the current value, 10. Press any char (other than q of course) and press enter to check the value again. No matter how many times you check it, it's 10.

Now open another instance of the program. It shows the value as 20, proving that even though it's got a whole new copy of the DLL in memory, it's got a hold of the data in the first program. Go back to the first program and have it check the value again to convince yourself that information really is crossing process boundaries.

Share this post


Link to post
Share on other sites
we're getting there; everything compiles.
but somehow, the joystick values don't get to the plugin on motionbuilder's side...

could it have something to do with a compile warnings i have in the plugin project? warning C4273: 'MyPlugin_SetRotX' : inconsistent dll linkage

Share this post


Link to post
Share on other sites
no MYPLUGIN_EXPORTS is in the plugin only...

oh well, we tried. I'm gonna try the Qt library now, see if it's gonna be kind to me.

thx for your time man

[Edited by - souskesiahh on April 12, 2010 3:58:09 PM]

Share this post


Link to post
Share on other sites
ok Qt's not really for me, since I have to work with already-made VS projects. So I came back to take a look at my first attempt here.

anyone's got any idea of what we're missing here?

Share this post


Link to post
Share on other sites
my friend spoke to a programmer, and he said something like the MotionBuilder plugins are multi-threaded or whatnot, and that's causing our problems.

Now I just discovered the SDL lybrary, and i'll try that. Instead of passing values from the joystick program in shared memory, i'll call SDL joystick functions from within the plugin.

I'll tell you if I have problems with that. thx

Share this post


Link to post
Share on other sites
Multithread shouldn't be an issue. It's when two threads try to write to memory at the same time that bad stuff starts to happen, and that's pretty easily solved with a mutex or similar.

Hopefully SDL does the trick for you

Share this post


Link to post
Share on other sites
yea I achieved what I was trying to do with SDL. and it was much simpler than everything I tried before. probably cause it's only in C...

this time, instead of passing values from the joystick exe, I just put the joystick SDL code in the plugin. to read values from the joystick directly from the plugin

Share this post


Link to post
Share on other sites

This topic is 2801 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.

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