- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想将 message.WParam 转换为 Socket。
protected override void WndProc(ref Message m)
{
if (m.Msg == Values.MESSAGE_ASYNC)
{
switch (m.LParam.ToInt32())
{
case Values.FD_READ:
WS2.Receive(m.WParam);
case Values.FD_WRITE: break;
default: break;
}
}
else
{
base.WndProc(ref m);
}
}
public class WS2
{
public static void Receive(IntPtr sock)
{
Socket socket = sock;
}
}
如何将 IntrPtr(sock) 转换为 Socket 以便调用 Receive()?
最佳答案
你不能这样做,因为 Socket 类创建并管理它自己的私有(private)套接字句柄。理论上,您可以使用一些邪恶的反射将您的套接字句柄插入套接字的私有(private)字段,但那完全是一种黑客攻击,我不会这样做。
给定一个有效的套接字句柄,您可以通过 P/Invoke 调用 Win32 recv 函数来接收数据,如下所示:
[DllImport("ws2_32.dll")]
extern static int recv([In] IntPtr socketHandle, [In] IntPtr buffer,
[In] int count, [In] SocketFlags socketFlags);
/// <summary>
/// Receives data from a socket.
/// </summary>
/// <param name="socketHandle">The socket handle.</param>
/// <param name="buffer">The buffer to receive.</param>
/// <param name="offset">The offset within the buffer.</param>
/// <param name="size">The number of bytes to receive.</param>
/// <param name="socketFlags">The socket flags.</param>
/// <exception cref="ArgumentException">If <paramref name="socketHandle"/>
/// is zero.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="buffer"/>
/// is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">If the
/// <paramref name="offset"/> and <paramref name="size"/> specify a range
/// outside the given buffer.</exception>
public static int Receive(IntPtr socketHandle, byte[] buffer, int offset,
int size, SocketFlags socketFlags)
{
if (socketHandle == IntPtr.Zero)
throw new ArgumentException("socket");
if (buffer == null)
throw new ArgumentNullException("buffer");
if (offset < 0 || offset >= buffer.Length)
throw new ArgumentOutOfRangeException("offset");
if (size < 0 || offset + size > buffer.Length)
throw new ArgumentOutOfRangeException("size");
unsafe
{
fixed (byte* pData = buffer)
{
return Recv(socketHandle, new IntPtr(pData + offset),
size, socketFlags);
}
}
}
关于c# - 如何将 IntPtr/Int 转换为 Socket?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/765364/
我正在尝试使用 BitmapData 类?并且在将 IntPtr 值分配给 BitmapData.Scan0 属性时遇到一些问题。这是我的代码: var data = bmp.LockBits(new
我正在尝试使用 BitmapData 类?并且在将 IntPtr 值分配给 BitmapData.Scan0 属性时遇到一些问题。这是我的代码: var data = bmp.LockBits(new
我有两个 IntPtr 值指向一些 length 字节的数据区域。 length 可能有 200k 到 400k 的数量级。 int length = /* ..*/ IntPtr ptrSrc =
我在网上到处都看过,似乎找不到能够解决该问题的特定答案。从字面上看,这是我得到的唯一错误,只有在解决此问题后才能构建可执行文件。 int num3 = new VAMemory(Data.proces
我不知道 Copy(IntPtr[], Int32, IntPtr, Int32) 方法是如何工作的。我虽然它可以将多个 IntPtr 中包含的数据复制到一个 IntPtr 中(如 MSDN 所述),
我有一个类的以下代码。这是一个类的初始化。 第三方动态链接库 [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatib
我刚刚了解到 IntPtr.Zero 无法与 default(IntPtr) 相提并论。谁能告诉我为什么? IntPtr.Zero == new IntPtr(0) -> "could not eva
这两种说法有什么区别吗: IntPtr myPtr = new IntPtr(0); IntPtr myPtr2 = IntPtr.Zero; 我见过许多使用 PInvoke 的示例,如果 myPtr
这个问题已经有答案了: What makes more sense - char* string or char *string? [duplicate] (13 个回答) 已关闭 9 年前。 本能地
我知道 Marshal.SizeOf() 和 sizeof() 有多么根本的不同。但是在 IntPtr 的情况下,无论 CPU 架构如何,它们都不会总是返回完全相同的东西吗? 最佳答案 一方面,有原始
我需要实现一个与找到的解决方案非常相似的解决方案 here ,写于 2009 年,用于检索文件的唯一标识符。具体来说,我正在查看答案的 ApproachB()函数和以下行(重写了一点以反射(refle
是否可以通过 Skype 用户窗口获取处理程序并使用 SendMessage(whdl,BM_CLICK,intptr.zero,intrptr.zero,intptr.zero) 单击发送文件或调用
经过一些研究我翻译了 pHandle = IntPtr.Zero; 到德尔福 var pHandle: Pointer; 当我在 Delphi 中中断调试时,我得到 $314328,当我在 C s
运营商的description on MSDN有一句话: An exception is only thrown if the value of value requires more bits th
这个函数在一个循环中。当我运行程序时,带有 IntPtr 的行给我内存问题,我已经删除 [],但它仍然没有解决内存问题,有人可以帮忙吗?谢谢 void showImage(IplImage *img,
直到最近,我还认为在 P/Invoke 中编码 IntPtr 只会涉及“blitting”操作;即简单地从例如复制指针C++ 到 C#。 然而,我最近与某人交谈时提到,事情远不止于此。他特别提到 In
我目前正在编写一些代码,这些代码反射(reflect)了从对 native dll 的调用中编码返回的结构。一些结构包含指向以 null 结尾的指针数组的 IntPtr* 字段。这些字段需要特殊处理。
我正在尝试在 Ubuntu 14.04 上使用 Mono 执行函数 P/Invoke: [DllImport(@"libRT", CharSet = CharSet.Ansi, CallingConv
我有一个类定义如下: [StructLayout(LayoutKind.Sequential)] public class OUR_MEM_STR {
我遇到了一段C#源码如下 int* ptr = ...; int w = ...; int* ptr3 = ptr + (IntPtr)w; CS0019: Operator '+' cannot
我是一名优秀的程序员,十分优秀!