1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how i can to load a function from a dll loaded dinamically

Discussion in 'C++' started by michal, Oct 13, 2014.

  1. michal

    michal New Member

    Joined:
    Sep 12, 2014
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Hello there

    how i can to load a function from a dll loaded dinamically. In Dev-C++

    This is the dll.cpp code:

    __declspec(dllexport) float Suma(float a, float b){ return(a+b);}

    The dll.h was not modified

    The code of .exe is this:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    HINSTANCE dllhandle; //handle de la dll que se quiere cargar
    FARPROC direccion;  //direccion de la funcion
    bool descargada;    //var de control de descarga de la dll
    
    int main(int argc, char *argv[])
    {
        //proceso de carga dinamica de la dll
        dllhandle = LoadLibrary("dindll.dll");
        if(dllhandle)
        cout<<"se ha cargado la dll"<<endl;
        else{
             cout<<"No se ha cargado la dll"<<endl; 
            }
    // the above code works
    //--------------------------------------------------------------------------
    // but this not
    direccion = GetProcAddress(dllhandle,"Suma");
    if(direccion != NULL) cout << (direccion)(3,4)<<endl;
    //why not
    //---------------------------------------------------------------------------
    descargada = FreeLibrary(dllhandle);
        if(descargada)
        cout<<"se ha descargado la dll"<<endl;
        else{
             cout<<"No se ha descargado la dll"<<endl; 
            }
    // the above code works too
    
    thanks
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    In what way exactly doesn't it work?
     
  3. alia123

    alia123 New Member

    Joined:
    Jan 8, 2016
    Messages:
    65
    Likes Received:
    3
    Trophy Points:
    0
    Hey, here's the example just check this :
    Code:
    #include <windows.h>
    #include <iostream>
    
    /* Define a function pointer for our imported
     * function.
     * This reads as "introduce the new type f_funci as the type: 
     *                pointer to a function returning an int and 
     *                taking no arguments.
     *
     * Make sure to use matching calling convention (__cdecl, __stdcall, ...)
     * with the exported function. __stdcall is the convention used by the WinAPI
     */
    typedef int (__stdcall *f_funci)();
    
    int main()
    {
      HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\Documents and Settings\\User\\Desktop  \\fgfdg\\dgdg\\test.dll");
    
      if (!hGetProcIDDLL) {
        std::cout << "could not load the dynamic library" << std::endl;
        return EXIT_FAILURE;
      }
    
      # resolve function address here
      f_funci funci = (f_funci)GetProcAddress(hGetProcIDDLL, "funci");
      if (!funci) {
        std::cout << "could not locate the function" << std::endl;
        return EXIT_FAILURE;
      }
    
      std::cout << "funci() returned " << funci() << std::endl;
    
      return EXIT_SUCCESS;
    }
     

Share This Page