PDA

Click to See Complete Forum and Search --> : Using dll exports in c++


el-half
October 10th, 2004, 03:23 PM
Hey, I am trying to use exported dll functions like in the following working code:

#include <windows.h>
#include <iostream.h>
#include <stdio.h>
#include <conio.h>

#define MAXMODULE 50

typedef void (WINAPI*cfunc)();

cfunc DoSomething;

void main() {

HINSTANCE hLib=LoadLibrary("SHELL32.dll");


if(hLib==NULL) {

cout << "Unable to load library!" << endl;
getch(); //lame pause
return;
}

char mod[MAXMODULE];

GetModuleFileName((HMODULE)hLib, (LPTSTR)mod, MAXMODULE);
cout << "Library loaded: " << mod << endl;


DoSomething=(cfunc)GetProcAddress((HMODULE)hLib, "SHExitWindowsEx");

if((DoSomething==NULL)) {

cout << "Unable to load function(s)." << endl;
FreeLibrary((HMODULE)hLib);
return;
}

DoSomething();

FreeLibrary((HMODULE)hLib);

getch(); //lame pause
}


How do I use a function which needs arguments (unlike SHExitWindowsEx)? And where would I find the arguments it needs?

jdenny
October 11th, 2004, 09:13 AM
See this article for an example of a function that needs a (string) argument:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/using_run_time_dynamic_linking.asp

I believe SHExitWindowsEx takes an integer argument and is only supported on Windows ME/98:
http://support.microsoft.com/default.aspx?scid=kb;en-us;234216

You can use ExitWindowsDialog with no arguments on newer Windows. It will display the standard Shut Down Windows dialog. Unfortunately, there is no way to know what the user chose. This function will immediately begin the shut down, reboot or whatever she chose (including cancel).

Note that using LoadLibrary incorrectly can pose security risks (for example, if someone has altered the PATH or replaced the shell32.dll with her own version for malicious purposes). In this case, LoadLibrary should be provided with a fully-qualified path to the "trusted" dll. Also, examine output from DllGetVersion to ensure the correct version of dll is used.

Peace always,
<jdenny>

el-half
October 11th, 2004, 09:22 AM
I understand now, thanks.

#include <windows.h>
#include <iostream>

#define MAXMODULE 50

typedef int (WINAPI*cfunc)(HWND, LPCTSTR, LPCTSTR, UINT);


void main() {

HINSTANCE hLib=LoadLibrary("USER32.dll");
cfunc DoSomething;


if(hLib==NULL) {

std::cout << "Unable to load library!" << std::endl;
std::cin.get();
return;
}

char mod[MAXMODULE];

GetModuleFileName((HMODULE)hLib, (LPTSTR)mod, MAXMODULE);
std::cout << "Library loaded: " << mod << std::endl;


DoSomething=(cfunc)GetProcAddress((HMODULE)hLib, "MessageBoxA");

if((DoSomething==NULL)) {
std::cout << "Unable to load function(s)." << std::endl;
FreeLibrary((HMODULE)hLib);
return;
}

DoSomething(0, "Hello World!", "Information", MB_ICONINFORMATION);

FreeLibrary((HMODULE)hLib);

std::cin.get();
}


Someone else has provided me with this code, I simply forgot to give the correct typedef:
typedef int (WINAPI*cfunc)(HWND, LPCTSTR, LPCTSTR, UINT);