C Netsh不';t在特定接口上设置ip

C Netsh不';t在特定接口上设置ip,c,winapi,interface,cygwin,C,Winapi,Interface,Cygwin,下面是我创建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, L

下面是我创建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;

 }
#包括
#包括
#包括
#包括
#包括
#包括
typedef BOOL WINAPI(*安装了selecteddriverproto)(HWND、HDEVINFO、LPCTSTR、BOOL、PDWORD);
int main()
{
InstallSelectedDriver Proto InstallSelectedDriver;
静态常量int MAX_NAME=256;
HDEVINFO devices=无效的句柄值;
char infPath[260]={0};
char deviceId[260]={0};
DWORD=0;
hmoduledll=NULL;
int devNameSize=260;
GUID classGUID={0};
char ClassName[MAX_NAME]={0};
char hwIdList[LINE_LEN+4]={0};
SP_设备信息_数据设备数据={0};
SP_DRVINFO_数据驱动器rinfodata={sizeof(SP_DRVINFO_数据)};
常量字符*hwid;
char*devName=NULL;
char deviceName[260];
hwid=“tsu”;
char*inf;
inf=“C:\\ProgramFiles(x86)\\TSU\\TSU.inf”;
strncpy(infPath,inf,sizeof(infPath)-1);
infPath[sizeof(infPath)-1]='\0';
memset(hwIdList,0,sizeof(hwIdList));
strcpy(hwIdList,hwid);
德沃德结式;
if(setupDigetInfo类)(infPath和类GUID,
类名称,最大名称,0)=FALSE)
{
printf(“SetupDigetInfo类失败。%d\n”,GetLastError());
fflush(stdout);
返回1;
}
设备=SetupDiCreateDeviceInfo列表(&classGUID,0);
如果(设备==无效的\u句柄\u值)
{
printf(“设备==无效的句柄值。\n”);
fflush(stdout);
返回1;
}
deviceData.cbSize=sizeof(SP_DEVINFO_数据);
if(devName==NULL | | devName[0]==0)
{
如果(SetupDiCreateDeviceInfo)(设备、hwid和类GUID,“yverichita”,0,
DICD_生成_ID和设备数据)==FALSE)
{
printf(“SetupDiCreateDeviceInfo失败。%d\n”,GetLastError());
fflush(stdout);
返回1;
}
}
其他的
{
strcpy(设备ID,“根\\”);
strcat(设备ID,类名);
strcat(设备ID,“\\”);
strcat(设备ID、设备名称);
如果(SetupDiCreateDeviceInfo)(设备、设备ID和类GUID,
“yverichita”、0、0和deviceData)=FALSE)
{
printf(“SetupDiCreateDeviceInfo失败。%d\n”,GetLastError());
fflush(stdout);
返回1;
}
}
如果(设置设备和设备数据),
SPDRP_硬件ID,(字节*)hwIdList,
(strlen(hwIdList)+2))==FALSE)
{
printf(“SetupDiSetDeviceRegistryProperty失败。%d\n”,GetLastError());
fflush(stdout);
返回1;
}
如果(设置DiCallClassInstaller(DIF_注册设备,
设备和设备数据==错误)
{
printf(“SetupDiCallClassInstaller失败。%d\n”,GetLastError());
fflush(stdout);
返回1;
}
AddDriverPackage(infPath);
Dll=LoadLibrary(“Newdev.Dll”);
如果(Dll==NULL)
{
printf(“Dll==NULL%d\n”,GetLastError());
fflush(stdout);
返回1;
}
InstallSelectedDriver=(InstallSelectedDriverProto)GetProcAddress(Dll,“InstallSelectedDriver”);
如果(InstallSelectedDriver==NULL)
{
printf(“InstallSelectedDriver==NULL失败。%d\n”,GetLastError());
fflush(stdout);
返回1;
}
if(SetupDiSetSelectedDevice(设备和设备数据)=FALSE)
{
printf(“SetupDiSetSelectedDevice失败。%d\n”,GetLastError());
fflush(stdout);
返回1;
}
如果(SetupDiBuildDriverFolist)(设备和设备数据,
SPDIT_(驱动程序)==错误)
{
printf(“SetupDiBuildDriverFolist失败。%d\n”,GetLastError());
fflush(stdout);
返回1;
}
如果(设置DiCallClassInstaller)(DIF_选择BestCompatDRV,设备,
&deviceData)==假)
{
printf(“SetupDiBuildDriverFolist失败。%d\n”,GetLastError());
fflush(stdout);
返回1;
}
如果(设置数字)选择了驱动(设备和设备数据),
&driverInfoData)==FALSE)
{
printf(“SetupDiGetSelectedDriver失败。%d\n”,GetLastError());
fflush(stdout);
返回1;
}
if(InstallSelectedDriver(NULL、设备、NULL、TRUE和重新启动)=FALSE)
{
printf(“InstallSelectedDriver失败。%d\n”,GetLastError());
fflush(stdout);
返回1;
}
如果(SetupDiGetDeviceInstanceId)(设备和设备数据,
deviceName,devNameSize,NULL)==FALSE)
{
printf(“SetupDiGetDeviceInstanceId失败。%d\n”,GetLastError());
fflush(stdout);
返回1;
}
printf(“SetupDiGetDeviceInstanceId:使用的名称:%s\n”,deviceName);
fflush(stdout);
返回0;
}
然后我使用netsh更改它的名称,然后设置它的ip。我使用以下命令:

netsh.exe接口集interface name=“Local Area Connection 69”newname=“Network#1”


这个命令运行时没有问题,我也从GUI中进行了检查,清楚地创建了界面,并且正确地更改了名称。然后我运行:

netsh接口ip set address name=“Network#1”静态192.168.1.92 255.255.255.252无
这给了我错误的回答:

配置dhcp服务失败。接口可能已断开。系统找不到指定的文件。



经过一些调试后,我注意到如果我选择name
Network#5
,例如一切正常,name设置正确,ip设置正常。我搜索了可能的原因,认为问题可能出在netsh中,所以我尝试了解决它,但不幸的是,它不起作用,我甚至在vmware上安装了新的windows,并尝试在那里运行此代码,一切正常,显然“有些东西没有删除”,这导致了一些问题<我在用Windows,Cygwin。希望你能帮忙。谢谢

不确定这是否是netsh错误的原因,但IP地址192.168.1.92/30是网络地址,而不是可分配的IP地址。您是否尝试过使用IP地址192.168.1.93或