Monday, December 13, 2004

Okay, latest and greatest on this project.

I have a button that calls an unmanaged function looking at the properties in the camera. I'm getting a NullReferenceException AFTER the function runs in system.windows.forms.dll.

I declare a bunch of IntPtrs at the begining of my class that are all Private. (The .Dispose method calls a function called FlushIt, that calls an unmanaged function to clear those IntPtrs.)

Here's the function. Remember, it's after this that I get my errors. But something here probably isn't getting cleared correctly. The function calls unmanaged code to walk an iterator which was created by unmanaged code. I've also included the Private Declarations of the IntPtrs. 'myPro4' is the managed wrapper to the unmanaged DLL.
'References, IntPtrs, for each one of these you must
'Add a FreeRef command in FlushIt
Private myLibMgr As IntPtr = Marshal.AllocHGlobal(1)
Private myCamIter As IntPtr = Marshal.AllocHGlobal(1)
Private myDirItemRef As IntPtr = Marshal.AllocHGlobal(1)
Private myIORef As IntPtr = Marshal.AllocHGlobal(1)
Private myCameraRef As IntPtr = Marshal.AllocHGlobal(1)
Private myPropIter As IntPtr = Marshal.AllocHGlobal(1)
Private myPropRef As IntPtr = Marshal.AllocHGlobal(1)
'End References, IntPtrs

Private Sub FireCamera()
Dim thisCamera As String 'How we know what camera we're on as we iterate.
Dim theSize As Integer = 0
Dim theDataType As Integer = 0
Dim ptrToAttrib As IntPtr = Marshal.AllocHGlobal(1)
Dim ptrToLabel As IntPtr = Marshal.AllocHGlobal(1)

Try
'---------------------------
'Look for the camera
Do
'Get the camera reference
myStatus = myPro4.KIteratorNext(myCamIter, myCameraRef)

'Get the serial
myStatus = myPro4.KGetAttributeInfo(myCameraRef, 501, theDataType, theSize)
If myStatus = 0 Then myStatus = myPro4.KGetAttributeValue(myCameraRef, 501, theSize, ptrToAttrib)
thisCamera = Marshal.PtrToStringAnsi(ptrToAttrib)

Loop Until thisCamera = useCamera
'---------------------------

'---------------------------
'Get the Iterator of properties for the camera
myStatus = myPro4.KAllocPropertyIterator(myCameraRef, Me.myPropIter)
'---------------------------

'---------------------------
'Get the reference to the 'saveEnable' property
If myStatus = 0 Then
Dim theProperty, theLabel As String
myStatus = myPro4.KIteratorNext(myPropIter, myPropRef)
Do

'Get the serial
myStatus = myPro4.KGetAttributeInfo(myPropRef, 1004, theDataType, theSize)
If myStatus = 0 Then
myStatus = myPro4.KGetAttributeValue(myPropRef, 1004, theSize, ptrToAttrib)

If myStatus = 0 Then
theProperty = Marshal.ReadInt32(ptrToAttrib)
End If

myStatus = myPro4.KGetAttributeValue(myPropRef, myPro4.KPDCPropEnumValueLabelID(Marshal.ReadInt32(ptrToAttrib)), 256, ptrToLabel)
If myStatus = 0 Then
theLabel = Marshal.PtrToStringAnsi(ptrToLabel)
End If

End If

Call Me.WriteOutput(theLabel & " - " & theProperty)

myPro4.KFreeRef(myPropRef)
myStatus = myPro4.KIteratorNext(myPropIter, myPropRef)

Loop Until myStatus <> 0

Else
WriteOutput("Could not load camera properties")
End If
'---------------------------

'---------------------------
'Clean up a bit
myStatus = myPro4.KIteratorReset(myCamIter)
Marshal.FreeHGlobal(ptrToAttrib)
Marshal.FreeHGlobal(ptrToLabel)
'---------------------------

Catch ex As Exception
Call Me.WriteOutput(ex.ToString)

Finally

End Try
End Sub

No comments: