Conversion from VB6 to VB.NET

M

Mike M

Hi,

My conversion seems from VB6 to VB.Net of an interface to an API seem to
have gone OK, but when I look at the output from the Debug.Print("Active
server is " & xmlServerActive) statement, xmlServerActive only contains one
character(the first one). I suspect it is being truncated. I am pretty much a
novice at this sort of thing so any advice would be welcome.

Here is the old VB6 code:
Listening for Servers and System Status using VB
‘ Declare the functions
Public Declare Function RCA_listenForServersBstr Lib "clientapi.dll" _
(ByVal serverActiveCallback As Long, _
ByVal serverShutdownCallback As Long) As Integer
Public Declare Function RCA_listenForSystemStatusBstr Lib "clientapi.dll" _
(ByVal systemStatusCallback As Long, _
ByVal clusterCreatedCallback As Long, _
ByVal clusterDeletedCallback As Long, _
ByVal systemExceptionCallback As Long) As Integer
‘ Declare callback functions
Public Function serverActiveCallback(ByVal xmlServerActive As String) As Long
Debug.Print "Active server is " & xmlServerActive
End Function
Public Function serverShutdownCallback(ByVal server As String) As Long
Debug.Print "Shutdown server is " & server
End Function
Public Function systemExceptionCallback(ByVal error As String) As Long
MsgBox "Error : " & error
End Function
Public Function systemStatusCallback(ByVal systemStatus As String) As Long
Debug.Print "System status is " & systemStatus
End Function
Public Function clusterCreatedCallback(ByVal cluster As String) As Long
Debug.Print "Created cluster is " & cluster
End Function
Public Function clusterDeletedCallback(ByVal cluster As String) As Long
Debug.Print "Deleted cluster is " & cluster
End Function
‘ Set up to listen for servers
ret = RCA_listenForServersBstr(AddressOf serverActiveCallback, _
AddressOf serverShutdownCallback)
‘ Set up to listen for system status
ret = RCA_listenForSystemStatusBstr(AddressOf systemStatusCallback, _
AddressOf clusterCreatedCallback, _
AddressOf clusterDeletedCallback, _
AddressOf systemExceptionCallback)


The new VB.NET (It has some other pieces I have added)
Option Strict Off
Option Explicit On
Module RimagesAPI

Public Structure RCA_production_order_description
<VBFixedString(128),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=128)> Public orderId() As Char ' This field cannot be NULL
<VBFixedString(128),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=128)> Public clientId() As Char ' This field is optional and can
be NULL
<VBFixedString(128),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=128)> Public originator() As Char ' This field is optional and can
be NULL
<VBFixedString(128),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=128)> Public targetCluster() As Char ' This field cannot be NULL
<VBFixedString(128),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=128)> Public targetServer() As Char ' This field is optional, if
null then any
<VBFixedString(32),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=32)> Public priority() As Char ' Default for this field is
PRIORITY_MEDIUM
<VBFixedString(32),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=32)> Public action() As Char ' Default for this field is
ACTION_RECORD
<VBFixedString(32),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=32)> Public mediaType() As Char ' Default for this field is MT_CDR
<VBFixedString(32),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=32)> Public mediaSize() As Char ' Default for this field is MS_120
<VBFixedString(32),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=32)> Public targetLine() As Char ' Default for this field is TL_ANY
<VBFixedString(32),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=32)> Public copies() As Char ' Default for this field is 1
<VBFixedString(32),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=32)> Public inOutInputBin() As Char ' Default for this field is
IOB_ANY
<VBFixedString(32),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
SizeConst:=32)> Public outputMailslot() As Char ' Default for this field is 0
End Structure

Public Declare Function RCA_connect Lib "clientapi.dll" (ByVal clientId As
String) As Short
Public Declare Function RCA_connectEx Lib "clientapi.dll" (ByVal clientId
As String, ByVal host As String, ByVal port As String) As Short
Public Declare Function RCA_listenForServersBstr Lib "clientapi.dll"
(ByVal serverActiveCallback As serverActiveCallbackDelegate, ByVal
serverShutdownCallback As serverShutdownCallbackDelegate) As Short
Public Declare Function RCA_listenForSystemStatusBstr Lib
"clientapi.dll" (ByVal systemStatusCallback As systemStatusCallbackDelegate,
ByVal clusterCreatedCallback As clusterCreatedCallbackDelegate, ByVal
clusterDeletedCallback As clusterDeletedCallbackDelegate, ByVal
systemExceptionCallback As systemExceptionCallbackDelegate) As Short
Public Declare Function RCA_submitProductionOrderBstr Lib
"clientapi.dll" (ByRef orderDesc As RCA_production_order_description, ByVal
xmlOrder As String, ByVal orderStatusCallback As orderStatusCallbackDelegate)
As Short
Public Declare Function RCA_cancelProductionOrder Lib "clientapi.dll"
(ByRef orderDesc As RCA_production_order_description, ByVal abortCurrent As
Boolean) As Short
Public Declare Function RCA_stopListeningForProductionOrder Lib
"clientapi.dll" (ByRef orderDesc As RCA_production_order_description) As Short


