   LoadLibraryA example/tutorial?
  I'm trying to use loadLibraryA and GetProcAddress to dynamically link to a library at runtime in my c++ app, but I can't for the lif of me find ONE single tutorial or code sample that shows you how to form the function prototypes.In the language I use now, bmax, I do this,function TestFunc:Int(a:int,b:int) "win32"lib = LoadLibraryA( "mylib.dll" )if lib testfunc = getprocaddress( lib,name )endifSo, what's the C++ alternative? Something like,int Testfunc(int a,int b) "win32"HWMODULE lib = loadlibraryA( "mylib.dll")if( lib ){   testFunc = getProcAddress( lib,name )}or is there another way?   
typedef int (*testfunc)( int a, int b );HMODULE lib = Loadlibrary( "mylib.dll");if( lib ){   testfunc func = (testfunc) GetProcAddress( lib,name );   func( 2, 3 );}I hope this works. Has been some time since I last did function pointers.Edit: Horrible typos fixed![Edited by - Konfusius on April 20, 2006 6:55:57 AM];  Thanks alot, checking it now :);Quote:Original post by Konfusiustypedef int (*testfunc)( int a, int b );HWMODULE lib = Loadlibrary( "mylib.dll");if( lib ){   testfunc func = (testfunc) = GetProcAddress( lib,name );   func( 2, 3 );}I hope this works. Has been some time since I last did function pointers.   testfunc func = (testfunc)GetProcAddress( lib,name );If func is to be used outside of those parens, it needs to be declared outside of them as well. And the calling convention might be required too: typedef int (WINAPI *testfunc)( int a, int b );WINAPI is a define for __stdcall. Most functions in system dlls use this convention. Another thing to note is that GetProcAddress requires that name be ANSI, not UNICODE.  ;  D'you know which file I have to include? I tried the obvious, <windows.h> but it throws up errors stating that HWMODULE is an unknown identifier etc. User32?;Quote:Original post by LessBreadQuote:Original post by Konfusiustypedef int (*testfunc)( int a, int b );HWMODULE lib = Loadlibrary( "mylib.dll");if( lib ){   testfunc func = (testfunc) = GetProcAddress( lib,name );   func( 2, 3 );}I hope this works. Has been some time since I last did function pointers.   testfunc func = (testfunc)GetProcAddress( lib,name );If func is to be used outside of those parens, it needs to be declared outside of them as well. And the calling convention might be required too: typedef int (WINAPI *testfunc)( int a, int b );WINAPI is a define for __stdcall. Most functions in system dlls use this convention. Another thing to note is that GetProcAddress requires that name be ANSI, not UNICODE.Thanks for clearing that up.;  HWMODULE is a typo. Try HMODULE [smile]   ;  Well it's my fault, I typoed it first :);  Here's another tip. If the functions you're interested in importing are exported by kernel32.dll or ntdll.dll (on NT/2K/XP), you can use GetModuleHandle to obtain an HMODULE and you don't have to call FreeLibrary on it later. Those two dlls will always be loaded into the address space of a process.   ;  Thanks for the tip. Atm I'm just trying to link to a dll I created myself using blitzmax. Can anyone see where I've gone wrong here? It's just a simple test that opens a dll I created but it crashes saying 'This application needs to close' etc.#include &lt;iostream&gt;#include &lt;windows.h&gt;typedef int (WINAPI *testfunc)( int a, int b );int main(){std::cout &lt;&lt; "Hello world!" &lt;&lt; std::endl;    HMODULE lib = LoadLibrary( "demo.dll");    if( lib ){   std::cout &lt;&lt; "Library Opened." &lt;&lt;std::endl;   testfunc func = (testfunc)GetProcAddress( lib,"AddIntegers" );   int ret = func( 2, 3 );   if( ret== 5 )   {  std::cout &lt;&lt; "Number was correct." &lt;&lt; std::endl;   }else{  std::cout &lt;&lt; "Inccorect number." &lt;&lt; std::endl;   }    }else{   std::cout &lt;&lt; "Library failed to open. Shit." &lt;&lt; std::endl;    }    while( 1==1 )    {    }return 0;}