Win32 API 日本語リファレンス
ホームMedia.Audio › midiOutOpen

midiOutOpen

関数
再生用のMIDI出力デバイスを開く。
DLLWINMM.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// WINMM.dll
#include <windows.h>

DWORD midiOutOpen(
    HMIDIOUT* phmo,
    DWORD uDeviceID,
    UINT_PTR dwCallback,   // optional
    UINT_PTR dwInstance,   // optional
    MIDI_WAVE_OPEN_TYPE fdwOpen
);

パラメーター

名前方向説明
phmoHMIDIOUT*outHMIDIOUT ハンドルへのポインターです。この場所には、開かれた MIDI 出力デバイスを識別するハンドルが格納されます。このハンドルは、他の MIDI 出力関数の呼び出しでデバイスを識別するために使用されます。
uDeviceIDDWORDin開く対象の MIDI 出力デバイスの識別子です。
dwCallbackUINT_PTRinoptionalMIDI 再生中に再生の進行状況に関するメッセージを処理するために呼び出される、コールバック関数、イベントハンドル、スレッド識別子、またはウィンドウやスレッドのハンドルへのポインターです。コールバックが不要な場合は、このパラメーターに NULL を指定します。コールバック関数の詳細については、MidiOutProc を参照してください。
dwInstanceUINT_PTRinoptionalコールバックに渡されるユーザーインスタンスデータです。このパラメーターは、ウィンドウコールバックやスレッドでは使用されません。
fdwOpenMIDI_WAVE_OPEN_TYPEin

デバイスを開く際のコールバックフラグです。次の値を指定できます。

意味
CALLBACK_EVENT dwCallback パラメーターはイベントハンドルです。このコールバックメカニズムは出力専用です。
CALLBACK_FUNCTION dwCallback パラメーターはコールバック関数のアドレスです。
CALLBACK_NULL コールバックメカニズムはありません。この値が既定の設定です。
CALLBACK_THREAD dwCallback パラメーターはスレッド識別子です。
CALLBACK_WINDOW dwCallback パラメーターはウィンドウハンドルです。

戻り値の型: DWORD

公式ドキュメント

midiOutOpen 関数は、再生のために MIDI 出力デバイスを開きます。

戻り値

成功した場合は MMSYSERR_NOERROR を、それ以外の場合はエラーを返します。発生し得るエラー値には次のものがあります。

戻り値 説明
MIDIERR_NODEVICE
MIDI ポートが見つかりませんでした。このエラーは、マッパーを開いた場合にのみ発生します。
MMSYSERR_ALLOCATED
指定されたリソースは既に割り当てられています。
MMSYSERR_BADDEVICEID
指定されたデバイス識別子が範囲外です。
MMSYSERR_INVALPARAM
指定されたポインターまたは構造体が無効です。
MMSYSERR_NOMEM
システムがメモリを割り当てまたはロックできません。

解説(Remarks)

システムに存在する MIDI 出力デバイスの数を調べるには、midiOutGetNumDevs 関数を使用します。wDeviceID で指定するデバイス識別子は、0 から存在するデバイス数より 1 小さい値までの範囲です。デバイス識別子として MIDI_MAPPER を使用することもできます。

コールバック情報を受け取る対象としてウィンドウまたはスレッドを選択した場合、MIDI 出力の進行状況を示すために次のメッセージがウィンドウプロシージャまたはスレッドに送信されます: MM_MOM_OPENMM_MOM_CLOSE、および MM_MOM_DONE

コールバック情報を受け取る対象として関数を選択した場合、MIDI 出力の進行状況を示すために次のメッセージが関数に送信されます: MOM_OPENMOM_CLOSE、および MOM_DONE

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// WINMM.dll
#include <windows.h>

