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!
LVL 1
biggaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

r3belCommented:
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
jitendra_wadhwaniCommented:
>>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
jhanceCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

biggaAuthor Commented:
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
jhanceCommented:
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
jhanceCommented:
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
Andre_TertlingCommented:
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
Andre_TertlingCommented:
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
biggaAuthor Commented:
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
biggaAuthor Commented:
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
biggaAuthor Commented:
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
jhanceCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
biggaAuthor Commented:
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
jhanceCommented:
That would do it as well.  I guess I assumed that since the UNICODE build was being selected that you had selected that...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.