Problem getting callback events from C code when using P/Invoke

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • microcool1024
    New Member
    • Apr 2010
    • 1

    Problem getting callback events from C code when using P/Invoke

    I am having a problem getting a C callback to correctly activate in a C# program after using a dllimport.

    Here is the declaration of the C code in the C header:

    Code:
    typedef BOOL (CALLBACK *HII_PaymentAcceptedProc)(BYTE bSrcNode, BYTE bNumber, BYTE bType, WORD wValue);
    
    HIIFAPI UINT WINAPI HII_PaymentAccepted(BYTE bDestNode, BYTE bNumber, BYTE bType, WORD wValue);
    Then in the demo C program I have it is called like this:

    Code:
    HII_InstallPaymentAcceptedProc(PaymentAcceptedProc);
    
    
    static BOOL CALLBACK PaymentAcceptedProc(BYTE bSrcNode, BYTE bNumber, BYTE bType, WORD wValue)
    {
    	LPTSTR lpszType;
    	UNREFERENCED_PARAMETER(bSrcNode);
    
    	// Payment accepted event received
    	switch(bType)
    	{
    		case 0:		lpszType = TEXT("Coin");		break;
    		case 1:		lpszType = TEXT("Value Token"); break;
    		case 2:		lpszType = TEXT("Vend Token");	break;
    		case 3:		lpszType = TEXT("Known Slug");	break;
    		case 4:		lpszType = TEXT("Note");		break;
    		default:	lpszType = TEXT("Unknown");		break;
    	}
    
    	FormatEvent(hwndMain, TEXT("Payment Accepted, number %u, type %s, value %u\r\n"),
    		bNumber, lpszType, wValue);
    
    	return TRUE;
    }
    I then tried to translate this into C# like this:
    Code:
    public delegate bool HII_PaymentAcceptedProc(Byte bSrcNode, Byte bNumber, Byte bType, UInt16 wValue);
    [DllImport("HiiF.dll")]
    public static extern UInt32 HII_InstallPaymentAccepted(HII_PaymentAcceptedProc pfnProc);
    Then call it like this:

    Code:
    public bool PaymentAcceptedProc(byte bSrcNode, byte bNumber, byte bType, UInt16 wValue)
    {
                switch(bType)
    	{
    		case 0:		break;
    		case 1:		break;
    		case 2:		break;
    		case 3:		break;
    		case 4:		break;
    		default:	break;
    	}
    return true;
            }
    
    HII_Wrapper.HII_PaymentAcceptedProc PaymentAcceptedProcDelegate = new HII_Wrapper.HII_PaymentAcceptedProc(this.PaymentAcceptedProc);
    
    HII_Wrapper.HII_InstallPaymentAcceptedProc(PaymentAcceptedProcDelegate);
    However it never his the PaymentAccepted Proc function and I don't understand why. Are there any thoughts? Am I handling the delegates incorrectly?
Working...