DWORD midiOutOpen(
    HMIDIOUT* phmo,
    DWORD uDeviceID,
    UINT_PTR dwCallback,   // optional
    UINT_PTR dwInstance,   // optional
    MIDI_WAVE_OPEN_TYPE fdwOpen
);
[DllImport("WINMM.dll", ExactSpelling = true)]
static extern uint midiOutOpen(
    IntPtr phmo,   // HMIDIOUT* out
    uint uDeviceID,   // DWORD
    UIntPtr dwCallback,   // UINT_PTR optional
    UIntPtr dwInstance,   // UINT_PTR optional
    uint fdwOpen   // MIDI_WAVE_OPEN_TYPE
);
<DllImport("WINMM.dll", ExactSpelling:=True)>
Public Shared Function midiOutOpen(
    phmo As IntPtr,   ' HMIDIOUT* out
    uDeviceID As UInteger,   ' DWORD
    dwCallback As UIntPtr,   ' UINT_PTR optional
    dwInstance As UIntPtr,   ' UINT_PTR optional
    fdwOpen As UInteger   ' MIDI_WAVE_OPEN_TYPE
) As UInteger
End Function
' phmo : HMIDIOUT* out
' uDeviceID : DWORD
' dwCallback : UINT_PTR optional
' dwInstance : UINT_PTR optional
' fdwOpen : MIDI_WAVE_OPEN_TYPE
Declare PtrSafe Function midiOutOpen Lib "winmm" ( _
    ByVal phmo As LongPtr, _
    ByVal uDeviceID As Long, _
    ByVal dwCallback As LongPtr, _
    ByVal dwInstance As LongPtr, _
    ByVal fdwOpen As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

midiOutOpen = ctypes.windll.winmm.midiOutOpen
midiOutOpen.restype = wintypes.DWORD
midiOutOpen.argtypes = [
    ctypes.c_void_p,  # phmo : HMIDIOUT* out
    wintypes.DWORD,  # uDeviceID : DWORD
    ctypes.c_size_t,  # dwCallback : UINT_PTR optional
    ctypes.c_size_t,  # dwInstance : UINT_PTR optional
    wintypes.DWORD,  # fdwOpen : MIDI_WAVE_OPEN_TYPE
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('WINMM.dll')
midiOutOpen = Fiddle::Function.new(
  lib['midiOutOpen'],
  [
    Fiddle::TYPE_VOIDP,  # phmo : HMIDIOUT* out
    -Fiddle::TYPE_INT,  # uDeviceID : DWORD
    Fiddle::TYPE_UINTPTR_T,  # dwCallback : UINT_PTR optional
    Fiddle::TYPE_UINTPTR_T,  # dwInstance : UINT_PTR optional
    -Fiddle::TYPE_INT,  # fdwOpen : MIDI_WAVE_OPEN_TYPE
  ],
  -Fiddle::TYPE_INT)
#[link(name = "winmm")]
extern "system" {
    fn midiOutOpen(
        phmo: *mut *mut core::ffi::c_void,  // HMIDIOUT* out
        uDeviceID: u32,  // DWORD
        dwCallback: usize,  // UINT_PTR optional
        dwInstance: usize,  // UINT_PTR optional
        fdwOpen: u32  // MIDI_WAVE_OPEN_TYPE
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("WINMM.dll")]
public static extern uint midiOutOpen(IntPtr phmo, uint uDeviceID, UIntPtr dwCallback, UIntPtr dwInstance, uint fdwOpen);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WINMM_midiOutOpen' -Namespace Win32 -PassThru
# $api::midiOutOpen(phmo, uDeviceID, dwCallback, dwInstance, fdwOpen)
#uselib "WINMM.dll"
#func global midiOutOpen "midiOutOpen" sptr, sptr, sptr, sptr, sptr
; midiOutOpen phmo, uDeviceID, dwCallback, dwInstance, fdwOpen   ; 戻り値は stat
; phmo : HMIDIOUT* out -> "sptr"
; uDeviceID : DWORD -> "sptr"
; dwCallback : UINT_PTR optional -> "sptr"
; dwInstance : UINT_PTR optional -> "sptr"
; fdwOpen : MIDI_WAVE_OPEN_TYPE -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "WINMM.dll"
#cfunc global midiOutOpen "midiOutOpen" sptr, int, sptr, sptr, int
; res = midiOutOpen(phmo, uDeviceID, dwCallback, dwInstance, fdwOpen)
; phmo : HMIDIOUT* out -> "sptr"
; uDeviceID : DWORD -> "int"
; dwCallback : UINT_PTR optional -> "sptr"
; dwInstance : UINT_PTR optional -> "sptr"
; fdwOpen : MIDI_WAVE_OPEN_TYPE -> "int"
; DWORD midiOutOpen(HMIDIOUT* phmo, DWORD uDeviceID, UINT_PTR dwCallback, UINT_PTR dwInstance, MIDI_WAVE_OPEN_TYPE fdwOpen)
#uselib "WINMM.dll"
#cfunc global midiOutOpen "midiOutOpen" intptr, int, intptr, intptr, int
; res = midiOutOpen(phmo, uDeviceID, dwCallback, dwInstance, fdwOpen)
; phmo : HMIDIOUT* out -> "intptr"
; uDeviceID : DWORD -> "int"
; dwCallback : UINT_PTR optional -> "intptr"
; dwInstance : UINT_PTR optional -> "intptr"
; fdwOpen : MIDI_WAVE_OPEN_TYPE -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	winmm = windows.NewLazySystemDLL("WINMM.dll")
	procmidiOutOpen = winmm.NewProc("midiOutOpen")
)

// phmo (HMIDIOUT* out), uDeviceID (DWORD), dwCallback (UINT_PTR optional), dwInstance (UINT_PTR optional), fdwOpen (MIDI_WAVE_OPEN_TYPE)
r1, _, err := procmidiOutOpen.Call(
	uintptr(phmo),
	uintptr(uDeviceID),
	uintptr(dwCallback),
	uintptr(dwInstance),
	uintptr(fdwOpen),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // DWORD
function midiOutOpen(
  phmo: Pointer;   // HMIDIOUT* out
  uDeviceID: DWORD;   // DWORD
  dwCallback: NativeUInt;   // UINT_PTR optional
  dwInstance: NativeUInt;   // UINT_PTR optional
  fdwOpen: DWORD   // MIDI_WAVE_OPEN_TYPE
): DWORD; stdcall;
  external 'WINMM.dll' name 'midiOutOpen';
result := DllCall("WINMM\midiOutOpen"
    , "Ptr", phmo   ; HMIDIOUT* out
    , "UInt", uDeviceID   ; DWORD
    , "UPtr", dwCallback   ; UINT_PTR optional
    , "UPtr", dwInstance   ; UINT_PTR optional
    , "UInt", fdwOpen   ; MIDI_WAVE_OPEN_TYPE
    , "UInt")   ; return: DWORD
●midiOutOpen(phmo, uDeviceID, dwCallback, dwInstance, fdwOpen) = DLL("WINMM.dll", "dword midiOutOpen(void*, dword, int, int, dword)")
# 呼び出し: midiOutOpen(phmo, uDeviceID, dwCallback, dwInstance, fdwOpen)
# phmo : HMIDIOUT* out -> "void*"
# uDeviceID : DWORD -> "dword"
# dwCallback : UINT_PTR optional -> "int"
# dwInstance : UINT_PTR optional -> "int"
# fdwOpen : MIDI_WAVE_OPEN_TYPE -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "winmm" fn midiOutOpen(
    phmo: ?*anyopaque, // HMIDIOUT* out
    uDeviceID: u32, // DWORD
    dwCallback: usize, // UINT_PTR optional
    dwInstance: usize, // UINT_PTR optional
    fdwOpen: u32 // MIDI_WAVE_OPEN_TYPE
) callconv(std.os.windows.WINAPI) u32;
proc midiOutOpen(
    phmo: pointer,  # HMIDIOUT* out
    uDeviceID: uint32,  # DWORD
    dwCallback: uint,  # UINT_PTR optional
    dwInstance: uint,  # UINT_PTR optional
    fdwOpen: uint32  # MIDI_WAVE_OPEN_TYPE
): uint32 {.importc: "midiOutOpen", stdcall, dynlib: "WINMM.dll".}
pragma(lib, "winmm");
extern(Windows)
uint midiOutOpen(
    void* phmo,   // HMIDIOUT* out
    uint uDeviceID,   // DWORD
    size_t dwCallback,   // UINT_PTR optional
    size_t dwInstance,   // UINT_PTR optional
    uint fdwOpen   // MIDI_WAVE_OPEN_TYPE
);
ccall((:midiOutOpen, "WINMM.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, UInt32, Csize_t, Csize_t, UInt32),
      phmo, uDeviceID, dwCallback, dwInstance, fdwOpen)
# phmo : HMIDIOUT* out -> Ptr{Cvoid}
# uDeviceID : DWORD -> UInt32
# dwCallback : UINT_PTR optional -> Csize_t
# dwInstance : UINT_PTR optional -> Csize_t
# fdwOpen : MIDI_WAVE_OPEN_TYPE -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t midiOutOpen(
    void* phmo,
    uint32_t uDeviceID,
    uintptr_t dwCallback,
    uintptr_t dwInstance,
    uint32_t fdwOpen);
]]
local winmm = ffi.load("winmm")
-- winmm.midiOutOpen(phmo, uDeviceID, dwCallback, dwInstance, fdwOpen)
-- phmo : HMIDIOUT* out
-- uDeviceID : DWORD
-- dwCallback : UINT_PTR optional
-- dwInstance : UINT_PTR optional
-- fdwOpen : MIDI_WAVE_OPEN_TYPE
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('WINMM.dll');
const midiOutOpen = lib.func('__stdcall', 'midiOutOpen', 'uint32_t', ['void *', 'uint32_t', 'uintptr_t', 'uintptr_t', 'uint32_t']);
// midiOutOpen(phmo, uDeviceID, dwCallback, dwInstance, fdwOpen)
// phmo : HMIDIOUT* out -> 'void *'
// uDeviceID : DWORD -> 'uint32_t'
// dwCallback : UINT_PTR optional -> 'uintptr_t'
// dwInstance : UINT_PTR optional -> 'uintptr_t'
// fdwOpen : MIDI_WAVE_OPEN_TYPE -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("WINMM.dll", {
  midiOutOpen: { parameters: ["pointer", "u32", "usize", "usize", "u32"], result: "u32" },
});
// lib.symbols.midiOutOpen(phmo, uDeviceID, dwCallback, dwInstance, fdwOpen)
// phmo : HMIDIOUT* out -> "pointer"
// uDeviceID : DWORD -> "u32"
// dwCallback : UINT_PTR optional -> "usize"
// dwInstance : UINT_PTR optional -> "usize"
// fdwOpen : MIDI_WAVE_OPEN_TYPE -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t midiOutOpen(
    void* phmo,
    uint32_t uDeviceID,
    size_t dwCallback,
    size_t dwInstance,
    uint32_t fdwOpen);
