Solved

DLL global data and explicit linking.

Posted on 2002-04-18
10
604 Views
Last Modified: 2013-12-03
Hello All,

How do I use the global DLL data in my code which is explicitly linked.

I get a linker error when I try to use the DLL data into my code.

Following is the code which gives linker error for the variable iVar:
==========================================================
#include <iostream>

#include <windows.h>

#include "FileWithVar.h"

using namespace std;

typedef void (*FUNC_PTR)(void);

void main()
{
     HMODULE hModule = LoadLibrary("DynamicLib.dll");
     if(SUCCEEDED(hModule))
     {
          FUNC_PTR funcPtr = (FUNC_PTR)GetProcAddress(hModule, "PrintVar");
          if(NULL == funcPtr)
          {
               cout << "Error getting function PrintVar" << endl;
          }
          else
          {
               funcPtr();

                        // iVar is a DLL global data.
               cout << "In EntryPoint: Var: " << iVar << endl;
          }
     }
}
==========================================================

The linker error is
==========================================================
EntryPoint.obj : error LNK2001: unresolved external symbol __imp__iVar
Debug/ExternConst.exe : fatal error LNK1120: 1 unresolved externals
==========================================================

The FileWithVar.h file is:
==========================================================
extern "C"
{

__declspec( dllimport ) extern const int iVar;
__declspec( dllimport ) void PrintVar();

};
==========================================================

The DLL code (file DynamicLib.dll) is
==========================================================
#include <iostream>

using namespace std;

extern "C"
{

__declspec( dllexport ) extern const int iVar = 10;

__declspec( dllexport ) void PrintVar()
{
     cout << "In PrintVar: Var: " << iVar << endl;
}

};
==========================================================

How to remove the linker error?

Thanks in advance for the help.

Regards,

~Ajit.
0
Comment
Question by:ajit_kamat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 5

Accepted Solution

by:
robpitt earned 50 total points
ID: 6950394
If your loading you dll manually (via LoadLibrary) then you shouldn't declare the imports as being __declspec( dllimport ) instead you should just retrieve pointers to them using GetProcAddress.

Further if your loading the DLL manually then the variable iVar will not be automatically visible. The best you can do is get a point to the location... try this:

e.g.

 int* pointerToIVar;

 pointerToIVar = (int*)GetProcAddress( ... , "iVar");

0
 

Expert Comment

by:bansaldeep
ID: 6953307
GetProcAddress will not work for data, it returns pointers to functions in dll only.
You just need to remove extern keyword from both declarations in dll and application.

0
 
LVL 5

Expert Comment

by:robpitt
ID: 6953365
bansaldeep, Are you sure about that?


Quote from MSDN : "The GetProcAddress function retrieves the address of an exported function OR VARIABLE from the specified dynamic-link library (DLL)."

Granted I've never used it for a variable, but I always assumed it must work... how else might you get the addresss? There is no GetDataAddress after all?


0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:ajit_kamat
ID: 6953429
Hello bansaldeep,

Your solution doesn't work. The previous solution by robpitt worked for me. Though I am not sure how complex data can be exported from the DLL. The data I tried to export was just an int which is an automic type.

Regards,

~Ajit.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6965843
GetProcAddress delivers a pointer to the exported name. So calling it for an exported variable should deliver a pointer to the variable. I tested that in Pascal and it works. I was able to read global variables from MSVCRT.DLL.
0
 
LVL 5

Expert Comment

by:robpitt
ID: 6966357
Indeedy. Can I be so bold as to ask for the points?
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6966937
No of course not! ;-)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question