Solved

DLL global data and explicit linking.

Posted on 2002-04-18
10
598 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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