这是我创建网络接口(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)设置接口(interface) name="本地连接 69"newname="网络#1"
该命令运行没有问题,我也从 GUI 进行了检查,界面已清晰创建,名称已正确更改。然后我运行:
netsh 接口(interface) ip set 地址名称=“网络#1”静态 192.168.1.92 255.255.255.252 无
这给我返回错误:
配置 dhcp 服务失败。接口(interface)可能已断开。系统找不到指定的文件。
经过一些调试后,我注意到如果我选择名称 Network #5
例如,一切正常,名称设置正确并且设置 ip 工作正常。我搜索了可能的原因,并认为问题可能出在 netsh 中,所以我尝试 reset它,但不幸的是它不起作用,我什至在 vmware 上安装了新的 Windows 并尝试在那里运行此代码,一切正常,显然“有些东西留下了”未删除,它导致了一些问题。
我使用的是Windows,Cygwin。希望你能帮忙。谢谢。
最佳答案
不确定这是否是 netsh 错误的原因,但 IP 地址 192.168.1.92/30 是一个网络地址,而不是可分配的 IP 地址。您是否尝试过使用IP地址192.168.1.93或192.168.1.94?
关于c - Netsh 不在特定接口(interface)上设置 ip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43655081/