M
Mike
Threads and callbacks is one of the last things that needs to be
address in my .NET SDK development project.
After exploring delegate functions, I think my question pertains to
type casting unmanaged data passed back in a callback.
The native C/C++ callback prototype is:
DWORD CALLBACK DoCallback(
DWORD userdata,
const TChannelMessage *msg);
In VB, I have this:
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure TChannelMessage
Public Channel As Integer
Public SenderId As Integer
Public UserData As Short
Public DataSize As Short
<MarshalAs(UnmanagedType.ByValArray, sizeconst:=500)> _
Public Data() As Byte
End Structure
Delegate Function cbWildcatDelegate(_
ByVal userdata As Long, _
ByRef msg As TChannelMessage) as Long
Function PrepareCallback(_
ByVal cbproc As cbWildcatDelegate, _
ByVal userdata As Integer) As Boolean
' calls API to set callback address and userdata
SetupWildcatCallback(cbProc, userdata)
End Function
Function DoCallBack(_
ByVal userdata As Long, _
ByRef msg As TChannelMessage) as Long
console.Writeline("Got Milk?")
return 0
End function
The callback is done, but some there is corruption and exception which
our DLL traps.
It will worke when I changed the delegate function parameters to pointers:
Delegate Function cbWildcatDelegate(_
ByVal userdata As IntPtr, _
ByRef msg As IntPtr) as Long
Function DoCallBack(_
ByVal userdata As IntPtr, _
ByRef msg As IntPtr) as Long
console.Writeline("Got Milk?!")
return 0
End function
So the next step is to typecast the msg pointer to TChannelMessage and
also UserData. Normally, UserData is 'this' in C/C++ or I guess ME in
VB.NET?
SetupWildcatCallback(cbProc, ME) ???
In any case:
Do I use System.Runtime.InteropServices.Marshal here, if so, how?
or can I do this auto-magically with the delegate function declaration?
Also, this has been crossing my mind, is there such as thing as a PURE
non-managed type structure in .NET? I thought that is what
<StructLayout(LayoutKind.Sequential)> did for you? No?
Thanks
--
address in my .NET SDK development project.
After exploring delegate functions, I think my question pertains to
type casting unmanaged data passed back in a callback.
The native C/C++ callback prototype is:
DWORD CALLBACK DoCallback(
DWORD userdata,
const TChannelMessage *msg);
In VB, I have this:
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure TChannelMessage
Public Channel As Integer
Public SenderId As Integer
Public UserData As Short
Public DataSize As Short
<MarshalAs(UnmanagedType.ByValArray, sizeconst:=500)> _
Public Data() As Byte
End Structure
Delegate Function cbWildcatDelegate(_
ByVal userdata As Long, _
ByRef msg As TChannelMessage) as Long
Function PrepareCallback(_
ByVal cbproc As cbWildcatDelegate, _
ByVal userdata As Integer) As Boolean
' calls API to set callback address and userdata
SetupWildcatCallback(cbProc, userdata)
End Function
Function DoCallBack(_
ByVal userdata As Long, _
ByRef msg As TChannelMessage) as Long
console.Writeline("Got Milk?")
return 0
End function
The callback is done, but some there is corruption and exception which
our DLL traps.
It will worke when I changed the delegate function parameters to pointers:
Delegate Function cbWildcatDelegate(_
ByVal userdata As IntPtr, _
ByRef msg As IntPtr) as Long
Function DoCallBack(_
ByVal userdata As IntPtr, _
ByRef msg As IntPtr) as Long
console.Writeline("Got Milk?!")
return 0
End function
So the next step is to typecast the msg pointer to TChannelMessage and
also UserData. Normally, UserData is 'this' in C/C++ or I guess ME in
VB.NET?
SetupWildcatCallback(cbProc, ME) ???
In any case:
Do I use System.Runtime.InteropServices.Marshal here, if so, how?
or can I do this auto-magically with the delegate function declaration?
Also, this has been crossing my mind, is there such as thing as a PURE
non-managed type structure in .NET? I thought that is what
<StructLayout(LayoutKind.Sequential)> did for you? No?
Thanks
--