?
Solved

Cannot link a DLL in Visual Studio 2005

Posted on 2006-04-18
14
Medium Priority
?
4,973 Views
Last Modified: 2013-12-14
Hi,

I have written a program in VC++ 6.0 which needs to link to a DLL file, iphlpapi.dll.
In VC++ 6.0, I simply use LoadLibrary("iphlpapi.dll") and it went just fine.
Now, after I ported my projects to Visual Studio 2005, I cannot use LoadLibrary anymore.

Please take a look at the code:

HMODULE hModule;

hModule = LoadLibrary("iphlpapi.dll");
if (hModule == NULL)
{
      printf("Error linking\n");
      return FALSE;
}

Everytime when I run my program, I always got "Error linking" message.
I tried to explicitly specify the DLL file path but it still didnt work.

Indeed, I have included the .h and .lib files which are related to the DLL already.
(They are done before I ported and I set the Project properties to include the .h and .lib path already.)

Another question is there are many version of the DLL and H files, how could I know that the versions I am using are the same.
I mean, how could I know that the DLL file is compatible with the H file I am referring to.
I believe there must be some way to syncronize the file versions.

Thanks a lot!
0
Comment
Question by:bigga
  • 5
  • 5
  • 2
  • +2
14 Comments
 

Assisted Solution

by:r3bel
r3bel earned 200 total points
ID: 16476692
Hello,

The exact reason of linking error is hard to determine without additional information. Be sure to use the GetLastError function or such piece of code:

TCHAR szBuf[80];
    LPVOID lpMsgBuf;
    DWORD dw = GetLastError();

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

Then in lpMsgBuf you get a more detailed explanation of error. Also, please check the default project directory, Visual Studio 2005 changes directory hierarchy of the project. Use this code:

char lpBuffer[MAX_PATH];
GetCurrentDirectory(MAX_PATH, lpBuffer);

Then check the directory in lpBuffer, whether it has your iphlpapi.dll.

0
 
LVL 3

Expert Comment

by:jitendra_wadhwani
ID: 16476893
>>I tried to explicitly specify the DLL file path but it still didnt work

Please try with full path like

c:\\ABC\\xyz.dll
0
 
LVL 32

Expert Comment

by:jhance
ID: 16477049
As noted already, knowing the ERROR CODE from GetLastError() would probably shed a lot of light into what the real problem is...

Just do this:

HMODULE hModule;

hModule = LoadLibrary("iphlpapi.dll");
if (hModule == NULL)
{
     printf("Error linking.  Error code was: %d\n", GetLastError());
     return FALSE;
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:bigga
ID: 16477327
Thanks a lot guys, the error code was 126.

I looked on google and found that the code means the specified module could not be found.
This is strange, the file is there.

And indeed I tried \\fullpath\\filename as well.
0
 
LVL 32

Expert Comment

by:jhance
ID: 16477395
It's also possible to get this error if a DEPENDENT dll (i.e. one needed by IPHLPAPI.DLL) is not found.  Are you using IPHLPAPI.DLL from its default location or are you using a "private" copy?  If default, it should find any dependent DLLs from the PATH or the folder where IPHLPAPI lives.  But you might check into it.
0
 
LVL 32

Expert Comment

by:jhance
ID: 16477429
BTW, what platform are you running on?  I'm assuming XP...

I tried the following code on XP which was compiled with VS2005:

#include "stdafx.h"
#include "windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
      HMODULE hMod = NULL;

      hMod = LoadLibrary("IPHLPAPI.DLL");
      if(hMod == NULL)
      {
            printf("Error in LoadLibrary(): %d\n", GetLastError());
            return 0;
      }

      FreeLibrary(hMod);

      return 0;
}

worked fine...  Perhaps a problem with your system?
0
 
LVL 3

Expert Comment

by:Andre_Tertling
ID: 16477784
When you link the library statically (i.e. adding .h and .lib to your project), there is no need to load the DLL manually with LoadLibrary. Simply use the functions from the header file and the lib stub will load the library when needed. So either use LoadLibrary and define/typecast its functions or use .h and .lib, but do now use both methods at the same time. Then again, where and when do you get the "linking error"? Is there a error number available when you receive this error?
0
 
LVL 3

Expert Comment

by:Andre_Tertling
ID: 16477806
Addendum: I just noticed I misread your initial post. Try to use Dependeny Walker (from www.dependencywalker.com) to see which modules your DLL requires. Dependency Walker will indicate which dependencies are not fulfilled and has options to change module search order etc.
Please post your findings here.
0
 
LVL 1

Author Comment

by:bigga
ID: 16485956
I do not think there is any problem with the dependency.
The DLL I am linking came with Windows and you can find it in Windows\System32.

However, there is a problem with my machine, I cannot even run the code by jhance above.
I compiled but cannot link the DLL at the run time.
0
 
LVL 1

Author Comment

by:bigga
ID: 16485994
Ok, now I can compile!
But I have to put _T in front of the file name.

Like this: hMod = LoadLibrary("IPHLPAPI.DLL");

Normally I used LPCWSTR - hMod = LoadLibrary(LPCWSTR("IPHLPAPI.DLL"));

I dont know why but my LoadLibrary cannot just accept "IPHLPAPI.DLL" directly.

Can anyone tell me whats going here?
0
 
LVL 1

Author Comment

by:bigga
ID: 16486006
Oh, by the way, I cannot use _T in my main program.
It says: error C3861: '_T': identifier not found

Do I need sth like MFC to use it?
Cuz I simply wrote my program quite primitively without MFC or anything else.
I wrote it almost purely C++.
0
 
LVL 32

Accepted Solution

by:
jhance earned 1800 total points
ID: 16487008
That would be because you are building UNICODE instead of ASCII.  Use the following:

hMod = LoadLibrary(L"IPHLPAPI.DLL");

The "L" macro tells the compiler to use a UNICODE string contant instead of an ASCII string constant.  This will cause the linker to use LoadLibraryW() instead of the default LoadLibraryA().
0
 
LVL 1

Author Comment

by:bigga
ID: 16487055
jhance, you got it right, I think.
I changed the whole project property to ASCII and the whole things works!!

Thanks a lot!
I think this ASCII/Unicode thing is the issue and you guys helped solved it.

0
 
LVL 32

Expert Comment

by:jhance
ID: 16487213
That would do it as well.  I guess I assumed that since the UNICODE build was being selected that you had selected that...
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

839 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