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:
Then in the demo C program I have it is called like this:
I then tried to translate this into C# like this:
Then call it like this:
However it never his the PaymentAccepted Proc function and I don't understand why. Are there any thoughts? Am I handling the delegates incorrectly?
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);
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;
}
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);
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);