使用 CreateFile 打开套接字
我们有一些旧的串行代码,只需打开然后关闭它即可检查串行端口是否可用。 现在我们正在向应用程序添加网络支持,我想通过提供字符串形式的 IP 地址来重用该函数。
/**
* So far I have tried:
* A passed in portPath normally looks like:
\\?\acpi#pnp0501#1#1#{GUID}
10.2.0.155:2001
//10.2.0.155:2001/
\\.\10.2.0.155:2001\
\\?\10.2.0.155:2001\
* all without success.
*/
bool PortIsAvailable( const CString& portPath )
{
HANDLE hCom = ::CreateFile( portPath,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, // not overlapped I/O
NULL ); // hTemplate must be NULL for comm devices
if (INVALID_HANDLE_VALUE != hCom )
{
::CloseHandle( hCom );
return true;
}
return false;
}
我知道我可以使用连接然后关闭,但我想以最小的更改重用该功能。 如果我能重用这个函数就更好了。 如果不是,那么我将不得不编写代码来确定它是否是套接字。
我想知道通过 CreateFile 打开套接字的正确方法是什么?
We've got some old serial code which checks whether a serial port is available simply by opening it and then closing it. Now we are adding network support to the app I want to reuse the function by supplying the ip address as a string.
/**
* So far I have tried:
* A passed in portPath normally looks like:
\\?\acpi#pnp0501#1#1#{GUID}
10.2.0.155:2001
//10.2.0.155:2001/
\\.\10.2.0.155:2001\
\\?\10.2.0.155:2001\
* all without success.
*/
bool PortIsAvailable( const CString& portPath )
{
HANDLE hCom = ::CreateFile( portPath,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, // not overlapped I/O
NULL ); // hTemplate must be NULL for comm devices
if (INVALID_HANDLE_VALUE != hCom )
{
::CloseHandle( hCom );
return true;
}
return false;
}
I know I could use connect followed by shutdown but I want to reuse the function with minimal changes. If I can reuse the function so much the better. If not then I will have to write code that determines whether it is a socket or not.
I was wondering what the correct way of opening a socket via CreateFile is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能通过 CreateFile 创建套接字。 为此,您应该使用 Windows 套接字 API目的。 要创建 SOCKET 句柄,请使用 WSASocket 。 请注意,此函数返回的 SOCKET 可以用作 Windows 句柄,其中包含一些 Windows 函数,例如 ReadFile 和 WriteFile。
You can not create a socket via CreateFile. You should use the windows socket API for this purpose. For creating the SOCKET handle, you use WSASocket. Note that the SOCKET returned by this function can be used as a Windows Handle with some Windows functions, such as ReadFile and WriteFile.
我不相信您可以使用 CreateFile() 操作套接字。 套接字是从 BSD (iirc) 导入的抽象,并以名称兼容的方式实现(最初通过
winsock.h
,当前通过winsock2.h
)。 我认为 MS 从未在CreateFile()
中添加对套接字的支持。更多基本原理:大多数(所有?)
CreateFile()
操作都会返回本机 Windows 句柄。 由于套接字是非本机抽象,因此它们在操作系统中没有本机句柄,因此CreateFile()
处理它们是没有意义的。I don't believe you can manipulate sockets with
CreateFile()
. Sockets are an abstraction imported from BSD (iirc) and implemented in a name-compatible way (originally viawinsock.h
, and currentlywinsock2.h
). I don't think MS ever added support for sockets toCreateFile()
.More rationale: Most (everything?)
CreateFile()
manipulates returns a native Windows handle. Because sockets are a non-native abstraction, they don't have a native handle in the OS, so it wouldn't make sense forCreateFile()
to handle them.