Solved

DLL global data and explicit linking.

Posted on 2002-04-18
10
591 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ajit_kamat
Comment Utility
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
Comment Utility
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
Comment Utility
Indeedy. Can I be so bold as to ask for the points?
0
 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
No of course not! ;-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now