📄 setupdriverdlg.cpp
字号:
// SetupDriverDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SetupDriver.h"
#include "SetupDriverDlg.h"
#include <setupapi.h>
#include <newdev.h>
#include <dshow.h>
#include <rpc.h>
#include <strsafe.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
typedef
BOOL
(CALLBACK* PINSTALLSELECTEDDRIVER)(
HWND hwndParent,
HDEVINFO DeviceInfoSet,
LPCTSTR Reserved,
BOOL Backup,
PBOOL pReboot
);
/////////////////////////////////////////////////////////////////////////////
// CSetupDriverDlg dialog
CSetupDriverDlg::CSetupDriverDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSetupDriverDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSetupDriverDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_chInstanceID = NULL;
m_chHardwareID = NULL;
}
void CSetupDriverDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSetupDriverDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSetupDriverDlg, CDialog)
//{{AFX_MSG_MAP(CSetupDriverDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSetupDriverDlg message handlers
BOOL CSetupDriverDlg::GetInstanceID(const CString& strInID)
{
CString strTemp;
strTemp = strInID.Right(strInID.GetLength() - strInID.Find("usb"));
strTemp.Replace("#", "\\");
strTemp = strTemp.Left(strTemp.Find("{") -1);
m_chInstanceID = new char[strTemp.GetLength()+1];
if (FAILED(StringCchCopy(m_chInstanceID, strTemp.GetLength()+1, strTemp))) {
DWORD Err = ERROR_INSUFFICIENT_BUFFER;
}
return TRUE;
}
BOOL CSetupDriverDlg::SetUpDriver()
{
DWORD Err = NO_ERROR;
HDEVINFO DeviceInfoSet = INVALID_HANDLE_VALUE;
SP_DEVINFO_DATA DeviceInfoData;
SP_DEVINSTALL_PARAMS DeviceInstallParams;
SP_DRVINFO_DATA DriverInfoData;
// GUID guid;
// char *GUIDString = NULL;
// GUIDString = "6bdd1fc6-810f-11d0-bec7-08002be2092f";
// UuidFromString((unsigned char *)GUIDString, &guid);
DeviceInfoSet = SetupDiCreateDeviceInfoList(NULL, NULL);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
Err = GetLastError();
return FALSE;
}
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
if (!SetupDiOpenDeviceInfo(DeviceInfoSet,
m_chInstanceID,
NULL,
0,
&DeviceInfoData))
{
Err = GetLastError();
return FALSE;
}
if (!SetupDiSetSelectedDevice(DeviceInfoSet,
&DeviceInfoData)) {
Err = GetLastError();
return FALSE;
}
DeviceInstallParams.cbSize = sizeof(DeviceInstallParams);
if (!SetupDiGetDeviceInstallParams(DeviceInfoSet,
&DeviceInfoData,
&DeviceInstallParams))
{
Err = GetLastError();
return FALSE;
}
DeviceInstallParams.FlagsEx |= DI_FLAGSEX_ALLOWEXCLUDEDDRVS;
if (!SetupDiSetDeviceInstallParams(DeviceInfoSet,
&DeviceInfoData,
&DeviceInstallParams))
{
Err = GetLastError();
return FALSE;
}
if (!SetupDiBuildDriverInfoList(DeviceInfoSet,
&DeviceInfoData,
SPDIT_COMPATDRIVER))
{
Err = GetLastError();
return FALSE;
}
if (!SetupDiCallClassInstaller(DIF_SELECTDEVICE,
DeviceInfoSet,
&DeviceInfoData))
{
Err = GetLastError();
return TRUE;
}
//
// Get the selected driver node.
// Note: If this list does not contain any drivers, this call
// will fail with ERROR_NO_DRIVER_SELECTED.
//
DriverInfoData.cbSize = sizeof(DriverInfoData);
if (!SetupDiGetSelectedDriver(DeviceInfoSet,
&DeviceInfoData,
&DriverInfoData))
{
Err = GetLastError();
return FALSE;
}
TCHAR NewDevPath[MAX_PATH];
HMODULE hNewDev = NULL;
PINSTALLSELECTEDDRIVER pInstallSelectedDriver = NULL;
if (GetSystemDirectory(NewDevPath, SIZECHARS(NewDevPath)) == 0) {
Err = GetLastError();
return FALSE;
}
if (FAILED(StringCchCat(NewDevPath, SIZECHARS(NewDevPath), TEXT("\\NEWDEV.DLL")))) {
Err = ERROR_INSUFFICIENT_BUFFER;
return FALSE;
}
hNewDev = LoadLibrary(NewDevPath);
if (!hNewDev)
{
Err = GetLastError();
return FALSE;
}
pInstallSelectedDriver = (PINSTALLSELECTEDDRIVER)GetProcAddress(hNewDev, "InstallSelectedDriver");
if (!pInstallSelectedDriver) {
Err = GetLastError();
return FALSE;
}
BOOL Reboot;
(*pInstallSelectedDriver)(NULL,
DeviceInfoSet,
NULL,
TRUE,
&Reboot);
if (Reboot & (DI_NEEDREBOOT | DI_NEEDRESTART))
{
//
// A reboot is required.
//
return FALSE;
}
if (hNewDev)
{
FreeLibrary(hNewDev);
hNewDev = NULL;
}
if (DeviceInfoSet != INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
DeviceInfoSet = INVALID_HANDLE_VALUE;
}
return TRUE;
}
int GetINFHardwareIDs(LPCSTR sInf, LPSTR aszHardwareIDs[], int num)
{
BOOL retVal = false;
int iIdx = 0;
UINT ErrorLine;
HINF hInf = SetupOpenInfFile("C:\\Program Files\\Common Files\\snp2std\\snp2std.inf", NULL, INF_STYLE_WIN4, &ErrorLine);
if(INVALID_HANDLE_VALUE == hInf) return retVal;
INFCONTEXT hInfContext = {0};
retVal = SetupFindFirstLine(hInf, TEXT("Manufacturer"),NULL, &hInfContext);
if (!retVal)
{
// SDKError err;
// _tprintf(err.GetErrMsg());
SetupCloseInfFile(hInf);
return 0;
}
LPSTR pReturnBuffer = NULL;
DWORD RequiredSize = 0;
SetupGetLineText(&hInfContext, NULL, NULL, NULL, NULL, RequiredSize, &RequiredSize);
if (RequiredSize > 0 )
{
pReturnBuffer = new CHAR[RequiredSize];
SetupGetLineText(&hInfContext, NULL, NULL, NULL, pReturnBuffer, RequiredSize, &RequiredSize);
retVal = SetupFindFirstLine(hInf, pReturnBuffer,NULL, &hInfContext);
delete[] pReturnBuffer;
if (!retVal)
{
// SDKError err;
// _tprintf(err.GetErrMsg());
}
else
{
do
{
DWORD RequiredSize = 0;
SetupGetLineText(&hInfContext, NULL, NULL, NULL, NULL, RequiredSize, &RequiredSize);
LPTSTR pReturnBuffer = new TCHAR[RequiredSize];
aszHardwareIDs[iIdx] = new TCHAR[RequiredSize]; //Need delete after used.
SetupGetLineText(&hInfContext, NULL, NULL, NULL, pReturnBuffer, RequiredSize, &RequiredSize);
char *ptr = strrchr(pReturnBuffer,',');
if (ptr == 0) continue;
StringCchCopy(aszHardwareIDs[iIdx++], strlen(ptr) ,ptr+1);
delete[] pReturnBuffer;
}while(SetupFindNextLine(&hInfContext, &hInfContext));
}
}
SetupCloseInfFile(hInf);
return iIdx; //不需要减1,是需要从1开始的计数的。
}
BOOL CSetupDriverDlg::OnInitDialog()
{
CDialog::OnInitDialog();
/////////// Main Process //////////////
if (!FindExistingDevice())
AfxMessageBox("Can't Find Device!");
if (!SetUpDriver())
AfxMessageBox("Install Device Fail!");
//InstallRootEnumeratedDriver(0);
//////////////////////////////////////
OnCancel();
return TRUE; // return TRUE unless you set the focus to a control
}
BOOL CSetupDriverDlg::SetupDiDriver()
{
return TRUE;
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
BOOL
CSetupDriverDlg::InstallRootEnumeratedDriver(
OUT PBOOL RebootRequired OPTIONAL
)
{
HDEVINFO DeviceInfoSet = 0;
SP_DEVINFO_DATA DeviceInfoData;
GUID ClassGUID;
TCHAR ClassName[MAX_PATH];
DWORD err;
//
// Use the INF File to extract the Class GUID.
//
// char *GUIDString = NULL;
//
// ZeroMemory(&ClassGUID, sizeof(GUID));
//
// GUIDString = "6bdd1fc6-810f-11d0-bec7-08002be2092f";
// UuidFromString((unsigned char *)GUIDString, &ClassGUID);
if (!SetupDiGetINFClass("F:\\temp\\2008.01.15-SonixUVC_BurnAP_v1.1.6.0.1(Release)\\Driver\\st50220.inf",&ClassGUID,ClassName,sizeof(ClassName),0))
{
/* return DisplayError(TEXT("GetINFClass"));*/
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -