GiuseppeDini
2010-12-09 07:57:46 UTC
I’ve written my first wrapper class for calling a c++ function.
The code is as follows and it doesn’t need to be commented because I
think its clear.
Do you think it is ok or that it needs come improvement?
’ like c++ counterpart
Public Structure managedStructure
Public Integer1 As Integer
Public Pointer1 As IntPtr
' ...
End Structure
Class myWrapper
Implements IDisposable
Public Ptr As IntPtr
Public Sub New(ByVal dynamParams As Integer, ByVal measureParams As
Integer, ByVal controlParams As Integer)
Dispose()
‘ allocate and initialize unmanaged Resource
Ptr = Emgu.CV.CvInvoke.allocateResource(dynamParams,
measureParams, controlParams)
GC.AddMemoryPressure(Runtime.InteropServices.Marshal.SizeOf(GetType(Emgu.CV.Structure.managedStructure)))
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
If Ptr <> IntPtr.Zero Then
Emgu.CV.CvInvoke.ReleaseResource(Ptr)
GC.RemoveMemoryPressure(Runtime.InteropServices.Marshal.SizeOf(GetType(Emgu.CV.Structure.
managedStructure)))
GC.SuppressFinalize(Me)
Ptr = IntPtr.Zero
End If
End Sub
Protected Overrides Sub Finalize()
Try
Dispose()
Finally
MyBase.Finalize()
End Try
End Sub
Public ReadOnly Property getmanagedStructure() As
Emgu.CV.Structure.managedStructure
Get
Return
CType(Runtime.InteropServices.Marshal.PtrToStructure(Ptr,
GetType(Emgu.CV.Structure. managedStructure)),
Emgu.CV.Structure.managedStructure)
End Get
End Property
End Class
The code is as follows and it doesn’t need to be commented because I
think its clear.
Do you think it is ok or that it needs come improvement?
’ like c++ counterpart
Public Structure managedStructure
Public Integer1 As Integer
Public Pointer1 As IntPtr
' ...
End Structure
Class myWrapper
Implements IDisposable
Public Ptr As IntPtr
Public Sub New(ByVal dynamParams As Integer, ByVal measureParams As
Integer, ByVal controlParams As Integer)
Dispose()
‘ allocate and initialize unmanaged Resource
Ptr = Emgu.CV.CvInvoke.allocateResource(dynamParams,
measureParams, controlParams)
GC.AddMemoryPressure(Runtime.InteropServices.Marshal.SizeOf(GetType(Emgu.CV.Structure.managedStructure)))
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
If Ptr <> IntPtr.Zero Then
Emgu.CV.CvInvoke.ReleaseResource(Ptr)
GC.RemoveMemoryPressure(Runtime.InteropServices.Marshal.SizeOf(GetType(Emgu.CV.Structure.
managedStructure)))
GC.SuppressFinalize(Me)
Ptr = IntPtr.Zero
End If
End Sub
Protected Overrides Sub Finalize()
Try
Dispose()
Finally
MyBase.Finalize()
End Try
End Sub
Public ReadOnly Property getmanagedStructure() As
Emgu.CV.Structure.managedStructure
Get
Return
CType(Runtime.InteropServices.Marshal.PtrToStructure(Ptr,
GetType(Emgu.CV.Structure. managedStructure)),
Emgu.CV.Structure.managedStructure)
End Get
End Property
End Class
--
Giuseppe
Giuseppe