passing values from exe to dll c++

Started by
19 comments, last by souskesiahh 14 years ago
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.

If I put my 2 cents in and get a penny for my thoughts, where does my other penny go?
Advertisement
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]
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)#endifPLUGINFN	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 functionsPLUGINFN 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.
If I put my 2 cents in and get a penny for my thoughts, where does my other penny go?
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
Make sure MYPLUGIN_EXPORTS is not defined in your joystick project, only in the DLL project.
If I put my 2 cents in and get a penny for my thoughts, where does my other penny go?
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]
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?
Could you post your project somewhere?
If I put my 2 cents in and get a penny for my thoughts, where does my other penny go?
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
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
If I put my 2 cents in and get a penny for my thoughts, where does my other penny go?

This topic is closed to new replies.

Advertisement