Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从C调用WSASend#_C#_Marshalling_Winsock - Fatal编程技术网

C# 从C调用WSASend#

C# 从C调用WSASend#,c#,marshalling,winsock,C#,Marshalling,Winsock,这是我的代码,但我没有成功: Unmanaged.WsaBuf buffer = Unmanaged.WsaBuf.Create("Blah Blah"); IntPtr bufferPointer = Marshal.AllocHGlobal(Marshal.SizeOf(buffer)); Marshal.StructureToPtr(buffer, bufferPointer, true); IntPtr o; SocketError error = Unmanaged.WSA

这是我的代码,但我没有成功:

Unmanaged.WsaBuf buffer =
    Unmanaged.WsaBuf.Create("Blah Blah");

IntPtr bufferPointer = Marshal.AllocHGlobal(Marshal.SizeOf(buffer));
Marshal.StructureToPtr(buffer, bufferPointer, true);

IntPtr o;
SocketError error = Unmanaged.WSA_Send(
    socket,
    bufferPointer,
    1,
    out o,
    SocketFlags.None,
    IntPtr.Zero,
    IntPtr.Zero);
下面是方法声明:

    [DllImport("Ws2_32.dll", SetLastError = true, EntryPoint = "WSASend")]
    public static extern SocketError WSA_Send(IntPtr socket, IntPtr buffer, int len, out IntPtr numberOfBytesSent, SocketFlags flags, IntPtr overlapped, IntPtr completionRoutine);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WsaBuf
{
    public ulong Len;

    public IntPtr Buf;

    public static WsaBuf Create(string str)
    {
        byte[] connectBuf = Encoding.ASCII.GetBytes(str);
        GCHandle pinnedArray = GCHandle.Alloc(connectBuf, GCHandleType.Pinned);
        return new WsaBuf { Buf = pinnedArray.AddrOfPinnedObject(), Len = (ulong)connectBuf.Length };
    }

    public void Free()
    {
        if (!Buf.Equals(IntPtr.Zero))
        {
            GCHandle.FromIntPtr(Buf).Free();
        }
    }
}
这是我的WSABUF宣言:

    [DllImport("Ws2_32.dll", SetLastError = true, EntryPoint = "WSASend")]
    public static extern SocketError WSA_Send(IntPtr socket, IntPtr buffer, int len, out IntPtr numberOfBytesSent, SocketFlags flags, IntPtr overlapped, IntPtr completionRoutine);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WsaBuf
{
    public ulong Len;

    public IntPtr Buf;

    public static WsaBuf Create(string str)
    {
        byte[] connectBuf = Encoding.ASCII.GetBytes(str);
        GCHandle pinnedArray = GCHandle.Alloc(connectBuf, GCHandleType.Pinned);
        return new WsaBuf { Buf = pinnedArray.AddrOfPinnedObject(), Len = (ulong)connectBuf.Length };
    }

    public void Free()
    {
        if (!Buf.Equals(IntPtr.Zero))
        {
            GCHandle.FromIntPtr(Buf).Free();
        }
    }
}

我在另一边什么也没收到。有什么想法吗?

u_long
unsigned long
,它映射到
uint
,因为它在Microsoft Visual C编译器上是一个4字节的量
Len
应该是
uint
。可能,
WSASend
只查看了前4个字节,发现它们是
0
,这是一个零长度的缓冲区


顺便说一句,您误用了
GCHandle.FromIntPtr
。看看这些文档,看看它到底有什么作用。这个问题无关紧要,所以我不会详细说明。

当.NET公开TcpClient,甚至是
Socket
类时,为什么要使用WinSock?@johnsaunds这是一个挂钩的情况,但MSDN很有用,还需要解决其他问题,但这似乎是解决发送问题的方法。