无效的句柄传递给midiInClose

时间:2019-08-26 13:57:04

标签: delphi

下面有一些代码。我想发生的是,当按下按钮时,它会根据现有连接的状态创建或关闭MIDI连接。

但是,当尝试关闭MIDI手柄时,我收到一个错误响应代码5,这意味着MIDI设备的无效手柄已传递给midiInClose

我不确定为什么会发生这种情况,原因是范围问题?我只是不知道如何解决这个问题。应该将hMidiIn定义为表单中的类变量吗?

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.UITypes, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  MMSystem, uMIDI;

type
  TForm2 = class(TForm)
    MidiInputCombo: TComboBox;
    Label1: TLabel;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
  IsConnected: Boolean = False;
  SelectedMidiInputID: Integer = 0;
  hMidiIn: PHMIDIIN;
implementation

{$R *.dfm}

procedure MidiInProc(hmi: PHMIDIIN; wMsg: UINT; dwInstance: DWORD_PTR; dwParam1: DWORD_PTR; dwParam2: DWORD_PTR); stdcall;
begin
  // Do something
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  MidiInCloseResult: Integer;
begin
  if IsConnected then
    begin
      MidiInCloseResult :=  midiInClose(hMidiIn^);
      if MidiInCloseResult = MMSYSERR_NOERROR then
        begin
          IsConnected := False;
          Button1.Caption := 'Connect';
        end
      else
        MessageDlg('Response: ' + IntToStr(MidiInCloseResult), mtInformation, [mbOk], 0, mbOk);
    end
  else
    if midiInOpen(@hMidiIn, SelectedMidiInputID, DWORD_PTR(@MidiInProc), 0, CALLBACK_FUNCTION) = MMSYSERR_NOERROR then
      begin
        IsConnected := True;
        Button1.Caption := 'Disconnect';
      end;
end;

procedure TForm2.FormCreate(Sender: TObject);
var
  I: Integer;
  DevCount: Integer;
  Pmic: MIDIINCAPS;
begin
  DevCount := MidiInputDeviceCount();

  if DevCount > 0 then
    for I := 0 to DevCount do
       midiInGetDevCaps(I, @Pmic, SizeOf(pmic));
       MidiInputCombo.Items.Add(Pmic.szPname);

    MidiInputCombo.ItemIndex := SelectedMidiInputID;
end;

end.

0 个答案:

没有答案