visual-c++ - ConnectEx 在哪里定义?

标签 visual-c++ windows-7 asynchronous connect

我想在 Windows7 上使用 ConnectEx 功能,使用 MSVC2010。

我收到错误 C3861: 'ConnectEx': identifier not found

MSDN 建议该函数应该在 mswsock.h 中声明,但是,在检查它时,它没有在那里定义。

有小费吗?

最佳答案

如果您进一步阅读 the MSDN article for ConnectEx() 你提到过,它说:

Note The function pointer for the ConnectEx function must be obtained at run time by making a call to the WSAIoctl function with the SIO_GET_EXTENSION_FUNCTION_POINTER opcode specified. The input buffer passed to the WSAIoctl function must contain WSAID_CONNECTEX, a globally unique identifier (GUID) whose value identifies the ConnectEx extension function. On success, the output returned by the WSAIoctl function contains a pointer to the ConnectEx function. The WSAID_CONNECTEX GUID is defined in the Mswsock.h header file.



与其他 Windows API 函数不同,ConnectEx()必须在运行时加载,因为头文件实际上不包含 ConnectEx() 的函数声明(它确实有一个 typedef 用于名为 LPFN_CONNECTEX 的函数)并且文档没有特别提到您必须链接到的特定库才能使其工作(其他Windows API 函数通常就是这种情况) .

这是一个如何让它工作的例子(为了说明省略了错误检查):
#include <Winsock2.h> // Must be included before Mswsock.h
#include <Mswsock.h>

// Required if you haven't specified this library for the linker yet
#pragma comment(lib, "Ws2_32.lib")

/* ... */

SOCKET s = /* ... */;
DWORD numBytes = 0;
GUID guid = WSAID_CONNECTEX;
LPFN_CONNECTEX ConnectExPtr = NULL;
int success = ::WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
    (void*)&guid, sizeof(guid), (void*)&ConnectExPtr, sizeof(ConnectExPtr),
    &numBytes, NULL, NULL);
// Check WSAGetLastError()!

/* ... */

// Assuming the pointer isn't NULL, you can call it with the correct parameters.
ConnectExPtr(s, name, namelen, lpSendBuffer,
    dwSendDataLength, lpdwBytesSent, lpOverlapped);

关于visual-c++ - ConnectEx 在哪里定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10967516/

相关文章:

php - wp-async-task 不触发 run_action 方法

c# - Windows Phone 8.1 从远程 URL 占位符加载图像

c - 如何防止 Visual C++ 链接器包含地址被占用的每个函数?

c++ - 如何在 win32 C++ 中创建自定义形状控件

windows - Play 2 框架停止服务器

c# - 缓存网络流

c++ - 使用右值编写 operator+ 的正确方法

c++ - 使/欺骗 Visual C++ 正确地缩进宏结构

mysql - 如何在命令行中使用执行mysqldiff实用程序?

javascript - Node : Passing function specific variables to Async. 并行()