?
Solved

DLL global data and explicit linking.

Posted on 2002-04-18
10
Medium Priority
?
606 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 200 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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 …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

777 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