若你想让你的WIN32应用程序连接打开UDP或TCP 的设备对象,可以这样做,首先要使应用程序打开一个TDI控制通道给UDP或TCP,默认情况下没有到UDP或TCP的符号连接,所以在调用CreateFile()前要使用DefineDosDevice() 函数建立符号连接。
如下面程序:
#include <windows.h>
void main()
{
HANDLE handle = NULL;
BOOL success = NULL;
char deviceName[] = "TCP";
char targetPath[] = "//Device//TCP";
char completeDeviceName[] = ".//TCP";
//
// First create a symbolic link.
//
success = DefineDosDevice (DDD_RAW_TARGET_PATH,
deviceName,
targetPath
);
if (!success) {
MessageBox(NULL,"DefineDosDevice failed","Notice",MB_OK);
}
//
// Then call CreateFile().
//
handle = CreateFile(completeDeviceName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (handle == INVALID_HANDLE_VALUE) {
MessageBox(NULL,"CreateFile failed","Notice",MB_OK);
} else {
MessageBox(NULL,"CreateFile succeded","Notice",MB_OK);
CloseHandle(handle);
}
}
#include <windows.h>
void main()
{
HANDLE handle = NULL;
BOOL success = NULL;
char deviceName[] = "TCP";
char targetPath[] = "//Device//TCP";
char completeDeviceName[] = ".//TCP";
//
// First create a symbolic link.
//
success = DefineDosDevice (DDD_RAW_TARGET_PATH,
deviceName,
targetPath
);
if (!success) {
MessageBox(NULL,"DefineDosDevice failed","Notice",MB_OK);
}
//
// Then call CreateFile().
//
handle = CreateFile(completeDeviceName,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (handle == INVALID_HANDLE_VALUE) {
MessageBox(NULL,"CreateFile failed","Notice",MB_OK);
} else {
MessageBox(NULL,"CreateFile succeded","Notice",MB_OK);
CloseHandle(handle);
}
}