Utilizzare funzioni contenute in una DLL in C++

From RVM Wiki
Jump to navigation Jump to search

Esempio:

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

int main () {
	
	/*Typedef the hello function*/
	typedef void (*pfunc)();
	
	/*Windows handle*/
	HANDLE hdll;
	
	/*A pointer to a function*/
	pfunc hello;
	
	/*LoadLibrary*/
	hdll = LoadLibrary("message.dll");
	
	/*GetProcAddress*/
	hello = (pfunc)GetProcAddress(hdll, "hello");
	
	/*Call the function*/
	hello();
	return 0;
}

Come esempio da richiamare, si può usare la funzione isUserAdmin contenuta nella DLL standard di Windows shell32.dll: MSDN:IsUserAnAdmin Function ()

Vedi http://rvmserver:8000/changeres

// A simple program that uses LoadLibrary and 

// GetProcAddress to access myPuts from Myputs.dll. 

 

#include <windows.h> 

#include <stdio.h> 

 

typedef int (__cdecl *MYPROC)(LPWSTR); 

 

int main(VOID) 

{ 

    HINSTANCE hinstLib; 

    MYPROC ProcAdd; 

    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 

 

    // Get a handle to the DLL module.

 

    hinstLib = LoadLibrary(TEXT("shell32.dll")); 

 

    // If the handle is valid, try to get the function address.

 

    if (hinstLib != NULL) 

    { 

        ProcAdd = (MYPROC) GetProcAddress(hinstLib, "IsUserAnAdmin"); 

 

        // If the function address is valid, call the function.

 

        if (NULL != ProcAdd) 

        {

            fRunTimeLinkSuccess = TRUE;

            (ProcAdd) (L"Message sent to the DLL function\n"); 

        }

        // Free the DLL module.

 

        fFreeResult = FreeLibrary(hinstLib); 

    } 



    // If unable to call the DLL function, use an alternative.

    if (! fRunTimeLinkSuccess) 

        printf("Message printed from executable\n"); 

    return 0;

}


Riferimenti