C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to access external data in dll ?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
andreas.kratzsch@freenet.
Guest





PostPosted: Sat Aug 27, 2005 7:38 pm    Post subject: How to access external data in dll ? Reply with quote



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





PostPosted: Mon Aug 29, 2005 11:31 am    Post subject: Re: How to access external data in dll ? Reply with quote



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





PostPosted: Mon Aug 29, 2005 1:57 pm    Post subject: Re: How to access external data in dll ? Reply with quote



//////////////
// 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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.