这是我的代码,它创建了一个netowork接口
#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.I&#39; m使用以下命令:
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中,所以我试着reset它,但不幸的是它没有工作,我甚至安装了新的窗口vmware并尝试在那里运行这些代码,一切正常,它清楚了#34;有些事情已经离开&#34;未删除并导致一些问题。我正在使用Windows,Cygwin。希望你能帮忙。谢谢。
答案 0 :(得分:0)
不确定这是否是netsh错误的原因,但IP地址192.168.1.92 / 30是网络地址而不是可分配的IP地址。您是否尝试过使用IP地址192.168.1.93或192.168.1.94?