Getting Visual Basic 2010 to talk to FreeStyler
Posted: 22 Mar 2013, 11:22
Having looked at the various community solutions to control FreeStyler none of them meet my needs so I'm in the process of writing my own app which I intend to release to the community when it's finished.
The problem I'm having is that I'm using Visual Basic 2010 and the sample code for talking to FS is for VB6. As a result there's a couple of problems as VB 2010 doesn't support the VarPtr function. I've found a work around but it's still throwing an error.
Here's my version of the VB code from the PDF in the documentation folder with comments to show where I've made changes:
The error is here:
in the SendData function. VB is complaining that:
The problem is that the variable cdCopyData is defined as containing longs and sendMmessage expects this but cdCopyData.lpData contains an integer which comes from the incoming byte values.
This is all Martian to me and I've spent hours searching the internet to try and find a way of converting the output from the VarPtr function to a Long but nothing works. The Clng function doesn't fix the problem when it's applied as here:
Thanks in advance
The problem I'm having is that I'm using Visual Basic 2010 and the sample code for talking to FS is for VB6. As a result there's a couple of problems as VB 2010 doesn't support the VarPtr function. I've found a work around but it's still throwing an error.
Here's my version of the VB code from the PDF in the documentation folder with comments to show where I've made changes:
Code: Select all
Public Class frm_Showtime
' Declare function to talk to FreeStyler
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
' VB6 called this a type
Private Structure COPYDATASTRUCT
Public dwData As Long
Public cbData As Long
Public lpData As Long
End Structure
Private ThWnd As Long
Private Const WM_COPYDATA = &H4A
Private Const WM_USER = &H400
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Sub SendData(ByVal Functioncode As Byte, ByVal Length As Long, ByVal arrValues() As Byte)
Dim cdCopyData As COPYDATASTRUCT
Dim DMXvalues() As Byte
Dim teller As Byte
Dim n As Integer
Dim i As Integer
cdCopyData.dwData = Functioncode
cdCopyData.cbData = Length
' This line is the problem: cdCopyData is a structure of longs, bytes are fed into VarPtr
' which no longer exists in VB2010 so I've used a function I found on the internet as a
' substitute but it returns an integer
cdCopyData.lpData = VarPtr(arrValues(0))
ThWnd = FindWindow(vbNullString, "FS")
If ThWnd > 0 Then
i = SendMessage(ThWnd, WM_COPYDATA, Me.ThWnd, cdCopyData)
End If
End Sub
Public Function VarPtr(ByVal o As Object) As Integer
Dim GC As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(o, System.Runtime.InteropServices.GCHandleType.Pinned)
Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return ret
End Function
End Class
Code: Select all
i = SendMessage(ThWnd, WM_COPYDATA, Me.ThWnd, cdCopyData)
Now I'm a PHP programmer who last dabbled with VB back when version 1 came out (I interfaced it to a FORTRAN library - shows my age !).Value of type 'WindowsApplication1.frm_SHowtime.COPYDATASTRUCT' cannot be converted to 'Long'
The problem is that the variable cdCopyData is defined as containing longs and sendMmessage expects this but cdCopyData.lpData contains an integer which comes from the incoming byte values.
This is all Martian to me and I've spent hours searching the internet to try and find a way of converting the output from the VarPtr function to a Long but nothing works. The Clng function doesn't fix the problem when it's applied as here:
How do I get around this stumbling block ?Public Function VarPtr(ByVal o As Object) As Long
Dim GC As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(o, System.Runtime.InteropServices.GCHandleType.Pinned)
Dim ret As Long = CLng(GC.AddrOfPinnedObject.ToInt32)
GC.Free()
Return ret
End Function
Thanks in advance