1

我有一个用 C# 编写的 dll,该 dll 用于套接字客户端我遇到的问题是 Xojo 没有“out”方法,所以:

public static int Clientx(String strData, int portadd, string ipadd,out string  response)

不起作用,如果我删除“out”它会起作用,但我没有得到回应

Xojo 中是否有替代方案,或者我需要对 dll 做些什么来解决问题?如果我不使用“out”编译,这是有效的代码

Declare Function Clientx lib "C:\Users\nige\Desktop\realprojects\ClassLibrary1.dll" (strData as CString,  portadd As integer,  ipadd as CString, response as CString ) AS CString
Dim  ipadd as String= "127.0.0.1"
Dim portadd As Int32
Dim  response as String
portadd =6300
Call Clientx(strData, portadd, ipadd,  response)
Main.response.text=(response)

客户代码

 using System;
 using System.Runtime.InteropServices;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Net;
 using System.Net.Sockets;
 using RGiesecke.DllExport;

namespace ClassLibrary1
{
public class Class1
{
    [DllExport]
    //string response;
    //String strData ;

    public static int Clientx(String strData, int portadd, string ipadd, out string response)
    {
        // this is pretty much untouched apart from a few messages and added text boxes for the ip and port
        // string ipadd = "localhost";
        //int portadd = Int32.Parse("6300");

        //String response; 
        IPAddress[] ipAddress = Dns.GetHostAddresses(ipadd);
        IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], portadd);
        try
        {
            Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);



            clientSock.Connect(ipEnd);


            Console.WriteLine(strData);


            int iLen = strData.Length;
            Console.WriteLine(iLen);


            int iConvertedLength = Encoding.UTF8.GetByteCount(strData);
            Console.WriteLine(iConvertedLength);


            byte[] bDataFromString = Encoding.UTF8.GetBytes(strData);


            byte[] bBufToSend = new byte[501];


            Array.Copy(bDataFromString, 0, bBufToSend, 8, iConvertedLength);


            for (int i = 0; i < 26; i++)
                Console.Write("{0:X2} ", bBufToSend[i]);
            Console.WriteLine();


            byte[] bLength = BitConverter.GetBytes(iConvertedLength);


            Array.Copy(bLength, 0, bBufToSend, 0, 4);

            // Debug:
            for (int i = 0; i < 26; i++)
                Console.Write("{0:X2} ", bBufToSend[i]);
            Console.WriteLine();


            int iZero = 0;
            byte[] bZero = BitConverter.GetBytes(iZero);

            Array.Copy(bZero, 0, bBufToSend, 4, 4);


            for (int i = 0; i < 26; i++)
                Console.Write("{0:X2} ", bBufToSend[i]);
            Console.WriteLine();

            // Send.  Remember we've grown the converted data by 8 bytes
            clientSock.Send(bBufToSend, 0, iConvertedLength + 8, SocketFlags.None);

            Console.WriteLine("Sent command executed");

            // Get response

            byte[] serverData = new byte[1024];
            // We need to know how many bytes are received so we can safely read starting
            // From the 8th byte (so we know how many bytes will be left to read by GetString below)
            int iBytesReceived = clientSock.Receive(serverData);

            // Debug:
            Console.WriteLine("Raw bytes received");
            for (int i = 0; i < iBytesReceived; i++)
                Console.Write("{0:X2} ", serverData[i]);
            Console.WriteLine();
            Console.Write(Encoding.UTF8.GetString(serverData, 8, iBytesReceived - 8));
            Console.WriteLine();
            //response.Text
            response = Encoding.UTF8.GetString(serverData, 8, iBytesReceived - 8);
            clientSock.Shutdown(SocketShutdown.Both);


            clientSock.Close();
            Console.Write(response);
            Console.ReadLine();
        }

        catch (System.Net.Sockets.SocketException)
        {
            //timer1.Enabled = false;
            response = "Can Not Connect to " + ipEnd;

        }

        return 1;
    }

}

}

我希望这是有道理的

4

0 回答 0