Link to home
Start Free TrialLog in
Avatar of bigga
bigga

asked on

Cannot link a DLL in Visual Studio 2005

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!
SOLUTION
Avatar of r3bel
r3bel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jitendra_wadhwani
jitendra_wadhwani

>>I tried to explicitly specify the DLL file path but it still didnt work

Please try with full path like

c:\\ABC\\xyz.dll
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;
}
Avatar of bigga

ASKER

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.
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.
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?
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?
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.
Avatar of bigga

ASKER

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.
Avatar of bigga

ASKER

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?
Avatar of bigga

ASKER

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++.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bigga

ASKER

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.

That would do it as well.  I guess I assumed that since the UNICODE build was being selected that you had selected that...