Connecting Tech Pros Worldwide Forums | Help | Site Map

Unmanaged To Managed - Please Help

Newbie
 
Join Date: Sep 2009
Posts: 3
#1: Sep 15 '09
Hi

Please could someone help me with the following, i'm trying to port the unmanaged C++ below into either VB.Net or C# (i don't mind which).

Expand|Select|Wrap|Line Numbers
  1.  
  2. typedef struct {
  3.   U32 Size;
  4.   U32 Start;
  5.   U32 Reserved1;
  6.   U32 Reserved2;
  7. } EVENT_INFO;
  8.  
  9. typedef enum {
  10.   EVENT_ERASE,
  11.   EVENT_PROGRAM,
  12.   EVENT_VERIFY
  13. } EVENT_TYPE;
  14.  
  15. typedef void EVENT_FUNC(EVENT_TYPE event, const EVENT_INFO * info);
  16.  
  17. STDCALL void SetProgressEvent (EVENT_FUNC * eventFunc);
  18.  
  19.  
For clarity, i will post the code i have written so far in a reply. Basically, all the above code does is provide feeback on erase, write and verify cycles when programming a microprocessor.

Thanks in advance

Kev

Newbie
 
Join Date: Sep 2009
Posts: 3
#2: Sep 15 '09

re: Unmanaged To Managed - Please Help


The code i have so far is...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Friend Structure ProgressInformation
  3.     Public Size As UInt32
  4.     Public Start As UInt32
  5.     Public Reserved1 As UInt32
  6.     Public Reserved2 As UInt32
  7. End Structure
  8.  
  9. Friend Enum ProgressEvent
  10.     ChipErase
  11.     Program
  12.     Verify
  13. End Enum
  14.  
  15. <UnmanagedFunctionPointer(CallingConvention.StdCall)> _
  16. Public Delegate Sub ProgressDelegate(ByVal progressEvent As ProgressEvent, ByVal state As ProgressInformation)
  17.  
  18. <DllImport(LIBRARY, EntryPoint:="SetProgressEvent ", CallingConvention:=CallingConvention.StdCall)> _
  19. Friend Shared Sub SetProgressHandler(ByVal notifyHandler As ProgressDelegate)
  20.     ' Empty method body
  21. End Sub
  22.  
  23.  

I use the imported method as follows...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub UpdateProgress(ByVal progressEvent As ProgressEvent, ByVal state As ProgressInformation)
  3.     Console.WriteLine(progressEvent.ToString())
  4.     Console.WriteLine("Size:{0}, Start:{1}, Reserved1:{2}, Reserved2:{3}", state.Size, state.Start, state.Reserved1, state.Reserved2)
  5. End Sub
  6.  
  7. ...
  8. Public Sub New()
  9. .... other initialisation code here
  10.  
  11.     SetProgressHandler(AddressOf UpdateProgress)
  12.  
  13. .... other dispose code here
  14. End Sub
  15.  
Now, the first time the UpdateProgress method runs i get valid data in the structure. As soon as the method returns, i get an AccessViolationException saying that the memory is corrupt. I'm thinking its something related to the way i've declared the import but i'm struggling to understand what.

Once again, thanks for any help in advance

Kev
Newbie
 
Join Date: Sep 2009
Posts: 3
#3: Sep 16 '09

re: Unmanaged To Managed - Please Help


And the answer is.... CDECL not STDCALL
Reply