Winapi:创建网络设备

时间:2017-01-20 11:20:18

标签: c++ c winapi device-driver

这是我的代码:

#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 devs = INVALID_HANDLE_VALUE;

  char infPath[260] = {0};

  char deviceId[260] = {0};

  DWORD reboot = 0;

  HMODULE newDll = 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;

  hwid = "net";

  char *inf;

  inf = "C:\\Program Files (x86)\\Infs\\net.inf";

  strncpy(infPath, inf, sizeof(infPath)-1);

  infPath[sizeof(infPath)-1] = '\0';

  memset(hwIdList, 0, sizeof(hwIdList));

  strcpy(hwIdList, hwid);

  if (SetupDiGetINFClass(infPath, &classGUID,
                              ClassName, MAX_NAME, 0) == FALSE)
  {
    printf("SetupDiGetINFClass failed. %d\n", GetLastError());
    fflush(stdout);
  }

  devs = SetupDiCreateDeviceInfoList(&classGUID, 0);

  if (devs == INVALID_HANDLE_VALUE)
  {
    printf("devs == INVALID_HANDLE_VALUE.\n");
    fflush(stdout);
  }

  deviceData.cbSize = sizeof(SP_DEVINFO_DATA);

  if (devName == NULL || devName[0] == 0)
  {
    if (SetupDiCreateDeviceInfo(devs, hwid, &classGUID, NULL, 0,
                                     DICD_GENERATE_ID, &deviceData) == FALSE)
    {
      printf("SetupDiCreateDeviceInfo failed. %d\n", GetLastError());
      fflush(stdout);
    }
  }
  else
  {
    strcpy(deviceId, "Root\\");

    strcat(deviceId, ClassName);
    strcat(deviceId, "\\");
    strcat(deviceId, devName);

    if (SetupDiCreateDeviceInfo(devs, deviceId, &classGUID,
                                     NULL, 0, 0, &deviceData) == FALSE)
    {
      printf("SetupDiCreateDeviceInfo failed. %d\n", GetLastError());
      fflush(stdout);
    }
  }

  if (SetupDiSetDeviceRegistryProperty(devs, &deviceData,
                                            SPDRP_HARDWAREID, (BYTE *) hwIdList,
                                                (strlen(hwIdList) + 2)) == FALSE)
  {
    printf("SetupDiSetDeviceRegistryProperty failed. %d\n", GetLastError());
    fflush(stdout);
  }


  if (SetupDiCallClassInstaller(DIF_REGISTERDEVICE,
                                     devs, &deviceData) == FALSE)
  {
    printf("SetupDiCallClassInstaller failed. %d\n", GetLastError());
    fflush(stdout);
  }

  newDll = LoadLibrary("newdesv.dll");

  if (newDll == NULL)
  {
    printf("newDll == NULL.\n");
    fflush(stdout);
  }

  InstallSelectedDriver = (InstallSelectedDriverProto) GetProcAddress(newDll, "InstallSelectedDriver");

  if (InstallSelectedDriver == NULL)
  {
    printf("InstallSelectedDriver == NULL failed. %d\n", GetLastError());
    fflush(stdout);
  }

  if (SetupDiSetSelectedDevice(devs, &deviceData) == FALSE)
  {
    printf("SetupDiSetSelectedDevice failed. %d\n", GetLastError());
    fflush(stdout);
  }


  if (SetupDiBuildDriverInfoList(devs, &deviceData,
                                        SPDIT_COMPATDRIVER) == FALSE)
  {
    printf("SetupDiBuildDriverInfoList failed. %d\n", GetLastError());
    fflush(stdout);
  }


  if (SetupDiCallClassInstaller(DIF_SELECTBESTCOMPATDRV, devs,
                                       &deviceData) == FALSE)
  {
    printf("SetupDiBuildDriverInfoList failed. %d\n", GetLastError());
    fflush(stdout);
  }

  if (SetupDiGetSelectedDriver(devs, &deviceData,
                                      &deviceData) == FALSE)
  {
    printf("SetupDiGetSelectedDriver failed. %d\n", GetLastError());
    fflush(stdout);
  }

  if (InstallSelectedDriver(NULL, devs, NULL, TRUE, &reboot) == FALSE)
  {
    printf("InstallSelectedDriver failed. %d\n", GetLastError());
    fflush(stdout);
  }


  if (devName && devNameSize > 0)
  {
    if (SetupDiGetDeviceInstanceId(devs, &deviceData,
                                      devName, devNameSize, NULL) == FALSE)
    {
      printf("SetupDiGetDeviceInstanceId failed. %d\n", GetLastError());
      fflush(stdout);
    }
  }

  return 0;
}

我要做的是创建一个网络设备。我有Segmentation fault但不知道为什么。我无法粘贴整个.inf文件,因为它是大学项目,但显然有

[Version]
Signature = "$Windows NT$"
Class = Net
ClassGUID = {4d36e972-e325-11ce-bfc1-08002be10318}

我尝试了不同的东西并注意到SetupDiCreateDeviceInfo可能是问题,因为我试图删除命令及其下面的所有内容,在这种情况下一切正常。对不起,如果问题很愚蠢,我是winapi的新手。感谢。

P.S 关于SetupDiGetDeviceInstanceId的问题,正如我在评论中给出的Segmentation fault,我甚至试过这个版本:

char *devName1 = NULL;
if (SetupDiGetDeviceInstanceId(devs, &deviceData,
                                  devName1, devNameSize, NULL) == FALSE)
{
  printf("SetupDiGetDeviceInstanceId failed. %d\n", GetLastError());
  fflush(stdout);
}

创建了分离的变量并进行了初始化,但它给了我同样的错误。

0 个答案:

没有答案