我想在 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/