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
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; }