Thread: DLLs - Explicit Linking

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    7

    DLLs - Explicit Linking

    Good morning,
    I wouldl like to ask your help; I am trying to link a dll library explicltly.
    I use windows, in visual studio have created a new empty project, added a source file, changed its extension to .c, and written the following code:

    Code:
    
    #include <stdio.h>
    
    #include <windows.h>
    
    
    int main()
    {
        
           HANDLE hDLL = LoadLibrary("Library.dll");
    
            if (hDLL == NULL)
                
            {
                    printf("Failed to load the library.\n");
            }
                        
            else
            
            {            
                    printf("Library loaded.\n");
            }
    }
    I can build the project and execute it but always I get the output "failed to load the library". I tried to copy the dll library in every possible directory inside the visual studio project but the result is always the same.

    I would like to ask you how can I get some hints (i.e.: a more useful error message) about why it is not possible to load the library.

    thank you,
    Alberto

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    LoadLibrary function (Windows)

    You get more information by calling GetLastError.

    Watch out for the whole Unicode vs ANSI thing. Recent versions of visual studio default to Unicode.

    Any string you pass into a Win32 API should use the TEXT() macro. This insulates you from having to worry about whether you're compiling for Unicode or ANSI.
    Eg.
    Code:
    HANDLE hDLL = LoadLibrary(TEXT("Library.dll"));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2018
    Posts
    7
    Quote Originally Posted by Salem View Post
    LoadLibrary function (Windows)

    You get more information by calling GetLastError.

    Watch out for the whole Unicode vs ANSI thing. Recent versions of visual studio default to Unicode.

    Any string you pass into a Win32 API should use the TEXT() macro. This insulates you from having to worry about whether you're compiling for Unicode or ANSI.
    Eg.
    Code:
    HANDLE hDLL = LoadLibrary(TEXT("Library.dll"));
    Salem,
    Thank you for your help, this solved the problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explicit DLL load
    By Kamil_ in forum C++ Programming
    Replies: 15
    Last Post: 08-03-2011, 01:23 PM
  2. Linking DLLs to EXEs
    By code2d in forum Windows Programming
    Replies: 2
    Last Post: 01-02-2007, 11:35 AM
  3. explicit
    By Trauts in forum C++ Programming
    Replies: 4
    Last Post: 05-16-2003, 07:58 PM
  4. Overloaded functions and Dll(with Explicit Linking)
    By Nishant Ghai in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2003, 12:34 AM
  5. Explicit dynamic linking
    By mkyong in forum Windows Programming
    Replies: 9
    Last Post: 01-14-2003, 05:46 AM

Tags for this Thread