rougie
 
 
  Joined: 14 Jun 2006 Posts: 31
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| Help for compiler error! | 
			 
			
				 Posted: Tue Jan 20, 2009 1:03 pm     | 
				     | 
			 
			
				
  | 
			 
			
				Hello, please consider the following code:
 
 
 	  | Code: | 	 		  
 
=================================== krn.h
 
typedef long *LRESULT;
 
typedef long WPARAM;
 
typedef long LPARAM;
 
 
typedef struct tagHwnd{
 
void *handle;                  
 
} HWND;
 
 
typedef LRESULT (*WNDPROC)(HWND, long, WPARAM, LPARAM);   
 
 
typedef struct tagWnd{   
 
WNDPROC  lpfnWndProc;
 
int      style;
 
long     backGround;
 
long     titleMsg; 
 
long     extra;
 
} WND, *pWND;
 
 
typedef struct tagRwp{  
 
HWND     hwnd;
 
int      showWinEnable;
 
} RWP;
 
 
 
typedef struct tagMsg{
 
HWND    hwnd;
 
long    msg;
 
WPARAM  wParam;
 
LPARAM  lParam;
 
long    time;
 
} KM_MSG, *pKM_MSG;  
 
 
RWP rwp[3] = {0,0,0,0,0,0};
 
 
void ULC_KERNEL_dispatch_message(KM_MSG *K); 
 
   
 
================================krn.c
 
 
#include <Dv4685.h>              // DEVICE INNITIALIZATIONS
 
#include <stdlib.h>              // CCS LIBRARY STANDARD LIBRARY
 
#include <KERNEL.h>              // KERNEL HEADER FILE!
 
 
LRESULT KWP(HWND x, long m, WPARAM w, LPARAM l);   
 
 
LRESULT callerFunction(LRESULT (*WNDPROC) (HWND, long, WPARAM, 
 
LPARAM), HWND h, long m, WPARAM w, LPARAM l);
 
 
void main()
 
{
 
KM_MSG  u;           // Create an KM_MSG object
 
pKM_MSG msg = &u;    // Assign the address of a KM_MSG object to a pKM_MSG pointer object
 
WND     wnd;         // Create a WND object
 
 
// DEFINE A WINDOW!
 
wnd.lpfnWndProc   = KWP;   // Assign a function pointer ! (Assigns the pointer to a function called KWP  !!! )
 
wnd.style         = 0;     // Assign other stuff...
 
wnd.backGround    = 0;     // Assign other stuff...
 
wnd.titleMsg      = 0;     // Assign other stuff...
 
wnd.extra         = 0;     // Assign other stuff...
 
 
 
// Assign the address of a wnd object to the void pointer called "handle" 
 
//which is a member of tagHwnd structure
 
 
(rwp[0].hwnd).handle = &wnd;   
 
msg->hwnd = rwp[0].hwnd;       // Assign a hwnd to another hwnd         
 
ULC_KERNEL_dispatch_message(msg);  // Pass a pointer to a msg object
 
 
}
 
 
void ULC_KERNEL_dispatch_message(KM_MSG *msg) 
 
{
 
LRESULT zzz;
 
 
// Call the correct function based on the contents of the function pointer  
 
// stored in "lpfnWndProc"
 
 
zzz = callerFunction(( (struct tagWnd*) (msg->hwnd).handle)->lpfnWndProc, msg->hwnd, msg->msg, msg->wParam, msg->lParam);
 
 
}
 
 
//CALLBACK FUNCTION IMPLEMENTATION
 
LRESULT callerFunction(LRESULT(*WNDPROC)(HWND, long, WPARAM, LPARAM) ,HWND h, long m, WPARAM w, LPARAM l)  
 
{ 
 
   WNDPROC (h, m, w, l);
 
   return 0; 
 
}
 
 
LRESULT KWP(HWND x, long m, WPARAM w, LPARAM l)  
 
{   
 
//... other code ...
 
return 0;
 
} | 	  
 
Errors do occur at compile time!
 
 
In reference to the sample code above, here is what I am trying to do:
 
 
================================
 
 
It is important to notice that I have defined a tagHwnd structure, like this: 
 
 	  | Code: | 	 		  typedef struct tagHwnd{
 
void *handle;                  
 
} HWND; | 	  
 
And It is also important to knowthat I have also defined an array of tagRWP structures where each element of the array contains an HWND and an showWinEnable member, like this:
 
 	  | Code: | 	 		  
 
typedef struct tagRwp{  
 
HWND     hwnd;
 
int      showWinEnable;
 
} RWP;
 
 
RWP rwp[3] = {0,0,0,0,0,0};
 
 | 	  
 
Basically what I am trying to do is, in main(), assign any function name to the lpfnWndProc member of a WND, like this:
 
 
 
//The following line could assign any function name. 
 
 	  | Code: | 	 		  
 
wnd.lpfnWndProc   = KWP;   // <<< Right now we are using KWP  | 	  
 
So if we want to reference the value in the “handle” member via the rwp[] array, we need to do this:
 
 	  | Code: | 	 		  | (rwp[0].hwnd).handle | 	  
 
So getting back to what I was saying, Once I assigned function KWP to lpfnWndProc I then store the address of the WND object to the void pointer “handle”, like this.
 
 	  | Code: | 	 		  | (rwp[0].hwnd).handle = &wnd;     | 	  
 
Then I assign the hwnd member of the first element of tagRwp structures to a hwnd member in the tagMsg, like this: 
 
 	  | Code: | 	 		  | msg->hwnd = rwp[0].hwnd;    | 	  
 
The msg object now has accessibility to the "lpfnWndProc" member stored in the hwnd object. The  “lpfnWndProc” member still contains the address of the function which is KWP’s address.
 
 
Next I pass the address of the msg object to the following function.
 
 	  | Code: | 	 		  | ULC_KERNEL_dispatch_message(msg);   | 	  
 
And therefore we allow the ULC_KERNEL_dispatch_message()  function’s  implementation to fetch the correct function based on the contents of the function pointer stored in "lpfnWndProc" by executing the following lines:
 
 
 	  | Code: | 	 		  LRESULT zzz;
 
 
zzz = callerFunction(((struct tagWnd*) (msg->hwnd).handle)->lpfnWndProc, msg->hwnd, msg->msg, msg->wParam, msg->lParam); | 	  
 
================================
 
 
Anybody experienced enough in using Callback types of functions this way in PICs using CCS ?
 
 
Thanking all in advance !
 
 
Regards
 
Roberto | 
			 
		  |