| View previous topic :: View next topic |
| Author |
Message |
andreas.kratzsch@freenet. Guest
|
Posted: Sat Aug 27, 2005 7:38 pm Post subject: How to access external data in dll ? |
|
|
Hi,
i habe a simple C/C++ question:
How can i access external variables inside a dll ?
For example:
main module:
int x; // extern !
void func();
int main()
{
x = 0;
func();
return x;
}
dll module:
extern int x;
void func()
{
// do somthing with x
x = 3;
}
I tried to use __declare(dllimport/dllexport) also,
but the linker always complains about the unresolved symbol x.
If i use /FORCE to create the dll the linker doesn't
resolve the symbol when the dll is loaded.
(Visual C++ 6)
Thanks !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
ravinderthakur@gmail.com Guest
|
Posted: Mon Aug 29, 2005 11:31 am Post subject: Re: How to access external data in dll ? |
|
|
the procedure for using the same is as follows:
in the header file having decratation of the variable , declare the
variable with dllexport
specification.
on compiling a .lib file will be created.
add the .lib file and the header file to the project for the dll.
now compile and link.
this should work.
thanks
rt
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Kodt Guest
|
Posted: Mon Aug 29, 2005 1:57 pm Post subject: Re: How to access external data in dll ? |
|
|
//////////////
// your_dll.h
#pragma once
// the selector of appropriate declspec
#ifdef YOUR_DLL_IMPL // this macro is defined when compiling the DLL
#define YOUR_DLL_API __declspec(dllexport)
#else // all others will import
#define YOUR_DLL_API __declspec(dllimport)
#endif
extern // tells that this is a declaration, not definition
int YOUR_DLL_API x;
////////////////
// your_dll.cpp
#define YOUR_DLL_IMPL // or set up your project / makefile
#include "your_dll.h"
// to be sure the definition matches to the declaration
int x = 123; // the definition
BOOL WINAPI DllMain(bla bla bla) { ..... }
////////////////
// your_exe.cpp
#include "your_dll.h" // include the dll's declarations
#pragma comment(lib,"your_dll") // tell the linker to use your_dll
// or set up your project/solution settings,
// or add "/link your_dll.lib" to the command line
int main() { return x; }
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|