Jump to content

Calling Dll functions in C?


Recommended Posts

I'm starting to program in C and i have a question.

What is the correct way to use functions from a DLL in C.

(BTW: It's user32.dll)

The part I've bolded is important. You've just started, therefore, you shouldn't even be thinking about Windows-specific stuff right now. Learn the core language first (more or less), then learn platform-specific details such as dynamic linking. Trying to learn the core language *and* the Windows API simultaneously will just steepen the learning curve by a significant amount.
Link to post
Share on other sites

The part I've bolded is important. You've just started, therefore, you shouldn't even be thinking about Windows-specific stuff right now. Learn the core language first (more or less), then learn platform-specific details such as dynamic linking. Trying to learn the core language *and* the Windows API simultaneously will just steepen the learning curve by a significant amount.

Completely agree.

It's a shame that most basic C or C++ courses don't give you a "step-up"or at least an example to calling a dll in C/C++.

Link to post
Share on other sites

It's a shame that most basic C or C++ courses don't give you a "step-up"or at least an example to calling a dll in C/C++.

That's because it's "platform specific". :)

Anyway, you don't need anything special for the user32.dll, normally. You just need to tell the linker that it should use user32.lib as an additional dependency. Any perhaps include the relevant header files in your C or C++ program, as mentioned in the MS docs. Ya know, things like

#include "afxwin.h"
#include "afxres.h"
#include <afxpriv.h>
#include "windows.h"
#include "stdafx.h"
#include <stdio.h>

However, if you happen to use Visual C++ Express Edition, you'll quickly learn that MS decided that you don't want to call some of the more interesting function. Trust me, MS knows better what you really want than you'll ever know yourself. :P

One solution might be to buy the full MS VC++ Edition. Yuk. Another option, however, is to download and install the Microsoft Platform SDK for Windows Server 2003 R2. The installation part (so it can be used in MS VC++ Express) is a bit tricky, but there are plenty of tutorials on the web which explain how this will work.

Best regards, Klaus

Edited by Klaws
Link to post
Share on other sites

For calling a function inside a DLL, you need three functions; LoadLibrary, GetProcAddress, and FreeLibrary.

Use LoadLibrary to load the dll and get the module instance, and the opposite with FreeLibrary - call it once you are done with the dll.

You use GetProcAddress to the get the address the function is at..

Here is a full message box example:

#undef UNICODE //don't like unicode
#include <windows.h>

//The syntax of the MessageBoxA function in user32.dll
typedef int(__stdcall *pFunc)(HWND, LPCSTR, LPCSTR, UINT);

//Our function prototype
int MsgBox(char *text);


int main()
{
    //Simple Call the function
    MsgBox("Hello World!");
}

int MsgBox(char *text)
{
    //Load the DLL into memory
    HMODULE hDll = LoadLibrary("user32.dll");

    //We get the address of the function inside the dll.
    //Then we typecast it to fit the function's syntax.
    pFunc lpFunc = (pFunc)GetProcAddress(hDll, "MessageBoxA");

    //We can now call the function
    int ret = lpFunc(NULL, text, "Message Box Test", MB_OK);

    //And don't forget to free the memory
    FreeLibrary(hDll);

    //return the function's return value.
    return ret;
}

Hope that helped :)

Btw, if that function is going to be used multiple times through your program, move the LoadLibrary, GetProcAddress, and FreeLibrary to outside the function so Window's doesn't allocate memory for the DLL each call.

Edited by chris95219
Link to post
Share on other sites

For calling a function inside a DLL, you need three functions; LoadLibrary, GetProcAddress, and FreeLibrary.

Use LoadLibrary to load the dll and get the module instance, and the opposite with FreeLibrary - call it once you are done with the dll.

You use GetProcAddress to the get the address the function is at..

Here is a full message box example:

#undef UNICODE //don't like unicode
#include <windows.h>

//The syntax of the MessageBoxA function in user32.dll
typedef int(__stdcall *pFunc)(HWND, LPCSTR, LPCSTR, UINT);

//Our function prototype
int MsgBox(char *text);
int main()
{
    //Simple Call the function
    MsgBox("Hello World!");
}

int MsgBox(char *text)
{
    //Load the DLL into memory
    HMODULE hDll = LoadLibrary("user32.dll");

    //We get the address of the function inside the dll.
    //Then we typecast it to fit the function's syntax.
    pFunc lpFunc = (pFunc)GetProcAddress(hDll, "MessageBoxA");

    //We can now call the function
    int ret = lpFunc(NULL, text, "Message Box Test", MB_OK);

    //And don't forget to free the memory
    FreeLibrary(hDll);

    //return the function's return value.
    return ret;
}

Hope that helped :)

Btw, if that function is going to be used multiple times through your program, move the LoadLibrary, GetProcAddress, and FreeLibrary to outside the function so Window's doesn't allocate memory for the DLL each call.

That is BEAUTIFUL!

Thank you!

Link to post
Share on other sites

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...