"DLL Failed To Load!"

This is a discussion on "DLL Failed To Load!" within the C++ Programming forums, part of the General Programming Boards category; Hi. I am trying to create a .dll for a C++ project using Visual Studio 2005. I successfully created it. ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    "DLL Failed To Load!"

    Hi.
    I am trying to create a .dll for a C++ project using Visual Studio 2005.
    I successfully created it. Now when I am calling this .dll from another console application, I am not able to get HINSTANCE handle.
    I am attaching code below....

    Project DLLTutorial creates a dll. And I am using it in Test1 project(console project).
    DLLTutorial has two files DLLTutorial.h and .cpp. And Test1 has one file Test1.cpp which loads DLL.

    Project DLLTutorial builds successfully and creates .dll and .lib which I have put in Test1 project.

    WHen I run Test1 project, it prints "DLL Failed To Load!" .

    Below are code for three files.

    DLLTutorial.h

    Code:
    #ifndef _DLL_TUTORIAL_H_
    #define _DLL_TUTORIAL_H_
    #include <iostream>
    
    extern "C"
    {
       __declspec(dllexport) int Add( int a, int b );
       __declspec(dllexport) void Function( void );
    }
    
    #endif
    DLLTutorial.cpp

    Code:
    #include <iostream>
    #include "DllTutorial.h"
    
    #define DLL_EXPORT
    
    extern "C"
    {
       __declspec(dllexport) int Add( int a, int b )
       {
          return( a + b );
       }
    
       __declspec(dllexport) void Function( void )
       {
          std::cout << "DLL Called!" << std::endl;
       }
    }
    Test1.cpp

    Code:
    #include <iostream>
    #include <windows.h>
    
    typedef int (*AddFunc)(int,int);
    typedef void (*FunctionFunc)();
    
    int main()
    {
       AddFunc _AddFunc;
       FunctionFunc _FunctionFunc;
       LPCTSTR path= (LPCTSTR)"DLLTutorial.dll";
       HINSTANCE hInstLibrary = LoadLibrary(path);
    
       if (hInstLibrary)
       {
          _AddFunc = (AddFunc)GetProcAddress(hInstLibrary, "Add");
          _FunctionFunc = (FunctionFunc)GetProcAddress(hInstLibrary,
             "Function");
    
          if (_AddFunc)
          {
             std::cout << "23 = 43 = " << _AddFunc(23, 43) << std::endl;
          }
          if (_FunctionFunc)
          {
             _FunctionFunc();
          }
    
          FreeLibrary(hInstLibrary);
       }
       else
       {
          std::cout << "DLL Failed To Load!" << std::endl;
       }
    
       std::cin.get();
    
       return 0;
    }
    Thanks !!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is the .dll in the same directory as the .exe from the test-harness?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Yes...both .dll and .lib are in same directory as .exe.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try printing the return value from GetLastError() in the error case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    They sure make it more difficult than needs be. Does the tutorial specifically mention dynamic dll loading?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Hi.
    I searched for GetLastError() in MSDN. But could not find way to use it.
    Can you tell.
    Thanks

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    What did you not understand? GetLastError() returns an error.
    DWORD dwErr = GetLastError();
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Yes..

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    so print out the error and it might give you an idea of whats going on. There are about 74 pages of error codes listed on msdn, so without knowing the specific error code returned, you have no way of knowing whats wrong.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    Change this
    Code:
       LPCTSTR path= (LPCTSTR)"DLLTutorial.dll";
       HINSTANCE hInstLibrary = LoadLibrary(path);
    ...
          std::cout << "DLL Failed To Load!" << std::endl;
    to this
    Code:
        const char *path= "DLLTutorial.dll";
        HINSTANCE hInstLibrary = LoadLibraryA(path);
    ...
          std::cout << "DLL Failed To Load! Error = " << GetLastError() << std::endl;
    You should never have to force a string literal into something (it may not be) with a cast.

    gg

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Hi.

    I tried it....now I am getting error....

    error C2664: 'LoadLibraryW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    the sample calls LoadLibraryA not LoadLibraryW
    if you want to have Unicode version - you could use
    Code:
    LPCTSTR path= TEXT("DLLTutorial.dll");
       HINSTANCE hInstLibrary = LoadLibrary(path);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    It worked this time....

    Thanks a lot...

    I didn't get the difference between LoadLibrary(path) and LoadLibraryA(path) functions.

    Kindly reply.

    Thanks again for your help.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Text in Windows is either 8-bit character (char), aka ANSI or 16-bit characters (wchar_t), aka UNICODE.

    Almost every Windows function has two variants, one that takes "ANSI", called XxxxxA, and one that takes "UNICODE", called XxxxxxW.

    If you want to write code that works for either, then you should use TEXT("xxxx"), which expands to either ANSI or UNICODE strings.

    The compiler is complaining in your code that you are passing a regular char string to a wide char expecting function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    In case you want to switch between ansi and unicode (unicode is recommended), you can do so in project settings, under Character Set (General).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. Find Injected DLLs In A Process?
    By pobri19 in forum Windows Programming
    Replies: 35
    Last Post: 02-06-2010, 08:53 AM
  3. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  4. C++ text file
    By statquos in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2008, 01:42 PM
  5. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21