DLLs and shared data sections
by Scott Burns · in Technical Issues · 03/15/2006 (11:29 am) · 1 replies
I've been experimenting with DLLs and shared data sections the last few days with inent of being able to interface Torque with an external application. In the research I've done it seems like I should be able to do it via a shared .dll that has shared data in it. This way the .dll will act as an intermediary between Torque and the other app. Its values being updated by Torque and retrieved by the other app.
From the few tutorials I've been able to find it seems like I'm on the right track, but my test app doesn't seem to be working correctly. So I thought I'd see if anyone has any experience with shared data sections and can tell me if I'm using them wrong.
The code:
the dll:
DLLTutorial.h
DLLTutorial.cpp
The test app:
main.cpp
The result of this test is I'll start one instance of it and add two numbers together thus changing x in the dll. Then I'll start a second instance of the testapp and instead of seeing x starting at its new value I see it at 0.
Any help would be appreciated.
From the few tutorials I've been able to find it seems like I'm on the right track, but my test app doesn't seem to be working correctly. So I thought I'd see if anyone has any experience with shared data sections and can tell me if I'm using them wrong.
The code:
the dll:
DLLTutorial.h
//DLLTutorial.h
#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllimport)
#else
#define DECLDIR __declspec(dllexport)
#endif
#pragma data_seg(".my_data")
int x = 0;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.my_data,RWS")
DECLDIR int Add(int a, int b);
DECLDIR void Function(void);
#endifDLLTutorial.cpp
//DLLTutorial.cpp
//#include <iostream>
#include "DLLTutorial.h"
#define DLL_EXPORT
extern "C"
{
DECLDIR int Add(int a, int b)
{
x = (a + b);
return x;
}
DECLDIR void Function(void)
{
std::cout<< "DLL Called!" << std::endl;
std::cout<< "X = " << x <<std::endl;
}
}The test app:
main.cpp
#include <iostream>
#pragma comment(lib, "DLLTut.lib")
#include "DLLTutorial.h"
int main()
{
int a, b;
Function();
std::cout<<"Enter a number"<<'\n';
std::cin>> a;
std::cout<<"Enter another number"<<'\n';
std::cin>> b;
std::cout<< Add(a, b) << '\n';
Function();
return(1);
}The result of this test is I'll start one instance of it and add two numbers together thus changing x in the dll. Then I'll start a second instance of the testapp and instead of seeing x starting at its new value I see it at 0.
Any help would be appreciated.
Associate Scott Burns
GG Alumni
So I decided to start over following a tutorial I found over on FlipCode.
That code seemed to get the job done. Its more or less the same except without the fancy if-else defining of the import and export calls. Which leads me to believe that may have been the source of my troubles.