我编写了一个C#dll,它使用TCP套接字将信息从客户端发送到服务器。我正在使用我的DLL中的StartClient函数,但遇到错误:标记为受限制的函数,或者该函数使用Visual Basic中不支持的自动化类型。
错误发生在returnValue = sabatheWise.StartClient(port,IpAddress,buffer)行,StartClient以黄色突出显示。我认为错误是由于我发现的一个参数而发生的。另外,即使我在我的dll中将port声明为int,当我在VBA中查看Object浏览器时,我看到端口被识别为long。如果我将端口设置为长或整数,它在我的VBA代码中没有区别,我收到相同的错误。
对此的任何指导将不胜感激。
这是VBA代码:
Sub TestProgram()
Dim port As Long
port = 15050
Dim IpAddress As String
IpAddress = "192.123.456.78"
Dim buffer() As Byte
'Dim BufferSize As Integer
'BufferSize = 16048
Dim sabatheWise As TCPMessage.AsynchronousClient
Set sabatheWise = New TCPMessage.AsynchronousClient
Dim returnValue As Long
returnValue = sabatheWise.StartClient(port, IpAddress, buffer)
If returnValue = 0 Then Debug.Print "The program was successful"
If returnValue = -1 Then Debug.Print "The program failed"
End Sub
这是dll的相关部分:
public int StartClient(int port, string IpAddress, byte[] buffer)
{
// Connect to a remote device.
try
{
IPAddress ipAddress = IPAddress.Parse(IpAddress);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
client.BeginSend(buffer, 0, buffer.Length, SocketFlags.None,
new AsyncCallback(SendCallback), client);
答案 0 :(得分:1)
该方法被标记为受限制,因为不支持byte[] buffer
。要使其工作,请通过引用定义要编组的缓冲区,并确保已分配缓冲区:
public int StartClient(int port, string IpAddress, ref byte[] buffer) {
...
}
Dim buffer(0 to 16048) As Byte
Dim client As New TCPMessage.AsynchronousClient
Dim returnValue As Long
returnValue = client.StartClient(3838, "127.0.0.1", buffer)