ホーム › Media.Audio › PlaySoundA
PlaySoundA
関数ファイル・リソース・別名からサウンドを再生する(ANSI版)。
シグネチャ
// WINMM.dll (ANSI / -A)
#include <windows.h>
BOOL PlaySoundA(
LPCSTR pszSound, // optional
HMODULE hmod, // optional
SND_FLAGS fdwSound
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszSound | LPCSTR | inoptional | 再生する音の名前/ファイル名/リソース名(ANSI)。NULLで再生停止。 |
| hmod | HMODULE | inoptional | SND_RESOURCE指定時にリソースを含むモジュールのハンドル。それ以外はNULL。 |
| fdwSound | SND_FLAGS | in | 再生方法を指定するSND_FLAGSフラグ(SND_ASYNC、SND_LOOP等)。 |
戻り値の型: BOOL
各言語での呼び出し定義
// WINMM.dll (ANSI / -A)
#include <windows.h>
BOOL PlaySoundA(
LPCSTR pszSound, // optional
HMODULE hmod, // optional
SND_FLAGS fdwSound
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WINMM.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern bool PlaySoundA(
[MarshalAs(UnmanagedType.LPStr)] string pszSound, // LPCSTR optional
IntPtr hmod, // HMODULE optional
uint fdwSound // SND_FLAGS
);<DllImport("WINMM.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function PlaySoundA(
<MarshalAs(UnmanagedType.LPStr)> pszSound As String, ' LPCSTR optional
hmod As IntPtr, ' HMODULE optional
fdwSound As UInteger ' SND_FLAGS
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' pszSound : LPCSTR optional
' hmod : HMODULE optional
' fdwSound : SND_FLAGS
Declare PtrSafe Function PlaySoundA Lib "winmm" ( _
ByVal pszSound As String, _
ByVal hmod As LongPtr, _
ByVal fdwSound As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
PlaySoundA = ctypes.windll.winmm.PlaySoundA
PlaySoundA.restype = wintypes.BOOL
PlaySoundA.argtypes = [
wintypes.LPCSTR, # pszSound : LPCSTR optional
wintypes.HANDLE, # hmod : HMODULE optional
wintypes.DWORD, # fdwSound : SND_FLAGS
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('WINMM.dll')
PlaySoundA = Fiddle::Function.new(
lib['PlaySoundA'],
[
Fiddle::TYPE_VOIDP, # pszSound : LPCSTR optional
Fiddle::TYPE_VOIDP, # hmod : HMODULE optional
-Fiddle::TYPE_INT, # fdwSound : SND_FLAGS
],
Fiddle::TYPE_INT)#[link(name = "winmm")]
extern "system" {
fn PlaySoundA(
pszSound: *const u8, // LPCSTR optional
hmod: *mut core::ffi::c_void, // HMODULE optional
fdwSound: u32 // SND_FLAGS
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WINMM.dll", CharSet = CharSet.Ansi)]
public static extern bool PlaySoundA([MarshalAs(UnmanagedType.LPStr)] string pszSound, IntPtr hmod, uint fdwSound);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WINMM_PlaySoundA' -Namespace Win32 -PassThru
# $api::PlaySoundA(pszSound, hmod, fdwSound)#uselib "WINMM.dll"
#func global PlaySoundA "PlaySoundA" sptr, sptr, sptr
; PlaySoundA pszSound, hmod, fdwSound ; 戻り値は stat
; pszSound : LPCSTR optional -> "sptr"
; hmod : HMODULE optional -> "sptr"
; fdwSound : SND_FLAGS -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "WINMM.dll"
#cfunc global PlaySoundA "PlaySoundA" str, sptr, int
; res = PlaySoundA(pszSound, hmod, fdwSound)
; pszSound : LPCSTR optional -> "str"
; hmod : HMODULE optional -> "sptr"
; fdwSound : SND_FLAGS -> "int"; BOOL PlaySoundA(LPCSTR pszSound, HMODULE hmod, SND_FLAGS fdwSound)
#uselib "WINMM.dll"
#cfunc global PlaySoundA "PlaySoundA" str, intptr, int
; res = PlaySoundA(pszSound, hmod, fdwSound)
; pszSound : LPCSTR optional -> "str"
; hmod : HMODULE optional -> "intptr"
; fdwSound : SND_FLAGS -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
winmm = windows.NewLazySystemDLL("WINMM.dll")
procPlaySoundA = winmm.NewProc("PlaySoundA")
)
// pszSound (LPCSTR optional), hmod (HMODULE optional), fdwSound (SND_FLAGS)
r1, _, err := procPlaySoundA.Call(
uintptr(unsafe.Pointer(windows.BytePtrFromString(pszSound))),
uintptr(hmod),
uintptr(fdwSound),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction PlaySoundA(
pszSound: PAnsiChar; // LPCSTR optional
hmod: THandle; // HMODULE optional
fdwSound: DWORD // SND_FLAGS
): BOOL; stdcall;
external 'WINMM.dll' name 'PlaySoundA';result := DllCall("WINMM\PlaySoundA"
, "AStr", pszSound ; LPCSTR optional
, "Ptr", hmod ; HMODULE optional
, "UInt", fdwSound ; SND_FLAGS
, "Int") ; return: BOOL●PlaySoundA(pszSound, hmod, fdwSound) = DLL("WINMM.dll", "bool PlaySoundA(char*, void*, dword)")
# 呼び出し: PlaySoundA(pszSound, hmod, fdwSound)
# pszSound : LPCSTR optional -> "char*"
# hmod : HMODULE optional -> "void*"
# fdwSound : SND_FLAGS -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "winmm" fn PlaySoundA(
pszSound: [*c]const u8, // LPCSTR optional
hmod: ?*anyopaque, // HMODULE optional
fdwSound: u32 // SND_FLAGS
) callconv(std.os.windows.WINAPI) i32;proc PlaySoundA(
pszSound: cstring, # LPCSTR optional
hmod: pointer, # HMODULE optional
fdwSound: uint32 # SND_FLAGS
): int32 {.importc: "PlaySoundA", stdcall, dynlib: "WINMM.dll".}pragma(lib, "winmm");
extern(Windows)
int PlaySoundA(
const(char)* pszSound, // LPCSTR optional
void* hmod, // HMODULE optional
uint fdwSound // SND_FLAGS
);ccall((:PlaySoundA, "WINMM.dll"), stdcall, Int32,
(Cstring, Ptr{Cvoid}, UInt32),
pszSound, hmod, fdwSound)
# pszSound : LPCSTR optional -> Cstring
# hmod : HMODULE optional -> Ptr{Cvoid}
# fdwSound : SND_FLAGS -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t PlaySoundA(
const char* pszSound,
void* hmod,
uint32_t fdwSound);
]]
local winmm = ffi.load("winmm")
-- winmm.PlaySoundA(pszSound, hmod, fdwSound)
-- pszSound : LPCSTR optional
-- hmod : HMODULE optional
-- fdwSound : SND_FLAGS
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('WINMM.dll');
const PlaySoundA = lib.func('__stdcall', 'PlaySoundA', 'int32_t', ['str', 'void *', 'uint32_t']);
// PlaySoundA(pszSound, hmod, fdwSound)
// pszSound : LPCSTR optional -> 'str'
// hmod : HMODULE optional -> 'void *'
// fdwSound : SND_FLAGS -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("WINMM.dll", {
PlaySoundA: { parameters: ["buffer", "pointer", "u32"], result: "i32" },
});
// lib.symbols.PlaySoundA(pszSound, hmod, fdwSound)
// pszSound : LPCSTR optional -> "buffer"
// hmod : HMODULE optional -> "pointer"
// fdwSound : SND_FLAGS -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t PlaySoundA(
const char* pszSound,
void* hmod,
uint32_t fdwSound);
C, "WINMM.dll");
// $ffi->PlaySoundA(pszSound, hmod, fdwSound);
// pszSound : LPCSTR optional
// hmod : HMODULE optional
// fdwSound : SND_FLAGS
// 構造体/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, W32APIOptions.ASCII_OPTIONS);
boolean PlaySoundA(
String pszSound, // LPCSTR optional
Pointer hmod, // HMODULE optional
int fdwSound // SND_FLAGS
);
}@[Link("winmm")]
lib LibWINMM
fun PlaySoundA = PlaySoundA(
pszSound : UInt8*, # LPCSTR optional
hmod : Void*, # HMODULE optional
fdwSound : UInt32 # SND_FLAGS
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef PlaySoundANative = Int32 Function(Pointer<Utf8>, Pointer<Void>, Uint32);
typedef PlaySoundADart = int Function(Pointer<Utf8>, Pointer<Void>, int);
final PlaySoundA = DynamicLibrary.open('WINMM.dll')
.lookupFunction<PlaySoundANative, PlaySoundADart>('PlaySoundA');
// pszSound : LPCSTR optional -> Pointer<Utf8>
// hmod : HMODULE optional -> Pointer<Void>
// fdwSound : SND_FLAGS -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PlaySoundA(
pszSound: PAnsiChar; // LPCSTR optional
hmod: THandle; // HMODULE optional
fdwSound: DWORD // SND_FLAGS
): BOOL; stdcall;
external 'WINMM.dll' name 'PlaySoundA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PlaySoundA"
c_PlaySoundA :: CString -> Ptr () -> Word32 -> IO CInt
-- pszSound : LPCSTR optional -> CString
-- hmod : HMODULE optional -> Ptr ()
-- fdwSound : SND_FLAGS -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let playsounda =
foreign "PlaySoundA"
(string @-> (ptr void) @-> uint32_t @-> returning int32_t)
(* pszSound : LPCSTR optional -> string *)
(* hmod : HMODULE optional -> (ptr void) *)
(* fdwSound : SND_FLAGS -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library winmm (t "WINMM.dll"))
(cffi:use-foreign-library winmm)
(cffi:defcfun ("PlaySoundA" play-sound-a :convention :stdcall) :int32
(psz-sound :string) ; LPCSTR optional
(hmod :pointer) ; HMODULE optional
(fdw-sound :uint32)) ; SND_FLAGS
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $PlaySoundA = Win32::API::More->new('WINMM',
'BOOL PlaySoundA(LPCSTR pszSound, HANDLE hmod, DWORD fdwSound)');
# my $ret = $PlaySoundA->Call($pszSound, $hmod, $fdwSound);
# pszSound : LPCSTR optional -> LPCSTR
# hmod : HMODULE optional -> HANDLE
# fdwSound : SND_FLAGS -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f PlaySoundW (Unicode版) — ファイル・リソース・別名からサウンドを再生する(Unicode版)。
使用する型