Jump to content
  • Advertisement
Sign in to follow this  

LoadLibraryA example/tutorial?

This topic is 4413 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 )
endif

So, 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?

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
Quote:
Original post by Konfusius
typedef 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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
Quote:
Original post by Konfusius
typedef 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!