share
Stack OverflowUsing Windows and MMSystem in Delphi
[+4] [1] Jose Martinez
[2013-11-10 20:10:03]
[ delphi ]
[ https://stackoverflow.com/questions/19894566/using-windows-and-mmsystem-in-delphi ]

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

Please show a complete, short program that demonstrates the problem. Include verbatim the error message. See SSCCE. - David Heffernan
"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
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
but it does not give me error because neither generates exe file. add an image of how I get the error - Jose Martinez
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
@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
when I press ctrl + F9 says 'done' - Jose Martinez
(1) Is your virus scanner deleteing the executable file immediately after the compiler creates it? - David Heffernan
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
[+4] [2013-11-10 21:50:56] Sertac Akyuz [ACCEPTED]

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 [1] as an alternative. Here's a Delphi translation of an answer [2] 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;
[1] http://msdn.microsoft.com/en-us/library/windows/desktop/aa363216%28v=vs.85%29.aspx
[2] https://stackoverflow.com/a/77291/243614

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
@David - Which one? The one requesting SSCCE, or the one about the AV software? - Sertac Akyuz
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
@Jose - Oops! Sorry, forgot to include that one. I edited the answer. - Sertac Akyuz
@SertacAkyuz AV software. I can see that you came up with the same idea. - David Heffernan
@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
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
(1) @Jose - Use IOCTL_STORAGE_LOAD_MEDIA ($0203) instead of IOCTL_STORAGE_EJECT_MEDIA. - Sertac Akyuz
1