Jump to content
  • Advertisement
Sign in to follow this  
elih1

export global variable from dll

This topic is 3558 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

Hello I have a dll,say A.dll witch looks like this: //A.h __declspec(dllexport) int DATA; __declspec(dllexport) void f(); //A.cpp #include "A.h" void f() { DATA=5; } Also I have main application which is using the A.dll like this: #include<A.h> #include<iostream> int main() { f(); std::cout<<DATA //prints 0 instead 5 } I was excepting 5 to be printed in main(). but instead 0 is printed. What is the right way to export the dll global variable and use from my .exe application?

Share this post


Link to post
Share on other sites
Advertisement
You must first LOAD in the dll using the LoadLibrary API function (check MSDN for a full explanation on LoadLibrary) then you must use the GetProcAddress API function to access variables and functions from dlls that are currently loaded.

If you want a more detailed explanation then bump this thread with a request. I'm sure myself or some other can run you through it. But those 2 functions I've mentioned should be enough to get you on the right path.

Share this post


Link to post
Share on other sites
Entities shared in a DLL should be declared __declspec(dllexport) in the DLL, but __declspec(dllimport) in anything using that DLL.

Share this post


Link to post
Share on other sites
Quote:
Original post by CodeStorm
You must first LOAD in the dll using the LoadLibrary API function (check MSDN for a full explanation on LoadLibrary) then you must use the GetProcAddress API function to access variables and functions from dlls that are currently loaded.

Only for explicitly loaded DLLs. You don't have to do that for implicitly or delay-loaded ones. The OP is probably using the most straightforward form, ie. an implicitly loaded DLL with import library.

Share this post


Link to post
Share on other sites
Thank you.This works
I do the following in A.h
#pragma once
#ifdef USING
#define DLLSPEC __declspec(dllimport)
#else
#define DLLSPEC __declspec(dllexport)
#endif
DLLSPEC int DATA;
DLLSPEC void f();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!