C, "WINMM.dll");
// $ffi->midiOutOpen(phmo, uDeviceID, dwCallback, dwInstance, fdwOpen);
// phmo : HMIDIOUT* out
// uDeviceID : DWORD
// dwCallback : UINT_PTR optional
// dwInstance : UINT_PTR optional
// fdwOpen : MIDI_WAVE_OPEN_TYPE
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Winmm extends StdCallLibrary {
    Winmm INSTANCE = Native.load("winmm", Winmm.class);
    int midiOutOpen(
        Pointer phmo,   // HMIDIOUT* out
        int uDeviceID,   // DWORD
        long dwCallback,   // UINT_PTR optional
        long dwInstance,   // UINT_PTR optional
        int fdwOpen   // MIDI_WAVE_OPEN_TYPE
    );
}
@[Link("winmm")]
lib LibWINMM
  fun midiOutOpen = midiOutOpen(
    phmo : Void*,   # HMIDIOUT* out
    uDeviceID : UInt32,   # DWORD
    dwCallback : LibC::SizeT,   # UINT_PTR optional
    dwInstance : LibC::SizeT,   # UINT_PTR optional
    fdwOpen : UInt32   # MIDI_WAVE_OPEN_TYPE
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef midiOutOpenNative = Uint32 Function(Pointer<Void>, Uint32, UintPtr, UintPtr, Uint32);
typedef midiOutOpenDart = int Function(Pointer<Void>, int, int, int, int);
final midiOutOpen = DynamicLibrary.open('WINMM.dll')
    .lookupFunction<midiOutOpenNative, midiOutOpenDart>('midiOutOpen');
// phmo : HMIDIOUT* out -> Pointer<Void>
// uDeviceID : DWORD -> Uint32
// dwCallback : UINT_PTR optional -> UintPtr
// dwInstance : UINT_PTR optional -> UintPtr
// fdwOpen : MIDI_WAVE_OPEN_TYPE -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function midiOutOpen(
  phmo: Pointer;   // HMIDIOUT* out
  uDeviceID: DWORD;   // DWORD
  dwCallback: NativeUInt;   // UINT_PTR optional
  dwInstance: NativeUInt;   // UINT_PTR optional
  fdwOpen: DWORD   // MIDI_WAVE_OPEN_TYPE
): DWORD; stdcall;
  external 'WINMM.dll' name 'midiOutOpen';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "midiOutOpen"
  c_midiOutOpen :: Ptr () -> Word32 -> CUIntPtr -> CUIntPtr -> Word32 -> IO Word32
