preinst.cpp

来自「使用SetupCopyOEMInf预安装驱动」· C++ 代码 · 共 133 行

CPP
133 行
字号
/*
#include <windows.h>
#include <stdio.h>

void main(int argc,char**argv)
{
// 第二个参数为安装程序的窗口名字
// 第三个参数为安装时弹出的没有找到数字签名的窗口名称
// 数字签名对话框上的控件ID
// [是] ID 5303;[否] ID 5306

   int iNotFoundTimes=0;
   HWND hWndSign=NULL;

   HWND hWndInstall = FindWindow(NULL,argv[1]);
   
   while(hWndInstall!=NULL)
   {
       Sleep(10);
       hWndSign = FindWindow(NULL,argv[2]);        
       if(hWndSign!=NULL)
           SendMessage(hWndSign,WM_COMMAND,5303,0);

       hWndInstall = FindWindow(NULL,argv[1]);
   }

   hWndSign = NULL;
   while(iNotFoundTimes <2000 )
   {
       Sleep(10);
       iNotFoundTimes++;
       hWndSign = FindWindow(NULL,argv[2]); 
       if(hWndSign!=NULL)
           SendMessage(hWndSign,WM_COMMAND,5303,0);
   }

}

*/

/*++

Copyright (c) Microsoft Corporation.  All rights reserved.

    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
    PURPOSE.

Module Name:

    preinst.cpp

Abstract:

    Demostrate usage of SetupCopyOEMInf(). 

Usage:

    1. Assume the driver pakcage includes a valid WHQL signature file (.cat)
    2. Place the demo .exe file together with the driver package (inf, cat, sys, etc.)
    3. Run the demo to pre-install the driver
    3. Plug in the device, and the driver will be loaded automatically

--*/

#include <windows.h>
#include <stdio.h>

#include <shlobj.h>   // for SHGetSpecialFolderPath(), SHCreateDirectoryEx()
#include <setupapi.h> // for SetupCopyOEMInf()

#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "setupapi.lib")

// ===============================================================

#define COMPANY "Goldbio"
#define PRODUCT "Foo Product"

char* inf = "dlc.inf";//"usbvm31b.Inf";
char* drivers[] = {
	inf,
	//"usbvm31b.cat",
	"usbscan.sys"
};

// ===============================================================
//int main1();

void main()
{
	//main1();

	BOOL bRet;

	char path[MAX_PATH];
	SHGetSpecialFolderPath(NULL, path, CSIDL_PROGRAM_FILES, FALSE);
	strcat(path, "\\" COMPANY "\\" PRODUCT);
	printf("pre-install %s\nto %s\n", inf, path);
	
	// CreateDirectory() fails with ERROR_PATH_NOT_FOUND if one or more intermediate directories do not exist;
	SHCreateDirectoryEx(NULL, path, NULL);

	for(int i = 0; i < sizeof(drivers) / sizeof(drivers[0]); i++)
	{
		char* file = drivers[i];

		char fullPath[MAX_PATH];
		strcpy(fullPath, path);
		strcat(fullPath, "\\");
		strcat(fullPath, file);

		bRet = CopyFile(file, fullPath, FALSE);
		if(!bRet)
		{
			printf("err 0x%x in CopyFile(%s)\n", GetLastError(), file);
			return;
		}
	}
	
	char infFullPath[MAX_PATH];
	strcpy(infFullPath, path);
	strcat(infFullPath, "\\");
	strcat(infFullPath, inf);

	bRet = SetupCopyOEMInf(infFullPath, path, SPOST_PATH, 0, NULL, 0, NULL, NULL);
	///*if(!bRet)
	//	printf("err 0x%x in SetupCopyOEMInf\n", GetLastError());*/
	//
	//printf("Done.\n");
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?