Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Secure Function Pointer and Callbacks in Windows Programming

, 4 May 2011
This article explains the usage of function pointer and callback in Windows application programming Interface (API).
Securefunctionpointer_CFunctionPointer.zip
CFunctionPointer
CFunctionPointer
CFunctionPointer.suo
Securefunctionpointer_CPPFunctionPointer.zip
CPPFunctionPointer
CPPFunctionPointer
CPPFunctionPointer.suo
Securefunctionpointer_SecureFP.zip
SecureFP
SecureFP
SecureFP.suo
Securefunctionpointer_UnsafeFP.zip
UnsafeFP
UnsafeFP
UnsafeFP.suo
Securefunctionpointer_Win32DLL.zip
Win32DLL
Win32DLL
Win32DLL.suo
Win32DLL.def
Securefunctionpointer_Win32DLLClient.zip
Win32DLLClient
Win32DLLClient
Win32DLLClient.suo
// Win32DLLClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<windows.h>

typedef int (__cdecl *MYPROC)(); 
typedef int (__cdecl *SORTDISPLAYPROC)(int arr[],int); 
typedef int (__cdecl *SEARCHPROC)(int arr[],int,int,int); 

int _tmain(int argc, _TCHAR* argv[])
{
	int arr[]={11,99,88,44,66,55,33,77,22,10,9,6};
	int s = 12;
	int svalue = 33;

	int sResult = 0;

	HINSTANCE hinstLib; 
    MYPROC ProcAdd; 
	SORTDISPLAYPROC ProcSort, ProcDisplay;
	SEARCHPROC lSearch, bSearch;
    BOOL fFreeResult;
 
    // Get a handle to the DLL module.
     hinstLib = LoadLibrary(TEXT("Win32DLL")); 
 

	 // If the handle is valid, try to get the function address.
     if (hinstLib != NULL) 
    { 

        ProcAdd = (MYPROC) GetProcAddress(hinstLib, "InitBubbleSort"); 
		ProcDisplay = (SORTDISPLAYPROC) GetProcAddress(hinstLib, "Display"); 
		ProcSort = (SORTDISPLAYPROC) GetProcAddress(hinstLib, "BubbleSort"); 

		lSearch = (SEARCHPROC) GetProcAddress(hinstLib, "LinearSearch"); 
		bSearch = (SEARCHPROC) GetProcAddress(hinstLib, "BinarySearch"); 


		int *ipArr = (int*) GetProcAddress(hinstLib, "arr");
		int *Size = (int*) GetProcAddress(hinstLib, "nSize");
		int *sv = (int*) GetProcAddress(hinstLib, "searchval");

		ipArr=arr;
		Size = &s;
		sv = &svalue;

        // If the function address is valid, call the function.
         if (NULL != ProcAdd){
            (ProcAdd)(); 
        }


		printf("\nDisply the array of elements before BubbleSort \n");
		 // If the function address is valid, call the function.
         if (NULL != ProcDisplay){
            (ProcDisplay)(arr,s); 
        }

		
		 // If the function address is valid, call the function.
         if (NULL != ProcSort){
            (ProcSort)(arr,s); 
        }


		 printf("\nDisply the array of elements After BubbleSort \n");
		   // If the function address is valid, call the function.
         if (NULL != ProcDisplay){
            (ProcDisplay)(arr,s); 
        }

	  printf("\nSearch using LinearSearch \n");
		if (NULL != lSearch){
            sResult = (lSearch)(arr,svalue,0,s); 
        }
		
		if(sResult == -1)
			printf("Search element not found!\n");
		else
			printf("Element %d found in %d position!\n", svalue,sResult);

		

        // Free the DLL module.
         fFreeResult = FreeLibrary(hinstLib); 
    } 
 
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

R.selvam
Software Developer (Senior)
India India
Selvam has worked on several technologies like Core Java, VC++, MFC, Windows API and Weblogic server. He takes a lot of interest in reading technical articles and enjoy writing them too. He has been awarded as a Microsoft Community Star in 2004, MVP in 2005-06, SCJP 5.0 in 2009, Microsoft Community Contributor(MCC) 2011.
 
Github : https://github.com/selvamselvam
Web site: http://www15.brinkster.com/selvamselvam
Follow on   LinkedIn

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 4 May 2011
Article Copyright 2011 by R.selvam
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid