Building a driver - windows

I have some issue in a driver & I have to troubleshoot it, before that I need to build it. I have no experience in driver.
I have the source code & bat file. It might be using WDK 7(I'm not sure)
Can anyone guide me in building the driver project with below bat file.
Ofcourse I can understand bat file is setting environment variables & calling build.exe
My main questions are:
1. Which WDK i need to install, so that I can specify its path in bat file
2. How to run bat file, do i need to run in visual studio command prompt or how?
if you give some Some general idea on building a driver for a beginner that would be much appreciated. Thanks.
[if you need any info, which i can provide please feel free to ask]
bat fie:
rem #echo off
rem --------------------------------------------------------------------
rem SafeBoot Windows NT 32 bit driver build script
rem --------------------------------------------------------------------
set MC_ENV=fre
if "%1%"=="debug" goto dbg
if "%2%"=="debug" goto dbg
if "%3%"=="debug" goto dbg
goto nodbg
:dbg
echo **** DEBUG BUILD ****
set MC_ENV=chk
:nodbg
rem --------------------------------------------------------------------
rem We want the absolute path of this directory, so we use a little utility
rem that creates a batch file that sets this into a environment variable.
rem --------------------------------------------------------------------
..\Translations\Bin\setcd -d DRV_DIR > thisdir.bat
call thisdir
del thisdir.bat
if NOT "%DDK64%"=="" goto x1
set DDK64=D:\Tools\WinDDK\6000
rem set DDK64=C:\WinDDK\6001.18001
:x1
set MSTOOLS=%MSSdk%
set BASEDIR=
set DDKBUILDENV=
set NTDBGFILES=
set DDK_INC_PATH=
set DDK_LIB_DEST=
set DDK_LIB_PATH=
set CRT_INC_PATH=
set CRT_LIB_PATH=
set BUILD_ALT_DIR=
call %DDK64%\bin\setenv %DDK64% %MC_ENV% i386
echo %DRV_DIR%
cd %DRV_DIR%
if NOT "%1"=="/a" goto build
del /q objfre_wlh_x86\i386\*.*
del /q objchk_wlh_x86\i386\*.*
rmdir /Q /S driver32
md driver32
:build
rem set MSC_OPTIMIZATION=/Od
echo Building started...
%DDK64%\bin\x86\build.exe -f -z -E
echo Building completed...
if ERRORLEVEL 1 goto error
rem copy objfre_wlh_x86\i386\McPvDrv.sys driver32\McPvDrv.sys
if "%MC_ENV%"=="chk" (
md ..\..\..\build\Win32\Debug
copy objchk_wlh_x86\i386\McPvDrv.sys ..\..\..\build\Win32\Debug\McPvDrv.sys
echo chk

If you want to build a driver for Windows 7 or newer use Visual Studio 2015 which is integrated with the lastest WDK 10. Building drivers is way easier this way.
If you want to build drivers for Windows XP then you will have to use WDK 7 and use a scripts similar like yours. Wdk 7 has no integration with visual studio. You have to specify source file in a special file called sources. See example drivers from Wdk 7. You can find a good cmd for building such drivers https://www.osronline.com/article.cfm?article=43 This website has the best driver dev tips
If you do not need XP support I highly recommend you go the much much easier way using Visual Studio 2015, windows sdk and windows 10 wdk.
LE:
You can also use Visual Studio 2013 which supports integration with WDK 8 and WDK 8.1 but not with WDK 10.

From your .bat file, it has:
if NOT "%DDK64%"=="" goto x1
set DDK64=D:\Tools\WinDDK\6000
rem set DDK64=C:\WinDDK\6001.18001
:x1
So, it might be looking for that version. If so, here is a page: What is the Microsoft name for WinDDK version 6001.18001?
Also, see https://en.wikipedia.org/wiki/Windows_Driver_Kit Your .bat wants, by default, build 6000 (released: november 29, 2006 for Windows Vista)
You could also try to use the latest one. Here's the link for WDK 10 with download instructions. WDK 7 should be similar: https://msdn.microsoft.com/en-us/library/windows/hardware/ff557573%28v=vs.85%29.aspx so find the right page. This page also has links to other pages that may help answer your other questions.
In any case, you'll need to modify the .bat to point to the correct WinDDK directory by setting the DDK64 variable to point to it.
You can probably install multiple different versions to different directories, but my guess is that the latest will let you build backward compatible versions. So, if you're building for Win7 (e.g.), get the latest DDK for Win7 (see the table in the wiki page).

Related

How can I perform different actions in windows batch file based on most recent version of installed program

Some of our computers run multiple versions of Microsoft Access (97 & 2010) while others run 365. For the pcs running multiple versions, the default is set to 97. I have a batch file that is performing various tests to see if files exist, and finished by running an Access 2010 database called MWO.accdb. See below.
if exist c:\windows\system32\mscomct2.ocx goto step2
rem copy mscomct2 and register
cscript \\file\apps\Database\Maintenance\365\MsgBox.vbs "Preparing necessary libraries."
copy "\\file\apps\Database\Maintenance\365\mscomct2.ocx" "c:\windows\system32\"
regsvr32 /u mscomct2.ocx
regsvr32 /i mscomct2.ocx
:step2
if exist "%USERPROFILE%\Desktop\MWO.lnk" goto step3
rem create shortcut on user's desktop for future use
cscript \\file\apps\Database\Maintenance\365\MsgBox.vbs "Creating shortcut on desktop & adding to start menu."
copy "\\file\apps\Database\Maintenance\MWO-INSTALL.lnk" "%USERPROFILE%\Desktop\MWO.lnk"
mkdir "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Maintenance"
copy "\\file\apps\Database\Maintenance\MWO-INSTALL.lnk" "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Maintenance\MWO.lnk"
:step3
\\file\apps\Database\Maintenance\365\MWO.accdb
Is there an easy way to test for the latest version of access, and force the file to open with it to avoid the defaulting to 97 problem?
wmic product where caption="Access" get caption,version
(I don't have access installed, the captionstring may differ)

Do Specific Job For Specific System By Batch "CMD" File

I need your help, I'm building small installation batch file for my small application (My app.exe must be copied in different places of windows version and architecture).
I played with it, but can't solve the given task...
I created this code: (For testing purposes)
If Exist "C:\Users\All Users\ntuser.dat" Goto Win 7
If Exist "C:\Documents and Settings\All Users\ntuser.dat" Goto Win XP
:Win 7
C:\w7\test.txt
:Win XP
C:\wxp\test.txt
But when executing the batch file (from windows xp) both jobs are done (Both files are opened)... why I can't understand... as I know it must open only win xp section code
Please how to modify the code, give me an example..
My Mission is:
I want that my app.exe file copied in different locations of different windows versions and builds (WinXP - X86-X64, Win7 - X86-X64)
If it is windows 7x64 x86 or win xp-x86,x64 it must only use the code which is set for the given system and not other
Thank You In Advanced
try this:
If Exist "C:\Users\All Users\ntuser.dat" Goto Win_7
If Exist "C:\Documents and Settings\All Users\ntuser.dat" Goto Win_XP
:Win_7
C:\w7\test.txt
goto:eof
:Win_XP
C:\wxp\test.txt
goto:eof
You need to tell it to stop running when it reaches the end of that section, or it will keep going into the next one. (Spaces in the branch labels aren't really a good idea either, although they're allowed. Why use them, when the user will never see them?)
If Exist "C:\Users\All Users\ntuser.dat" Goto Win7
If Exist "C:\Documents and Settings\All Users\ntuser.dat" Goto WinXP
:Win7
C:\w7\test.txt
goto :eof
:WinXP
C:\wxp\test.txt
goto :eof
:eof is a pre-defined branch label that means end of file. More info in the goto documentation

Find out windows version from non-privileged user command line

I need a way to find out what version of windows I'm running in using simple command line tools (no powershell). I need it to work from a non-privileged user, and I need to be able to parse out the difference between Windows XP, Vista, server 2008, and 7. I'm currently using:
wmic os get Caption but that fails when the user doesn't have permissions to run wmic.
Update:
To clarify, I need this command to not break with different service pack levels, etc. which probably rules out parsing a specific version number. Also if you look at this list of windows versions, you'll see that the numbers reported on Windows 7 and server 2008 r2 are the same.
I solved this problem by parsing the output of:
reg query "HKLM\Software\Microsoft\Windows NT\CurrentVersion" /v "ProductName"
systeminfo command shows everything about the os version including service pack number and the edition you are using.
C:\>systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
OS Name: Microsoft Windows 7 Enterprise
OS Version: 6.1.7601 Service Pack 1 Build 7601
Reference: Find Windows version from command prompt
You can use ver. I'm on a school computer with a non-privileged command prompt, and it gives me Microsft Windows [Version 6.1.7601]. I'm sure you'd be able to sort out Vista and XP from the number you get.
cmd displays the Windows version when started:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Joey>_
This is also a similar line as the one ver spits out, indeed.
One option then might be
echo exit|cmd|findstr Windows
another
cmd /c ver
depending on whether you have a pipeline or not.
if not CMDEXTVERSION 2 (
echo Error: This batch requires Command Extensions version 2 or higher
exit /b 1
)
FOR /F "usebackq tokens=4 delims=] " %%I IN (`ver`) DO for /F "tokens=1,2 delims=." %%J IN ("%%I") do set WindowsVersion=%%J.%%K
if "%WindowsVersion%" LSS "6.1" (
echo Error: This batch requires Windows 7 SP1 or higher
exit /b 1
)
You can get the SysInternals and install onto your C:\ directory. After that you can then go to a command prompt and use the command PSINFO.
It is great because it lets me query any PC on the network (that I have access to). At the command prompt you type: PSINFO \exactnameofcomputer
(PSINFO whack whack exactnameofcomputer)
Then hit enter. It will take a moment or two to report back, depending on where that computer is located at.

how to replace locked dll (was inuse)

I have a dll loaded into LSASS. I need to replace it on reboot.
There is an old utility called inuse from win2k reskit. It does not seem to work anymore (I am on windows 7)
Anybody know the right way to do it now?
Movefile from Sysinternals should do the trick :)
http://technet.microsoft.com/en-us/sysinternals/bb897556
Copying system dlls on Windows 7 (and Vista) is described in System File Checker tool article (in "Step2: If the System File Checker tool cannot repair a file").
Here is a quick (almost copy&paste) solution:
REM you will need to change this :-)
SET tmpset_SOURCE=c:\Windows\SysWOW64\d3d10_1.dll
SET tmpset_DEST=c:\_tmp\SysWOW64\d3d10_1.dll
REM copy and paste this straight to cmd:
takeown /f %tmpset_DEST%
icacls %tmpset_DEST% /GRANT %USERNAME%:F
copy %tmpset_SOURCE% %tmpset_DEST%

Installing a .inf file using a windows batch file

When you right click on a .inf file you have an option to "Install". I want to install a .inf file from the command line using a batch file. What is the "right" way to do this?
Thanks!
[edit]
I should clarify that I am trying to run this on Windows XP (and not Vista). Though I appriciate (and up-voted) the below answer mentioning InfDefaultInstall.exe, I believe that program was not shipped with XP.
You can find the command when looking at the HKCR\inffile\shell\Install\command registry key. On Windows XP this is
%SystemRoot%\System32\rundll32.exe setupapi,InstallHinfSection DefaultInstall 132 %1
on Windows Vista and later this would be
%SystemRoot%\System32\InfDefaultInstall.exe "%1"
To use the batch file across several Windows versions you would need some trickery. You could use reg.exe to query for the key and try parsing the output (I didn't find a quick way of getting only the value from reg). If you know what platforms you're running on you could also hard-code the command lines and switch according to the Windows version (which would need another hack to find that out. %OS% doesn't tell you more than "Windows NT", unfortunately.).
rem tested/works
:inf
ver | findstr /il "Version 6." > nul
if %ERRORLEVEL%==0 goto :vista
:xp
start/wait rundll32.exe setupapi,InstallHinfSection DefaultInstall 4 %_%
goto :eof
:vista
%SystemRoot%\System32\InfDefaultInstall.exe "%_%"
:eof
Should works on any Windows system that has IE 4.0+:
RunDll32 advpack.dll,LaunchINFSection <file.inf>,DefaultInstall

Resources