[英]Netsh doesn't set ip on specific interface
这是我的代码,它创建一个网络接口
#include <windows.h>
#include <winerror.h>
#include <Winbase.h>
#include <Shlwapi.h>
#include <Setupapi.h>
#include <stdio.h>
typedef BOOL WINAPI (*InstallSelectedDriverProto)(HWND, HDEVINFO, LPCTSTR, BOOL, PDWORD);
int main()
{
InstallSelectedDriverProto InstallSelectedDriver;
static const int MAX_NAME = 256;
HDEVINFO devices = INVALID_HANDLE_VALUE;
char infPath[260] = {0};
char deviceId[260] = {0};
DWORD reboot = 0;
HMODULE Dll = NULL;
int devNameSize = 260;
GUID classGUID = {0};
char ClassName[MAX_NAME] = {0};
char hwIdList[LINE_LEN + 4] = {0};
SP_DEVINFO_DATA deviceData = {0};
SP_DRVINFO_DATA driverInfoData = {sizeof(SP_DRVINFO_DATA)};
const char *hwid;
char *devName = NULL;
char deviceName[260];
hwid = "tsu";
char *inf;
inf = "C:\\Program Files (x86)\\TSU\\tsu.inf";
strncpy(infPath, inf, sizeof(infPath)-1);
infPath[sizeof(infPath)-1] = '\0';
memset(hwIdList, 0, sizeof(hwIdList));
strcpy(hwIdList, hwid);
DWORD resultant;
if (SetupDiGetINFClass(infPath, &classGUID,
ClassName, MAX_NAME, 0) == FALSE)
{
printf("SetupDiGetINFClass failed. %d\n", GetLastError());
fflush(stdout);
return 1;
}
devices = SetupDiCreateDeviceInfoList(&classGUID, 0);
if (devices == INVALID_HANDLE_VALUE)
{
printf("devices == INVALID_HANDLE_VALUE.\n");
fflush(stdout);
return 1;
}
deviceData.cbSize = sizeof(SP_DEVINFO_DATA);
if (devName == NULL || devName[0] == 0)
{
if (SetupDiCreateDeviceInfo(devices, hwid, &classGUID, "yverichita", 0,
DICD_GENERATE_ID, &deviceData) == FALSE)
{
printf("SetupDiCreateDeviceInfo failed. %d\n", GetLastError());
fflush(stdout);
return 1;
}
}
else
{
strcpy(deviceId, "Root\\");
strcat(deviceId, ClassName);
strcat(deviceId, "\\");
strcat(deviceId, devName);
if (SetupDiCreateDeviceInfo(devices, deviceId, &classGUID,
"yverichita", 0, 0, &deviceData) == FALSE)
{
printf("SetupDiCreateDeviceInfo failed. %d\n", GetLastError());
fflush(stdout);
return 1;
}
}
if (SetupDiSetDeviceRegistryProperty(devices, &deviceData,
SPDRP_HARDWAREID, (BYTE *) hwIdList,
(strlen(hwIdList) + 2)) == FALSE)
{
printf("SetupDiSetDeviceRegistryProperty failed. %d\n", GetLastError());
fflush(stdout);
return 1;
}
if (SetupDiCallClassInstaller(DIF_REGISTERDEVICE,
devices, &deviceData) == FALSE)
{
printf("SetupDiCallClassInstaller failed. %d\n", GetLastError());
fflush(stdout);
return 1;
}
AddDriverPackage(infPath);
Dll = LoadLibrary("Newdev.dll");
if (Dll == NULL)
{
printf("Dll == NULL %d\n", GetLastError());
fflush(stdout);
return 1;
}
InstallSelectedDriver = (InstallSelectedDriverProto) GetProcAddress(Dll, "InstallSelectedDriver");
if (InstallSelectedDriver == NULL)
{
printf("InstallSelectedDriver == NULL failed. %d\n", GetLastError());
fflush(stdout);
return 1;
}
if (SetupDiSetSelectedDevice(devices, &deviceData) == FALSE)
{
printf("SetupDiSetSelectedDevice failed. %d\n", GetLastError());
fflush(stdout);
return 1;
}
if (SetupDiBuildDriverInfoList(devices, &deviceData,
SPDIT_COMPATDRIVER) == FALSE)
{
printf("SetupDiBuildDriverInfoList failed. %d\n", GetLastError());
fflush(stdout);
return 1;
}
if (SetupDiCallClassInstaller(DIF_SELECTBESTCOMPATDRV, devices,
&deviceData) == FALSE)
{
printf("SetupDiBuildDriverInfoList failed. %d\n", GetLastError());
fflush(stdout);
return 1;
}
if (SetupDiGetSelectedDriver(devices, &deviceData,
&driverInfoData) == FALSE)
{
printf("SetupDiGetSelectedDriver failed. %d\n", GetLastError());
fflush(stdout);
return 1;
}
if (InstallSelectedDriver(NULL, devices, NULL, TRUE, &reboot) == FALSE)
{
printf("InstallSelectedDriver failed. %d\n", GetLastError());
fflush(stdout);
return 1;
}
if (SetupDiGetDeviceInstanceId(devices, &deviceData,
deviceName, devNameSize, NULL) == FALSE)
{
printf("SetupDiGetDeviceInstanceId failed. %d\n", GetLastError());
fflush(stdout);
return 1;
}
printf("SetupDiGetDeviceInstanceId: Name used: %s\n", deviceName);
fflush(stdout);
return 0;
}
然后我使用netsh更改其名称,然后设置其ip。我使用以下命令:
netsh.exe interface set interface name="Local Area Connection 69" newname="Network #1"
此命令运行没有问题,我也从GUI中进行了检查,并且接口已明确创建并且名称已正确更改。 然后我运行:
netsh interface ip set address name="Network #1" static 192.168.1.92 255.255.255.252 none
这使我回到错误说:
failed to configure the dhcp service. The interface may be disconnected. System cannot find file speciified.
经过一些调试后,我注意到如果我选择名称为Network #5
的示例,则一切正常,名称设置正确,设置ip正常。 我搜寻了可能的原因,并认为问题可能出在netsh中,所以我尝试将其重置 ,但不幸的是,它没有用,我什至在vmware上安装了新的Windows并尝试在其中运行此代码,一切正常,这显然是“遗留了一些东西”并引起了一些问题。
我正在使用Windows,Cygwin。 希望能对您有所帮助。 谢谢。
不知道这是否是netsh错误的原因,但IP地址192.168.1.92 / 30是网络地址,而不是可分配的IP地址。 您是否尝试过使用IP地址192.168.1.93或192.168.1.94?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.