NELyon Posted August 1, 2007 Share Posted August 1, 2007 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) Link to comment Share on other sites More sharing options...
piccaso Posted August 2, 2007 Share Posted August 2, 2007 include the header and call it the compiler makes your import table and the os loads the dll...but you probably mean loading and calling it manually / late bindings.take a look at MakeCUI i did that in there. CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map Link to comment Share on other sites More sharing options...
Valik Posted August 3, 2007 Share Posted August 3, 2007 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 comment Share on other sites More sharing options...
jvanegmond Posted August 3, 2007 Share Posted August 3, 2007 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++. github.com/jvanegmond Link to comment Share on other sites More sharing options...
Klaws Posted August 7, 2007 Share Posted August 7, 2007 (edited) 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. 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 August 7, 2007 by Klaws Link to comment Share on other sites More sharing options...
cppman Posted August 8, 2007 Share Posted August 8, 2007 (edited) 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 August 8, 2007 by chris95219 Miva OS Project Link to comment Share on other sites More sharing options...
NELyon Posted August 8, 2007 Author Share Posted August 8, 2007 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 comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now