Sign in to follow this  

Loading Dll's, please help

This topic is 4104 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

Hi In visual basic you load a dll function by the following:
private declare function *function name here* lib "dll name here" (arguments here) *
return value type*

'eg:
declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long 
How could i load (for example) kernel32.dll in c++ and do the same thing as in the above example. i am using dev c++ thanks in advance

Share this post


Link to post
Share on other sites
See Using Run-Time Dynamic Linking, LoadLibrary, GetProcAddress, FreeLibrary.

One thing worth noting about dynamic linking to functions exported by Kernel32.dll. Kernel32.dll will always be statically linked to your executable file, so if you use GetModuleHandle instead of LoadLibrary to get the handle, you don't have to call FreeLibrary on it later.

typedef BOOL (WINAPI *BeepT)(DWORD dwFreq,DWORD dwDuration);

HMODULE k32 = GetModuleHandle("kernel32.dll");

BeepT beep = (void*)GetProcAddress(k32,"Beep");

beep(37,37);

Share this post


Link to post
Share on other sites
Thanks for your responce.

I can see how this works... buts i keep getting an error on line 7 (it highlights line 6).
the error is: invalad conversion from 'void*' to 'BOOL'(*)(DWORD,DWORD)'

here is the code


#include <windows.h>

int WinMain()
{
typedef BOOL (WINAPI *BeepT)(DWORD dwFreq, DWORD dwDuration);
HMODULE k32 = GetModuleHandle("kernel32.dll");
BeepT beep = (void*)GetProcAddress(k32,"Beep");

beep(37,37);

return 0;
}


also what is the difference between BOOL (in caps not reconised by compiler) and bool(lower caps, reconised in compiler).
One more thing where dose BeepT come from or dose it not matter what you put for it.

thanks again

Share this post


Link to post
Share on other sites

typedef BOOL (WINAPI *BeepT)(DWORD dwFreq, DWORD dwDuration);
HMODULE k32 = GetModuleHandle(L"kernel32.dll");
BeepT beep = (BeepT)GetProcAddress(k32,"Beep");

beep(37,37);

Share this post


Link to post
Share on other sites
What dose the 'L' do.
and this time i get an error on line 6 (it highlights line 5)

line 5 is: HMODULE k32 = GetModuleHandle(L"kernel32.dll");

Error:
cannot convert `const wchar_t*' to `const CHAR*' for argument `1' to `HINSTANCE__* GetModuleHandleA(const CHAR*)'

thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by aaroncox_123
Thanks for your responce.

I can see how this works... buts i keep getting an error on line 7 (it highlights line 6).
the error is: invalad conversion from 'void*' to 'BOOL'(*)(DWORD,DWORD)'

here is the code


#include <windows.h>

int WinMain()
{
typedef BOOL (WINAPI *BeepT)(DWORD dwFreq, DWORD dwDuration);
HMODULE k32 = GetModuleHandle("kernel32.dll");
BeepT beep = (void*)GetProcAddress(k32,"Beep");

beep(37,37);

return 0;
}




Maybe it's just a C thing, not a C++ thing, but the purpose of casting the return value as a void pointer is because it's supposed to be possible to assign a void pointer to any type of pointer variable. GetProcAddress returns a FARPROC, which is really just a placeholder type to allow the compiler to properly deal with the call. You always need to cast the return from GetProcAddress as whatever type of function you're expecting.

todi1856 points out how it's supposed to be done, formally

BeepT beep = (BeepT)GetProcAddress(k32,"Beep");

casting to the specific function pointer type.


Quote:
Original post by aaroncox_123
also what is the difference between BOOL (in caps not reconised by compiler) and bool(lower caps, reconised in compiler).


bool (lower case) is an intrinsic type in C++ - like int, short, char, float etc. There is no similar instrinsic boolean type in C, so a typedef is used to create an alias for an int. For example.

typedef int BOOL;

You can search the windows header files to find the exact typedef. The Win32 Api is crafted with a C interface, so the function prototypes use BOOL instead of bool.

Quote:
Original post by aaroncox_123
One more thing where dose BeepT come from or dose it not matter what you put for it.


typedef BOOL (WINAPI *BeepT)(DWORD dwFreq, DWORD dwDuration);

It's a definition of a data type, in this case, a function pointer. Yes it does matter. The function pointer has to have the same signature as the function being imported. The signature of a function is the return type, the calling convention and the argument type(s). Here, the return type is BOOL, the calling convention is WINAPI, and the arguments types are DWORD, DWORD.

Quote:
Original post by aaroncox_123
What dose the 'L' do. and this time i get an error on line 6 (it highlights line 5)

line 5 is: HMODULE k32 = GetModuleHandle(L"kernel32.dll");

Error:
cannot convert `const wchar_t*' to `const CHAR*' for argument `1' to `HINSTANCE__* GetModuleHandleA(const CHAR*)'


When used with a string literal, that is, a string of characters written right into the code, the prepended L tells the compiler to use wide characters, that is, two byte characters, rather than single byte ANSI characters. Generally these two byte characters are referred to as UNICODE, but the details aren't that simple unfortunately. See Unicode in the Windows API for more information.

Regarding the error you're getting,

`HINSTANCE__* GetModuleHandleA(const CHAR*)'

Notice the 'A' at the end of the function name. That says that the function expects the string argument to use ANSI characters rather than wide characters. Functions that expect wide characters have a 'W' appendend to them. So you can specifically specify A or W or you can use a preprocessor macro TEXT that will handle all that for you.

HMODULE k32 = GetModuleHandle(TEXT("kernel32.dll"));
HMODULE k32 = GetModuleHandleW(L"kernel32.dll");
HMODULE k32 = GetModuleHandleA("kernel32.dll");

Also note that GetProcAddress, always expects an ANSI string argument.

Lastly, you might want to pick up a copy of Programming Windows by Charles Petzold.

Share this post


Link to post
Share on other sites
IT WORKS...

Thanks LessBread.
I changed WinMain() to just main() and used the TEXT macro.

cause before i changed WinMain to main i got a Linker error syaing Undeclared refference to 'WinMain@16'


#include <windows.h>
int main ()
{
typedef BOOL (WINAPI *BeepT)(DWORD dwFreq, DWORD dwDuration);
HMODULE k32 = GetModuleHandle(TEXT("kernel32.dll"));
BeepT beep = (BeepT)GetProcAddress(k32,"Beep");

beep(800,50);
return 0;
}


Thanks again.

Share this post


Link to post
Share on other sites

This topic is 4104 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this