Callback function not working?? Video capture.
-
Hi. I can't seem to get my¨callback function to work... plz help. This code will throw an error:
Public Function myCallBack(ByVal lwnd As Long, ByVal lpVHdr As Long) As Boolean Dim VideoData() As Byte Dim gh As GCHandle = GCHandle.Alloc(lpVHdr, GCHandleType.Pinned) Dim AddrOflpVHdr As IntPtr = gh.AddrOfPinnedObject() ' **The error come's here.** Dim VideoHeader As VIDEOHDR = CType(Marshal.PtrToStructure(AddrOflpVHdr, _ GetType(VIDEOHDR)), VIDEOHDR) gh.Free() End Function
The error is: An unhandled exception of type 'System.NullReferenceException' occurred in Unknown Module. Additional information: Object reference not set to an instance of an object. -
Hi. I can't seem to get my¨callback function to work... plz help. This code will throw an error:
Public Function myCallBack(ByVal lwnd As Long, ByVal lpVHdr As Long) As Boolean Dim VideoData() As Byte Dim gh As GCHandle = GCHandle.Alloc(lpVHdr, GCHandleType.Pinned) Dim AddrOflpVHdr As IntPtr = gh.AddrOfPinnedObject() ' **The error come's here.** Dim VideoHeader As VIDEOHDR = CType(Marshal.PtrToStructure(AddrOflpVHdr, _ GetType(VIDEOHDR)), VIDEOHDR) gh.Free() End Function
The error is: An unhandled exception of type 'System.NullReferenceException' occurred in Unknown Module. Additional information: Object reference not set to an instance of an object.Marshal.PtrToStructure(ptr,structure) is a Shared Sub and doesn't return a value. Try this revised code. I can't test it live but this is what i suspect is wrong with your code.
Public Function myCallBack(ByVal lwnd As Long, ByVal lpVHdr As Long) As Boolean
Dim VideoData() As Byte
Dim gh As Runtime.InteropServices.GCHandle _
= Runtime.InteropServices.GCHandle.Alloc _
(lpVHdr, Runtime.InteropServices.GCHandleType.Pinned)Dim AddrOflpVHdr As IntPtr = gh.AddrOfPinnedObject() Dim VideoHeader As New VIDEOHDR Marshal.PtrToStructure(AddrOflpVHdr, VideoHeader) gh.Free() End Function
-
Marshal.PtrToStructure(ptr,structure) is a Shared Sub and doesn't return a value. Try this revised code. I can't test it live but this is what i suspect is wrong with your code.
Public Function myCallBack(ByVal lwnd As Long, ByVal lpVHdr As Long) As Boolean
Dim VideoData() As Byte
Dim gh As Runtime.InteropServices.GCHandle _
= Runtime.InteropServices.GCHandle.Alloc _
(lpVHdr, Runtime.InteropServices.GCHandleType.Pinned)Dim AddrOflpVHdr As IntPtr = gh.AddrOfPinnedObject() Dim VideoHeader As New VIDEOHDR Marshal.PtrToStructure(AddrOflpVHdr, VideoHeader) gh.Free() End Function
This error appeares when it comes to:
Marshal.PtrToStructure(AddrOflpVHdr, VideoHeader)
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: The structure must not be a value class. The structure look like this:Structure VIDEOHDR Dim lpData As Integer '// address of video buffer Dim dwBufferLength As Integer '// size, in bytes, of the Data buffer Dim dwBytesUsed As Integer '// see below Dim dwTimeCaptured As Integer '// see below Dim dwUser As Integer '// user-specific data Dim dwFlags As Integer '// see below Dim dwReserved() As Integer '// reserved; do not use} 'UPGRADE_TODO: "Initialize" must be called to initialize instances of this structure. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1026"' Public Sub Initialize() ReDim dwReserved(3) End Sub End Structure
-
This error appeares when it comes to:
Marshal.PtrToStructure(AddrOflpVHdr, VideoHeader)
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: The structure must not be a value class. The structure look like this:Structure VIDEOHDR Dim lpData As Integer '// address of video buffer Dim dwBufferLength As Integer '// size, in bytes, of the Data buffer Dim dwBytesUsed As Integer '// see below Dim dwTimeCaptured As Integer '// see below Dim dwUser As Integer '// user-specific data Dim dwFlags As Integer '// see below Dim dwReserved() As Integer '// reserved; do not use} 'UPGRADE_TODO: "Initialize" must be called to initialize instances of this structure. Click for more: 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="vbup1026"' Public Sub Initialize() ReDim dwReserved(3) End Sub End Structure
I found a WebCam class here's a link "http://dotnetjunkies.com/WebLog/principal/articles/32662.aspx" it's in C# but you can either convert it to vb or compile it as a .dll and use it that way. This is the structure I pulled out of the class example in the link above.
_ Public Structure VIDEOHDR _ Public lpData As Integer _ Public dwBufferLength As Integer _ Public dwBytesUsed As Integer _ Public dwTimeCaptured As Integer _ Public dwUser As Integer _ Public dwFlags As Integer _ Public dwReserved() As Integer End Structure 'VIDEOHDR
-
I found a WebCam class here's a link "http://dotnetjunkies.com/WebLog/principal/articles/32662.aspx" it's in C# but you can either convert it to vb or compile it as a .dll and use it that way. This is the structure I pulled out of the class example in the link above.
_ Public Structure VIDEOHDR _ Public lpData As Integer _ Public dwBufferLength As Integer _ Public dwBytesUsed As Integer _ Public dwTimeCaptured As Integer _ Public dwUser As Integer _ Public dwFlags As Integer _ Public dwReserved() As Integer End Structure 'VIDEOHDR
I've found the problem: the garbage collector. :mad: Solved it by using
GC.KeepAlive()
Thanks for you help.