-- phmo : HMIDIOUT* out -> Ptr ()
-- uDeviceID : DWORD -> Word32
-- dwCallback : UINT_PTR optional -> CUIntPtr
-- dwInstance : UINT_PTR optional -> CUIntPtr
-- fdwOpen : MIDI_WAVE_OPEN_TYPE -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let midioutopen =
  foreign "midiOutOpen"
    ((ptr void) @-> uint32_t @-> size_t @-> size_t @-> uint32_t @-> returning uint32_t)
(* phmo : HMIDIOUT* out -> (ptr void) *)
(* uDeviceID : DWORD -> uint32_t *)
(* dwCallback : UINT_PTR optional -> size_t *)
(* dwInstance : UINT_PTR optional -> size_t *)
(* fdwOpen : MIDI_WAVE_OPEN_TYPE -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library winmm (t "WINMM.dll"))
(cffi:use-foreign-library winmm)

(cffi:defcfun ("midiOutOpen" midi-out-open :convention :stdcall) :uint32
  (phmo :pointer)   ; HMIDIOUT* out
  (u-device-id :uint32)   ; DWORD
  (dw-callback :uint64)   ; UINT_PTR optional
  (dw-instance :uint64)   ; UINT_PTR optional
  (fdw-open :uint32))   ; MIDI_WAVE_OPEN_TYPE
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $midiOutOpen = Win32::API::More->new('WINMM',
    'DWORD midiOutOpen(HANDLE phmo, DWORD uDeviceID, WPARAM dwCallback, WPARAM dwInstance, DWORD fdwOpen)');
# my $ret = $midiOutOpen->Call($phmo, $uDeviceID, $dwCallback, $dwInstance, $fdwOpen);
# phmo : HMIDIOUT* out -> HANDLE
# uDeviceID : DWORD -> DWORD
# dwCallback : UINT_PTR optional -> WPARAM
# dwInstance : UINT_PTR optional -> WPARAM
# fdwOpen : MIDI_WAVE_OPEN_TYPE -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型