.NET Forum / .NET Framework / Interop / December 2004
Passing and/or handling events to/from unmanaged DLL
|
|
Thread rating:  |
M K - 14 Dec 2004 20:47 GMT I'm using VB. (I have other posts on this project, but with other issues.)
The function signature in the documentation looks like this: //Specify a function to be called when the camera is disconnected from the system. After setting a notification function, the callback mechanism can be disabled by passing NULL for inNotificationFunc. KPDCStatus KPDCSetCameraEventNotification ( KPDCCameraRef inCameraRef, KPDCCameraEventNotificationFunc inNotificationFunc, void *inUserData);
In the demo code the do this: // Set up the camera event notification // The camera will send an event to notify you that the contents of the // active folder has changed. The most likely cause for a folder content // change event is when the camera had written a new image to the // directory. If you look for this event, you'll know when the image is // ready for retrieval after the shutter has been released. theStatus = theProcs.SetCameraEventNotification(theCamera, CameraEventHandler, NULL);
And CameraEventHandler looks like this: //////////////////////////////////////////////////////////////////////////////// // Description: // Callback function to handle camera events. If we receive a KPDCFolderContent // event we will set the global flag g_fYouveGotAPicture to true. // // Parameters: All defined in the *KPDCCameraEventNotificationFunc // callback data type in KProDCSDKTypes.h // //////////////////////////////////////////////////////////////////////////////// void KPDCCALLBACK CameraEventHandler( KPDCUInt32 inEvent, // The event code, see KPDCCameraEvents KPDCUInt32 inParam1, // Events may have an integer parameter const char *inParam2, // Events may have a string parameter KPDCUInt32 inParam2Length, // Length of the string parameter void *userData ) // Could be a pointer to a C++ object { if( (inEvent == KPDCAddFile) || (inEvent == KPDCFolderContent) ) { SetEvent(g_hImageReady); } }
I am trying to write my wrapper in VB. This code seems to be passing the name/address of an event handler to the unmanaged code. Is this true? None of what I've posted here was in the unmanaged code, it's all from a demo that is written in C++. I don't have the source to the unmanaged code, just the function information. (As seen in my first few lines here.)
M K - 15 Dec 2004 16:49 GMT Ok, the function signature of the unmanaged function looks like this in the documentation:
> //Specify a function to be called when the camera is disconnected from the > system. After setting a notification function, the callback mechanism can be > disabled by passing NULL for inNotificationFunc. > KPDCStatus KPDCSetCameraEventNotification ( KPDCCameraRef inCameraRef, > KPDCCameraEventNotificationFunc inNotificationFunc, void *inUserData); The demo code has this typedef:
>typedef void (KPDCCALLBACK *KPDCCameraEventNotificationFunc)( Int inEvent, // The event code, see KPDCCameraEvents Int inParam1, // Events may have an integer parameter const char *inParam2, // Events may have a string parameter Int inParam2Length, void *userData);
K, so how do I wrap KPDCSetCameraEventNotification and get my Visual Basic code to work with it.
I tried this: Declare Auto Function KSetCameraEventNotification Lib "DCSPro4SLR.dll" Alias "KPDCSetCameraEventNotification" (ByVal inIteratorRef As IntPtr, ByVal inNotificationFunc As EventHandler, <MarshalAs(UnmanagedType.AsAny)> ByVal inUserData As Object) As Integer
But I don't know how to pass my function to this function. Does anybody have any ideas? Please?
TSw - 15 Dec 2004 16:49 GMT void KPDCCALLBACK CameraEventHandler(...) looks like an asynchronous event handler, so it receives event notifications as generated by some other part of this dll
> void KPDCCALLBACK CameraEventHandler( > I am trying to write my wrapper in VB. This code seems to be passing the > name/address of an event handler to the unmanaged code. Is this true? M K - 15 Dec 2004 17:07 GMT Okay, then we've got this, also in the demo code where void KPDCCALLBACK CameraEventHandler(...) is found. theProcs is the unmanaged DLL:
// Set up the camera event notification // The camera will send an event to notify you that the contents of the // active folder has changed. The most likely cause for a folder content // change event is when the camera had written a new image to the // directory. If you look for this event, you'll know when the image is // ready for retrieval after the shutter has been released. theStatus = theProcs.SetCameraEventNotification(theCamera, CameraEventHandler, NULL);
TSw - 15 Dec 2004 17:53 GMT You have to hook your event handler to DLL's events. It is usually done by this schema: define event delegate define event handler method define DLL's SetHandlerOrHowIsCalled method (if it is supported by DLL) invoke DLL's SetHandlerOrHowIsCalled method and pass handler to it
i'm not familiar with VB, but i should work similar to c#
> Okay, then we've got this, also in the demo code where void KPDCCALLBACK > CameraEventHandler(...) is found. theProcs is the unmanaged DLL: [quoted text clipped - 7 lines] > theStatus = theProcs.SetCameraEventNotification(theCamera, > CameraEventHandler, NULL); M K - 15 Dec 2004 18:05 GMT Okay, then the question I have is, how do I define the SetHandlerOrHowIsCalled method (Step 3), what parameter. Then what do I pass to it when I invoke it. (Step 4)
Right now, I've tried to define the SetHandlerOrHowIsCalled method this way: Declare Auto Function KSetCameraEventNotification Lib "DCSPro4SLR.dll" Alias "KPDCSetCameraEventNotification" (ByVal inIteratorRef As IntPtr, ByVal inNotificationFunc As EventHandler, <MarshalAs(UnmanagedType.AsAny)> ByVal inUserData As Object) As Integer
Notice, the parameter name is 'inNotificationFunc' and I'm specifying the type of EventHandler. Is this correct?
You can post C# or point to examples, that is fine.
> You have to hook your event handler to DLL's events. It is usually done by > this schema: [quoted text clipped - 16 lines] > > theStatus = theProcs.SetCameraEventNotification(theCamera, > > CameraEventHandler, NULL); M K - 15 Dec 2004 18:51 GMT K, so I guess what you mean is this: 'Event Delegate Public Delegate Sub PictureTaken(ByVal inEvent As KPDCCameraEvents, ByVal inParam1 As Integer, ByVal inParam2 As Object, ByVal inParam2Length As Integer, ByVal userData As IntPtr)
'Define event method Private Sub CameraFired(ByVal inEvent As KPDCCameraEvents, ByVal inParam1 As Integer, ByVal inParam2 As Object, ByVal inParam2Length As Integer, ByVal userData As IntPtr) 'This event handles the camera events. 'We'll use the inEvent as a Select Case Dim fileName As String Select Case inEvent Case KPDCCameraEvents.KPDCAddFile RaiseNewFile(CType(inParam2, String)) End Select End Sub
'define DLL's SetHandlerOrHowIsCalled method (if it is supported by DLL) Declare Auto Function KSetCameraEventNotification Lib "DCSPro4SLR.dll" Alias "KPDCSetCameraEventNotification" (ByVal inIteratorRef As IntPtr, ByVal inNotificationFunc As PictureTaken, ByVal inUserData As IntPtr) As Integer
'invoke DLL's SetHandlerOrHowIsCalled method and pass handler to it myStatus = Me.KSetCameraEventNotification(myCameraRef, AddressOf Me.CameraFired, IntPtr.Zero)
Like so?
> You have to hook your event handler to DLL's events. It is usually done by > this schema: [quoted text clipped - 16 lines] > > theStatus = theProcs.SetCameraEventNotification(theCamera, > > CameraEventHandler, NULL); TSw - 16 Dec 2004 09:19 GMT Yes, it should look like that.
> K, so I guess what you mean is this: > 'Event Delegate [quoted text clipped - 46 lines] > > > theStatus = theProcs.SetCameraEventNotification(theCamera, > > > CameraEventHandler, NULL);
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|