ホーム › Media.Audio › PlaySoundW
PlaySoundW
関数ファイル・リソース・別名からサウンドを再生する(Unicode版)。
シグネチャ
// WINMM.dll (Unicode / -W)
#include <windows.h>
BOOL PlaySoundW(
LPCWSTR pszSound, // optional
HMODULE hmod, // optional
SND_FLAGS fdwSound
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pszSound | LPCWSTR | inoptional | 再生する音の名前/ファイル名/リソース名(Unicode)。NULLで再生停止。 |
| hmod | HMODULE | inoptional | SND_RESOURCE指定時にリソースを含むモジュールのハンドル。それ以外はNULL。 |
| fdwSound | SND_FLAGS | in | 再生方法を指定するSND_FLAGSフラグ(SND_ASYNC、SND_LOOP等)。 |
戻り値の型: BOOL
各言語での呼び出し定義
// WINMM.dll (Unicode / -W)
#include <windows.h>
BOOL PlaySoundW(
LPCWSTR pszSound, // optional
HMODULE hmod, // optional
SND_FLAGS fdwSound
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("WINMM.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern bool PlaySoundW(
[MarshalAs(UnmanagedType.LPWStr)] string pszSound, // LPCWSTR optional
IntPtr hmod, // HMODULE optional
uint fdwSound // SND_FLAGS
);<DllImport("WINMM.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function PlaySoundW(
<MarshalAs(UnmanagedType.LPWStr)> pszSound As String, ' LPCWSTR optional
hmod As IntPtr, ' HMODULE optional
fdwSound As UInteger ' SND_FLAGS
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function' pszSound : LPCWSTR optional
' hmod : HMODULE optional
' fdwSound : SND_FLAGS
Declare PtrSafe Function PlaySoundW Lib "winmm" ( _
ByVal pszSound As LongPtr, _
ByVal hmod As LongPtr, _
ByVal fdwSound As Long) As Long
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
PlaySoundW = ctypes.windll.winmm.PlaySoundW
PlaySoundW.restype = wintypes.BOOL
PlaySoundW.argtypes = [
wintypes.LPCWSTR, # pszSound : LPCWSTR optional
wintypes.HANDLE, # hmod : HMODULE optional
wintypes.DWORD, # fdwSound : SND_FLAGS
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('WINMM.dll')
PlaySoundW = Fiddle::Function.new(
lib['PlaySoundW'],
[
Fiddle::TYPE_VOIDP, # pszSound : LPCWSTR optional
Fiddle::TYPE_VOIDP, # hmod : HMODULE optional
-Fiddle::TYPE_INT, # fdwSound : SND_FLAGS
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "winmm")]
extern "system" {
fn PlaySoundW(
pszSound: *const u16, // LPCWSTR 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.Unicode)]
public static extern bool PlaySoundW([MarshalAs(UnmanagedType.LPWStr)] string pszSound, IntPtr hmod, uint fdwSound);
"@
$api = Add-Type -MemberDefinition $sig -Name 'WINMM_PlaySoundW' -Namespace Win32 -PassThru
# $api::PlaySoundW(pszSound, hmod, fdwSound)#uselib "WINMM.dll"
#func global PlaySoundW "PlaySoundW" wptr, wptr, wptr
; PlaySoundW pszSound, hmod, fdwSound ; 戻り値は stat
; pszSound : LPCWSTR optional -> "wptr"
; hmod : HMODULE optional -> "wptr"
; fdwSound : SND_FLAGS -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "WINMM.dll"
#cfunc global PlaySoundW "PlaySoundW" wstr, sptr, int
; res = PlaySoundW(pszSound, hmod, fdwSound)
; pszSound : LPCWSTR optional -> "wstr"
; hmod : HMODULE optional -> "sptr"
; fdwSound : SND_FLAGS -> "int"; BOOL PlaySoundW(LPCWSTR pszSound, HMODULE hmod, SND_FLAGS fdwSound)
#uselib "WINMM.dll"
#cfunc global PlaySoundW "PlaySoundW" wstr, intptr, int
; res = PlaySoundW(pszSound, hmod, fdwSound)
; pszSound : LPCWSTR optional -> "wstr"
; hmod : HMODULE optional -> "intptr"
; fdwSound : SND_FLAGS -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
winmm = windows.NewLazySystemDLL("WINMM.dll")
procPlaySoundW = winmm.NewProc("PlaySoundW")
)
// pszSound (LPCWSTR optional), hmod (HMODULE optional), fdwSound (SND_FLAGS)
r1, _, err := procPlaySoundW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pszSound))),
uintptr(hmod),
uintptr(fdwSound),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction PlaySoundW(
pszSound: PWideChar; // LPCWSTR optional
hmod: THandle; // HMODULE optional
fdwSound: DWORD // SND_FLAGS
): BOOL; stdcall;
external 'WINMM.dll' name 'PlaySoundW';result := DllCall("WINMM\PlaySoundW"
, "WStr", pszSound ; LPCWSTR optional
, "Ptr", hmod ; HMODULE optional
, "UInt", fdwSound ; SND_FLAGS
, "Int") ; return: BOOL●PlaySoundW(pszSound, hmod, fdwSound) = DLL("WINMM.dll", "bool PlaySoundW(char*, void*, dword)")
# 呼び出し: PlaySoundW(pszSound, hmod, fdwSound)
# pszSound : LPCWSTR optional -> "char*"
# hmod : HMODULE optional -> "void*"
# fdwSound : SND_FLAGS -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "winmm" fn PlaySoundW(
pszSound: [*c]const u16, // LPCWSTR optional
hmod: ?*anyopaque, // HMODULE optional
fdwSound: u32 // SND_FLAGS
) callconv(std.os.windows.WINAPI) i32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc PlaySoundW(
pszSound: WideCString, # LPCWSTR optional
hmod: pointer, # HMODULE optional
fdwSound: uint32 # SND_FLAGS
): int32 {.importc: "PlaySoundW", stdcall, dynlib: "WINMM.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "winmm");
extern(Windows)
int PlaySoundW(
const(wchar)* pszSound, // LPCWSTR optional
void* hmod, // HMODULE optional
uint fdwSound // SND_FLAGS
);ccall((:PlaySoundW, "WINMM.dll"), stdcall, Int32,
(Cwstring, Ptr{Cvoid}, UInt32),
pszSound, hmod, fdwSound)
# pszSound : LPCWSTR optional -> Cwstring
# hmod : HMODULE optional -> Ptr{Cvoid}
# fdwSound : SND_FLAGS -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
int32_t PlaySoundW(
const uint16_t* pszSound,
void* hmod,
uint32_t fdwSound);
]]
local winmm = ffi.load("winmm")
-- winmm.PlaySoundW(pszSound, hmod, fdwSound)
-- pszSound : LPCWSTR optional
-- hmod : HMODULE optional
-- fdwSound : SND_FLAGS
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('WINMM.dll');
const PlaySoundW = lib.func('__stdcall', 'PlaySoundW', 'int32_t', ['str16', 'void *', 'uint32_t']);
// PlaySoundW(pszSound, hmod, fdwSound)
// pszSound : LPCWSTR optional -> 'str16'
// hmod : HMODULE optional -> 'void *'
// fdwSound : SND_FLAGS -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("WINMM.dll", {
PlaySoundW: { parameters: ["buffer", "pointer", "u32"], result: "i32" },
});
// lib.symbols.PlaySoundW(pszSound, hmod, fdwSound)
// pszSound : LPCWSTR optional -> "buffer"
// hmod : HMODULE optional -> "pointer"
// fdwSound : SND_FLAGS -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t PlaySoundW(
const uint16_t* pszSound,
void* hmod,
uint32_t fdwSound);
C, "WINMM.dll");
// $ffi->PlaySoundW(pszSound, hmod, fdwSound);
// pszSound : LPCWSTR 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.UNICODE_OPTIONS);
boolean PlaySoundW(
WString pszSound, // LPCWSTR optional
Pointer hmod, // HMODULE optional
int fdwSound // SND_FLAGS
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("winmm")]
lib LibWINMM
fun PlaySoundW = PlaySoundW(
pszSound : UInt16*, # LPCWSTR 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 PlaySoundWNative = Int32 Function(Pointer<Utf16>, Pointer<Void>, Uint32);
typedef PlaySoundWDart = int Function(Pointer<Utf16>, Pointer<Void>, int);
final PlaySoundW = DynamicLibrary.open('WINMM.dll')
.lookupFunction<PlaySoundWNative, PlaySoundWDart>('PlaySoundW');
// pszSound : LPCWSTR optional -> Pointer<Utf16>
// hmod : HMODULE optional -> Pointer<Void>
// fdwSound : SND_FLAGS -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function PlaySoundW(
pszSound: PWideChar; // LPCWSTR optional
hmod: THandle; // HMODULE optional
fdwSound: DWORD // SND_FLAGS
): BOOL; stdcall;
external 'WINMM.dll' name 'PlaySoundW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "PlaySoundW"
c_PlaySoundW :: CWString -> Ptr () -> Word32 -> IO CInt
-- pszSound : LPCWSTR optional -> CWString
-- hmod : HMODULE optional -> Ptr ()
-- fdwSound : SND_FLAGS -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let playsoundw =
foreign "PlaySoundW"
((ptr uint16_t) @-> (ptr void) @-> uint32_t @-> returning int32_t)
(* pszSound : LPCWSTR optional -> (ptr uint16_t) *)
(* 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 ("PlaySoundW" play-sound-w :convention :stdcall) :int32
(psz-sound (:string :encoding :utf-16le)) ; LPCWSTR 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 $PlaySoundW = Win32::API::More->new('WINMM',
'BOOL PlaySoundW(LPCWSTR pszSound, HANDLE hmod, DWORD fdwSound)');
# my $ret = $PlaySoundW->Call($pszSound, $hmod, $fdwSound);
# pszSound : LPCWSTR optional -> LPCWSTR
# hmod : HMODULE optional -> HANDLE
# fdwSound : SND_FLAGS -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f PlaySoundA (ANSI版) — ファイル・リソース・別名からサウンドを再生する(ANSI版)。
使用する型