I have written the following C program as a replacement for the older DevPath.exe, which was written using AutoIT and VB.. but the source code doesn't exist.
This new DevPath.exe functions the same way as the old AutoIt/VisualBasic version of DevPath. It scans the supplied path for *.inf files and automatically sets the registry key value HKLM\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\DevicePath to the discovered list of driverpack folders that contain *.inf files.
The new version has a much larger string buffer size for handling the navigation of a large number of driverpack folders with corresponding *.inf files.
The same command is used to run it:
I compiled the following source code using "Microsoft Visual C++ 2010 Express" (The Express edition is free). I could post the project file used for building it.
I initially encountered a problem with DevPath.exe that required the existence of MSVCR100.DLL on the windows computer. On a clean install of Windows, this MSVCR100.DLL does not exist. This problem was fixed by changing the project properties to statically linking the MFC runtime and also statically link AdvAPI32.lib, which is part of the Windows SDK v7.1 (Downloaded and installed the x86 libraries). I added the one line to the code for #pragma comment(lib,"AdvAPI32.Lib")
Code was enhanced on 03/11/11 to handle upper and lower case filename extension (e.g. .inf or .INF or .Inf)
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <tchar.h>
#include <strsafe.h>
#pragma comment(lib, "AdvAPI32.Lib")
//------------------------------------------------------------------------------------------------------------------
// Globals...
//------------------------------------------------------------------------------------------------------------------
WIN32_FIND_DATA *fd;
#define DEVPATH_REGISTRY_VALUE_BUFFER 100000
#define DEVPATH_REGISTRY_KEY "SOFTWARE\\Microsoft\\Windows\\CurrentVersion"
#define DEVPATH_REGISTRY_VALUE "DevicePath"
//------------------------------------------------------------------------------------------------------------------
// Protos...
//------------------------------------------------------------------------------------------------------------------
int navigatepath(const char *path, char *registryvalue, BOOL recursive);
int fixpath(const char *inpath, char *outpath);
//------------------------------------------------------------------------------------------------------------------
// navigatepath()
//------------------------------------------------------------------------------------------------------------------
int navigatepath(const char *_path, char *registryvalue, BOOL recursive)
{
HANDLE fh;
int filecnt=0;
int filenamelen;
char path[MAX_PATH];
char tmppath[MAX_PATH];
fd = malloc(sizeof(WIN32_FIND_DATA));
fixpath(_path,path);
strcat_s(path,MAX_PATH,"*");
fh = FindFirstFile((LPCSTR) path,fd);
if (fh != INVALID_HANDLE_VALUE)
{
do
{
if (!(fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
filenamelen = strlen( fd->cFileName );
if (filenamelen >= 4)
{
if (fd->cFileName[filenamelen - 4] == '.' && (fd->cFileName[filenamelen - 3] == 'I' || fd->cFileName[filenamelen - 3] == 'i') && (fd->cFileName[filenamelen - 2] == 'N' || fd->cFileName[filenamelen - 2] == 'n') && (fd->cFileName[filenamelen - 1] == 'F' || fd->cFileName[filenamelen - 1] == 'f'))
{
filecnt++;
break;
}
}
}
}
while(FindNextFile(fh,fd));
}
if (filecnt)
{
strcat_s(registryvalue,DEVPATH_REGISTRY_VALUE_BUFFER,";");
strcat_s(registryvalue,DEVPATH_REGISTRY_VALUE_BUFFER,_path);
}
fh = FindFirstFile((LPCSTR) path,fd);
if (fh != INVALID_HANDLE_VALUE)
{
do
{
if (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if((0 != strcmp(fd->cFileName,".")) && (0 != strcmp(fd->cFileName,"..")))
{
fixpath(_path,tmppath);
strcat_s(tmppath,MAX_PATH,fd->cFileName);
if(recursive)
navigatepath(tmppath, registryvalue, recursive);
}
}
}
while(FindNextFile(fh,fd));
}
FindClose(fh);
return 0;
}
//------------------------------------------------------------------------------------------------------------------
// fixpath() - Adds \ to the end of a path if not present.
//------------------------------------------------------------------------------------------------------------------
int fixpath(const char *inpath, char *outpath)
{
int n=0;
strcpy_s(outpath,MAX_PATH,inpath);
while(inpath[n]) n++;
if(inpath[n-1] != '\\')
{
strcat_s(outpath,MAX_PATH,"\\");
return 1;
}
return 0;
}
//------------------------------------------------------------------------------------------------------------------
// Entry point...
//------------------------------------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
int recursive=1;
char registryvalue[DEVPATH_REGISTRY_VALUE_BUFFER];
DWORD dwCategoryNum = 1;
HKEY hk;
DWORD dwDisp;
registryvalue[0] = 0;
strcpy_s(registryvalue, DEVPATH_REGISTRY_VALUE_BUFFER, "%SystemRoot%\\Inf");
if (argc == 1)
{
printf("Devpath.exe [drive:][path]\n\n");
printf("DevPath.exe will scan [drive:][path] and all subfolders for the existence\n");
printf("of *.inf file(s). It will set the registry key to the list of subfolders\n");
printf("that contain *.inf file(s).\n");
printf("HKLM %s REG_EXPAND_SZ %s\n",DEVPATH_REGISTRY_KEY,DEVPATH_REGISTRY_VALUE);
}
if (argc == 2)
{
navigatepath(argv[1], registryvalue, recursive);
//printf("%s\n",registryvalue);
//printf("string lenght of registryvalue = %d\n",strlen(registryvalue) );
if ( RegCreateKeyEx( HKEY_LOCAL_MACHINE, TEXT(DEVPATH_REGISTRY_KEY), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hk, &dwDisp))
{
printf("Could not create the registry key\n");
return 0;
}
if ( RegSetValueEx( hk, TEXT(DEVPATH_REGISTRY_VALUE), 0, REG_EXPAND_SZ, (LPBYTE) registryvalue, (DWORD) (lstrlen(registryvalue)+1)*sizeof(TCHAR)))
{
printf("Could not set the registry value for \"DevicePath\"\n");
}
}
return 0;
}
Last edited by zookeeper (2011-03-12 06:23:39)