中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-15 04:26
47,812 topics / 349,917 posts / today 0 new / 48,268 members
DOS开发编程 & 发展交流 (开发室) » How to write a program under DOS to detect whether a CD-ROM drive is installed?
Printable Version  7,782 / 45
Floor1 wqqqing Posted 2003-01-29 00:00
初级用户 Posts 7 Credits 119
How can I write a C or assembly program under DOS to determine whether a CD-ROM drive is installed?
Could some expert please give me some pointers, maybe show a code snippet.
Thanks!!!
Floor2 Wengier Posted 2003-01-29 00:00
系统支持 Posts 10,521 Credits 27,736
Good idea.
Floor3 qzxym Posted 2003-01-30 00:00
中级用户 Posts 39 Credits 210
It's extremely simple, even BASIC can do it. The principle is just to determine whether Mscdex.exe is already running.
Floor4 nre Posted 2003-01-30 00:00
银牌会员 Posts 361 Credits 1,210
The following is a quote from qzxym on 2003-1-30 22:40:16:
It's extremely simple, even BASIC can do it. The principle is just to determine whether Mscdex.exe is already running.

What's the point of saying that? How exactly do you do it?
Floor5 Wengier Posted 2003-01-30 00:00
系统支持 Posts 10,521 Credits 27,736
Just checking whether MSCDEX is loaded is useless, and there are also other CD-ROM extension drivers, such as SHSUCD and so on.
Floor6 Wengier Posted 2003-01-31 00:00
系统支持 Posts 10,521 Credits 27,736
I just adapted the source code of that FINDRAMD mentioned earlier by baobolz and turned it into a FINDCD program. It can locate the drive letter where the CD is and set it to the environment variable CDROM. It has two obvious advantages:

1: It fully supports CLOAKING+MSCDEX, while the FINDCD.COM 1.02 on the Internet does not;
2: It can directly set the CD drive letter to the variable CDROM, which is very convenient to use, and there is no need to check any ERRORLEVEL value.

I have uploaded it to the forum, download link:
Open attachment link expired (admin note 2009-4-28)

Floor7 MYS Posted 2003-02-06 00:00
元老会员 Posts 1,637 Credits 5,170 From 广东佛山
Very good, very good. This makes creating boot disks much more convenient.
Floor8 Wengier Posted 2003-02-06 00:00
系统支持 Posts 10,521 Credits 27,736
I made some more enhancements to FINDCD and uploaded it again:

Open attachment link expired (admin note 2009-4-28)

Note: it also fixes the BUG where the system would hang when there was no CD-ROM drive.


Floor9 nre Posted 2003-02-07 00:00
银牌会员 Posts 361 Credits 1,210
Upload the original source code too. It would help everyone improve their skills.
Floor10 Wengier Posted 2003-02-07 00:00
系统支持 Posts 10,521 Credits 27,736
All right, below is part of the source code of this program:

Program initialization section (PASCAL source code):

program findcd;
const
env: arrayof Char='CDROM=c:'; <- prepares the environment variable
var
drvs: array of Char; <- CD-ROM character list
envp: PChar;

CD-ROM search subroutine (assembly source code):

procedure setdrv; assembler;
asm mov bx,0
mov ax,1500h
int 2fh <- calls interrupt to obtain CD-ROM status
cmp bx,1 <- checks whether the CD-ROM drive is installed
jc @@m <- if not, exit subroutine
push bx
push ds
pop es
mov bx,offset drvs
mov ax,150Dh
int 2Fh
pop cx
@@l: add byte ptr ,'A'
inc bx
loop @@l
mov byte ptr ,0
@@m:
end;

Add environment variable subroutine (assembly source code, omitted);

Main calling program (PASCAL source code):
begin
setdrv; <- calls the CD-ROM search subroutine;
if ord(drvs)=0 then <- if there is no CD-ROM drive, display “No CD-ROM found”;
WriteLn('No CD-ROM found.'
else begin
env:=drvs; <- if a CD-ROM drive exists, add the first CD-ROM drive letter to the environment variable CDROM;
WriteLn(env); <- display “CDROM=x:” on the screen;
if not InsertEnvVar <- calls the add environment variable subroutine;
then WriteLn('Cannot set environment variable!'; <- if it cannot be added, display an error;
exit;
end;
end. <- program ends.
Floor11 pfox Posted 2003-02-07 00:00
银牌会员 Posts 446 Credits 1,451
Does it support dual CD-ROM drives and triple CD-ROM drives?
Floor12 Wengier Posted 2003-02-08 00:00
系统支持 Posts 10,521 Credits 27,736
pfox: Good idea, so I just enhanced FINDCD again. Download it here:
Open attachment link expired (admin note 2009-4-28)

The feature of this version is that “FINDCD n” (n is a number) will set the CDROM environment variable to the nth CD-ROM drive. If n is not specified, it defaults to the first CD-ROM drive (that is, n=1). If n is 0 or not a number, then CDROM will be set to the number of CD-ROM drives.

Suppose there are three CD-ROM drives E:、F:、G:, then running “FINDCD” or “FINDCD 1” will give “CDROM=E:”, and running “FINDCD 3” will give “CDROM=G:”; if you run “FINDCD 0” you will get “CDROM=3” (that is, there are three CD-ROM drives); if you run “FINDCD 4” it will say CD-ROM drive not found (because there are only three CD-ROM drives).


Floor13 pfox Posted 2003-02-08 00:00
银牌会员 Posts 446 Credits 1,451
Good!

Wengier, could you enhance it a bit more? When running FindCD without parameters,
have it directly assign the drive letter of the first CDROM to the CDROM environment variable, and if there is a
second or third CDROM drive letter, assign them directly to CDROM1 and CDROM2?
That way, in a boot disk, cda.exe can be used directly to determine whether there
is a disc in the CD-ROM drive and whether a file exists on it,
and if so it can be executed or called directly.






Floor14 Wengier Posted 2003-02-15 00:00
系统支持 Posts 10,521 Credits 27,736
Why not write a batch file to do it? Since after running FINDCD 0 the value of %CDROM% is the number of CD-ROM drives, you can make a batch loop and, based on the number of CD-ROM drives, set all the CD-ROM drive letters into the %CDROM% variables.
Floor15 Wengier Posted 2003-02-15 00:00
系统支持 Posts 10,521 Credits 27,736
I just wrote a simple DOS batch file. Using the FINDCD program above, it can conveniently set CDROM1, CDROM2, etc. in sequence to the CD-ROM drive letters. The program is as follows:
@echo off
set n=0
:loop
set cdrom=
findcd %n% (that is: call the FINDCD above)
if %n%==0 set cdn=%cdrom%
if not %n%==0 set cdrom%n%=%cdrom%
if %n%#==%cdn%# goto end
count n (that is: n=n+1)
goto loop
:end
set n=
set cdn=
set cdrom=
echo.
1 2 3 4  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023