Solved

DLL global data and explicit linking.

Posted on 2002-04-18
10
594 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

810 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