473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. typedef BOOL (CALLBACK *HII_PaymentAcceptedProc)(BYTE bSrcNode, BYTE bNumber, BYTE bType, WORD wValue);
  2.  
  3. HIIFAPI UINT WINAPI HII_PaymentAccepted(BYTE bDestNode, BYTE bNumber, BYTE bType, WORD wValue);
  4.  
Then in the demo C program I have it is called like this:

Expand|Select|Wrap|Line Numbers
  1. HII_InstallPaymentAcceptedProc(PaymentAcceptedProc);
  2.  
  3.  
  4. static BOOL CALLBACK PaymentAcceptedProc(BYTE bSrcNode, BYTE bNumber, BYTE bType, WORD wValue)
  5. {
  6.     LPTSTR lpszType;
  7.     UNREFERENCED_PARAMETER(bSrcNode);
  8.  
  9.     // Payment accepted event received
  10.     switch(bType)
  11.     {
  12.         case 0:        lpszType = TEXT("Coin");        break;
  13.         case 1:        lpszType = TEXT("Value Token"); break;
  14.         case 2:        lpszType = TEXT("Vend Token");    break;
  15.         case 3:        lpszType = TEXT("Known Slug");    break;
  16.         case 4:        lpszType = TEXT("Note");        break;
  17.         default:    lpszType = TEXT("Unknown");        break;
  18.     }
  19.  
  20.     FormatEvent(hwndMain, TEXT("Payment Accepted, number %u, type %s, value %u\r\n"),
  21.         bNumber, lpszType, wValue);
  22.  
  23.     return TRUE;
  24. }
  25.  
I then tried to translate this into C# like this:
Expand|Select|Wrap|Line Numbers
  1. public delegate bool HII_PaymentAcceptedProc(Byte bSrcNode, Byte bNumber, Byte bType, UInt16 wValue);
  2. [DllImport("HiiF.dll")]
  3. public static extern UInt32 HII_InstallPaymentAccepted(HII_PaymentAcceptedProc pfnProc);
  4.  
Then call it like this:

Expand|Select|Wrap|Line Numbers
  1. public bool PaymentAcceptedProc(byte bSrcNode, byte bNumber, byte bType, UInt16 wValue)
  2. {
  3.             switch(bType)
  4.     {
  5.         case 0:        break;
  6.         case 1:        break;
  7.         case 2:        break;
  8.         case 3:        break;
  9.         case 4:        break;
  10.         default:    break;
  11.     }
  12. return true;
  13.         }
  14.  
  15. HII_Wrapper.HII_PaymentAcceptedProc PaymentAcceptedProcDelegate = new HII_Wrapper.HII_PaymentAcceptedProc(this.PaymentAcceptedProc);
  16.  
  17. HII_Wrapper.HII_InstallPaymentAcceptedProc(PaymentAcceptedProcDelegate);
  18.  
However it never his the PaymentAcceptedProc function and I don't understand why. Are there any thoughts? Am I handling the delegates incorrectly?
Apr 26 '10 #1
0 1396

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jeff Van Epps | last post by:
We've been unable to get events working going from C# to VJ++. We think that the C# component is being exposed properly as a ConnectionPoint, and the Advise() from the VJ++ side seems to be...
2
by: Stampede | last post by:
Hi guys 'n' girls, I want to use callback methods when using BeginInvoke on some events. So far no problem, but know I thought about what could happen (if I'm not completly wrong). Lets say an...
1
by: Sean | last post by:
Im fairly new to C# and am having a few problems getting events to fire when using COM interop with C#, can someone take a quick look at the following example and give me a pointer as to why the...
0
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
4
by: | last post by:
Hello, I have a worker thread ( a shared method in a class) that needs to callback to another (monitoring) class on certain events. I have been unable to figure out how to pass the callback...
10
by: Scooby | last post by:
Okay, I'm a noob at C# and microsoft's IDE, but have a fairly long history of programming (with, cough, Delphi). I'm writing a mobile app in C# with Visual Studio 2005. I think there must be...
4
by: sloan | last post by:
I"m trying to figure out what concept I'm missing here, or if its not a good idea .. or what. Here is my example.. code is below. I have an employee class. It has an event that can be raised....
5
by: sajin | last post by:
Hi All.. We are using VB .Net 2005 for implementing an API. API needs to generate events. For this client wants us to use Windows Callback (delegate implementation). The intention of using...
2
by: NannMaw | last post by:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Server Error in '/WebSite1' Application. -------------------------------------------------------------------------------- Invalid postback or callback argument. ...
9
by: 200dogz | last post by:
Hi guys, I want to have a button which opens up a new window when pressed. <asp:Button ID="Button1" runat="server" Text="Open new window" /> ... Button1.Attributes.Add("OnClick",
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.