Delegate Function serverActiveCallbackDelegate(ByVal xmlServerActive As
String) As Integer
Public Function serverActiveCallback(ByVal xmlServerActive As String) As
Integer
Debug.Print("Active server is " & xmlServerActive)
End Function
Delegate Function serverShutdownCallbackDelegate(ByVal server As String)
As Integer
Public Function serverShutdownCallback(ByVal server As String) As Integer
Debug.Print("Shutdown server is " & server)
End Function
Delegate Function systemExceptionCallbackDelegate(ByVal wError As
String) As Integer
Public Function systemExceptionCallback(ByVal wError As String) As Integer
MsgBox("Error : " & wError)
End Function
Delegate Function systemStatusCallbackDelegate(ByVal systemStatus As
String) As Integer
Public Function systemStatusCallback(ByVal systemStatus As String) As
Integer
Debug.Print("System status is " & systemStatus)
End Function
Delegate Function clusterCreatedCallbackDelegate(ByVal cluster As
String) As Integer
Public Function clusterCreatedCallback(ByVal cluster As String) As Integer
Debug.Print("Created cluster is " & cluster)
End Function
Delegate Function clusterDeletedCallbackDelegate(ByVal cluster As
String) As Integer
Public Function clusterDeletedCallback(ByVal cluster As String) As Integer
Debug.Print("Deleted cluster is " & cluster)
End Function
Delegate Function orderStatusCallbackDelegate(ByVal orderStatus As
String) As Integer
Public Function orderStatusCallback(ByVal orderStatus As String) As
Integer
On Error Resume Next
Debug.Print(orderStatus)
orderStatusCallback = 0
End Function
 
T

Tom Shelton

Mike M said:
Hi,

My conversion seems from VB6 to VB.Net of an interface to an API seem to
have gone OK, but when I look at the output from the Debug.Print("Active
server is " & xmlServerActive) statement, xmlServerActive only contains
one
character(the first one). I suspect it is being truncated. I am pretty
much a
novice at this sort of thing so any advice would be welcome.

I did a google search, and it sure looks like this api your referencing -
actually has a .NET version already, so you might want to check into that.
Never the less:
Here is the old VB6 code:
Listening for Servers and System Status using VB
‘ Declare the functions
Public Declare Function RCA_listenForServersBstr Lib "clientapi.dll" _
(ByVal serverActiveCallback As Long, _
ByVal serverShutdownCallback As Long) As Integer
Public Declare Function RCA_listenForSystemStatusBstr Lib "clientapi.dll"
_
(ByVal systemStatusCallback As Long, _
ByVal clusterCreatedCallback As Long, _
ByVal clusterDeletedCallback As Long, _
ByVal systemExceptionCallback As Long) As Integer
‘ Declare callback functions
Public Function serverActiveCallback(ByVal xmlServerActive As String) As
Long
Debug.Print "Active server is " & xmlServerActive
End Function
Public Function serverShutdownCallback(ByVal server As String) As Long
Debug.Print "Shutdown server is " & server
End Function
Public Function systemExceptionCallback(ByVal error As String) As Long
MsgBox "Error : " & error
End Function
Public Function systemStatusCallback(ByVal systemStatus As String) As Long
Debug.Print "System status is " & systemStatus
End Function
Public Function clusterCreatedCallback(ByVal cluster As String) As Long
Debug.Print "Created cluster is " & cluster
End Function
Public Function clusterDeletedCallback(ByVal cluster As String) As Long
Debug.Print "Deleted cluster is " & cluster
End Function
‘ Set up to listen for servers
ret = RCA_listenForServersBstr(AddressOf serverActiveCallback, _
AddressOf serverShutdownCallback)
‘ Set up to listen for system status
ret = RCA_listenForSystemStatusBstr(AddressOf systemStatusCallback, _
AddressOf clusterCreatedCallback, _
AddressOf clusterDeletedCallback, _
AddressOf systemExceptionCallback)


The new VB.NET (It has some other pieces I have added)
Option Strict Off

I would turn Option Strict On - pretty much always. There are few cases,
such as late binding where it is useful to have it turned off, but I would
not make it the general case.

Option Strict On

Imports System.Runtime.InteropServices ' save your self some typeing :)
Option Explicit On
Module RimagesAPI

Unless your planning to use this structure with VB.NET file IO - I would get
rid of the VBFixedString attribute. Without the documentation and seeing
the VB6 declaration, I would probably change the char arrays below to just
plain old strings. I'm not sure what char set this is using, but I'll
assume ansi:

<StructLayout (LayoutKind.Sequential, CharSet:=CharSet.Ansi) > _
Public Structure RCA_production_order_description
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
Public orderId As String ' This field cannot be NULL

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)>
Public clientId As String ' This field is optional and can be NULL
....
End Structure


Public Declare Ansi Function RCA_connect Lib "clientapi.dll" (ByVal clientId
As String) As Short

Anyway, I am making some assumptions here that may or may not be correct,
depending on this particular API - so I can't guarentee you that this is
going to work :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top