4

Hi I am making a program to open and close the cd reader in which I have thought to write data to CD, the problem is the basis of the problem, which use "uses Windows 'and' uses MMSystem" but the problem is that when I use both at the same time being "uses Windows, MMSystem" gives an error and the program does not compile, I am using Delphi 2010, the strange thing is that when I use only one either Windows or MMSystem works fine and compiles.

The error when I try to compile is: 'Could not find program'

The code in question is this:

mciSendString ('Set cdaudio door open wait', nil, 0, handle);

I have two things to ask you first is how I avoid the error when using the two (Windows and MMSystem) and the other question was if he could open the CD player without using MMSystem, bone using Windows API, but not where to start

The source :

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,Windows,MMSystem;

procedure opencd;
begin
  mciSendString('Set cdaudio door open wait', nil, 0, 0);
end;

begin
  try
    Writeln('test');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Image :

test

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Jose Martinez
  • 303
  • 2
  • 6
  • 14
  • Please show a complete, short program that demonstrates the problem. Include verbatim the error message. See SSCCE. – David Heffernan Nov 10 '13 at 20:24
  • "Could not find program" is not a Delphi compiler message. Please post the **exact error message** you're receiving; it includes the file name, the line number, an error code, and the error message. You need to post them **all**. – Ken White Nov 10 '13 at 21:05
  • Do you actually try to compile, or just hit F9 (run)? If the executable, for some reason, is not really there, hit Ctrl+F9 or Shift+F9. – Sertac Akyuz Nov 10 '13 at 21:10
  • but it does not give me error because neither generates exe file. add an image of how I get the error – Jose Martinez Nov 10 '13 at 21:37
  • That's not a *compiler error*. A compiler error is displayed in the Messages window at the bottom of the screen, and it looks like this: `[dcc32 Error] Unit4.pas(41): E2066 Missing operator or semicolon.`. – Ken White Nov 10 '13 at 21:41
  • @Jose - You are not getting that error when you try to compile, am I wrong? Just hit Ctrl+F9, do you get any error? – Sertac Akyuz Nov 10 '13 at 21:41
  • when I press ctrl + F9 says 'done' – Jose Martinez Nov 10 '13 at 21:46
  • 1
    Is your virus scanner deleteing the executable file immediately after the compiler creates it? – David Heffernan Nov 10 '13 at 21:49
  • rare, this same code goes well when I used a visual form but when I use a console program this happens, do not understand what's going on. the use antivirus is avast not report anything when I compile the program – Jose Martinez Nov 10 '13 at 21:50

1 Answers1

4

There should be no problem using 'mmsystem' together with 'windows'. Indeed he error in the screen shot in the question does not look like a compiler error. It's rather, the IDE is unable to find the executable. It might be antivirus software perhaps deleting the executable, or I don't know..

In any case, you can use DeviceIoControl as an alternative. Here's a Delphi translation of an answer on SO:

function CtlCode(DeviceType, _Function, Method, Access: Integer): DWORD;
begin
  Result := DeviceType shl 16 or Access shl 14 or _Function shl 2 or Method;
end;

procedure ejectDisk(driveLetter: Char);
const
  FILE_DEVICE_FILE_SYSTEM = $00000009;
  FILE_DEVICE_MASS_STORAGE = $0000002d;
  METHOD_BUFFERED = 0;
  FILE_ANY_ACCESS = 0;
  FILE_READ_ACCESS = $0001;
  IOCTL_STORAGE_BASE = FILE_DEVICE_MASS_STORAGE;
// bogus constants below, rather CTL_CODEs should be pre computed.
  FSCTL_LOCK_VOLUME = 6;
  FSCTL_DISMOUNT_VOLUME = 8;
  IOCTL_STORAGE_EJECT_MEDIA = $0202;
var
  tmp: string;
  handle: THandle;
  BytesReturned: DWORD;
begin
  tmp := Format('\\.\%s:', [driveLetter]);
  handle := CreateFile(PChar(tmp), GENERIC_READ, FILE_SHARE_WRITE, nil,
      OPEN_EXISTING, 0, 0);
  DeviceIoControl(handle,
      CtlCode(FILE_DEVICE_FILE_SYSTEM, FSCTL_LOCK_VOLUME, METHOD_BUFFERED,
      FILE_ANY_ACCESS), nil, 0, nil, 0, BytesReturned, nil);
  DeviceIoControl(handle,
      CtlCode(FILE_DEVICE_FILE_SYSTEM, FSCTL_DISMOUNT_VOLUME, METHOD_BUFFERED,
      FILE_ANY_ACCESS), nil, 0, nil, 0, BytesReturned, nil);
  DeviceIoControl(handle,
      CtlCode(IOCTL_STORAGE_BASE, IOCTL_STORAGE_EJECT_MEDIA, METHOD_BUFFERED,
      FILE_READ_ACCESS), nil, 0, nil, 0, BytesReturned, nil);
  CloseHandle(handle);
end;
Community
  • 1
  • 1
Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
  • nooo, avast antivirus was the problem, disable the shields for 10 minutes and the code'm right, I ask you to please explain to me how I avoid this error or if I can open the cd reader using windows api – Jose Martinez Nov 10 '13 at 21:56
  • @David - Which one? The one requesting SSCCE, or the one about the AV software? – Sertac Akyuz Nov 10 '13 at 22:02
  • sorry did not know that by the code because the code did not understand a question, as would do to close the cd reader, the code I just try and tell me this error: [DCC Error] Project1.dpr (29): E2003 Undeclared identifier: 'CtlCode' – Jose Martinez Nov 10 '13 at 22:04
  • @Jose - Oops! Sorry, forgot to include that one. I edited the answer. – Sertac Akyuz Nov 10 '13 at 22:05
  • @SertacAkyuz AV software. I can see that you came up with the same idea. – David Heffernan Nov 10 '13 at 22:07
  • @David - Indeed. Actually I was writing the answer when you suggested it and then Jose admitted it. But I didn't change the text since the question still has it. – Sertac Akyuz Nov 10 '13 at 22:10
  • great, works perfectly, I do not ask much for the code ah helped me a lot, but wanted to ask you as I close the reader, who would have to change the code to do that? – Jose Martinez Nov 10 '13 at 22:11
  • 1
    @Jose - Use `IOCTL_STORAGE_LOAD_MEDIA` ($0203) instead of `IOCTL_STORAGE_EJECT_MEDIA`. – Sertac Akyuz Nov 10 '13 at 22:14