Win32 API 日本語リファレンス
ホームSystem.Threading › AvSetMmThreadCharacteristicsW

AvSetMmThreadCharacteristicsW

関数
マルチメディア処理向けにスレッド特性を登録する(Unicode版)。
DLLAVRT.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

// AVRT.dll  (Unicode / -W)
#include <windows.h>

AVRT_TASK_HANDLE AvSetMmThreadCharacteristicsW(
    LPCWSTR TaskName,
    DWORD* TaskIndex
);

パラメーター

名前方向説明
TaskNameLPCWSTRin実行するタスクの名前。この名前は、次のキー HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile\Tasks のサブキーのいずれかの名前と一致する必要があります。
TaskIndexDWORD*inout一意のタスク識別子。この関数を初めて呼び出すときは、入力時にこの値を 0 にする必要があります。インデックス値は出力として返され、以降の呼び出しで入力として使用できます。

戻り値の型: AVRT_TASK_HANDLE

公式ドキュメント

呼び出し元スレッドを指定されたタスクに関連付けます。(Unicode)

戻り値

関数が成功した場合、タスクへのハンドルを返します。

関数が失敗した場合は 0 を返します。拡張エラー情報を取得するには、GetLastError を呼び出します。

発生し得るエラーコードは次のとおりです。

戻り値 説明
ERROR_INVALID_TASK_INDEX
TaskIndex が最初の呼び出しで 0 でないか、または(以降の呼び出しで)認識されない値です。
ERROR_INVALID_TASK_NAME
指定されたタスクが、レジストリに格納されているいずれのタスクとも一致しません。
ERROR_PRIVILEGE_NOT_HELD
呼び出し元に十分な特権がありません。

解説(Remarks)

タスクが完了したら、AvRevertMmThreadCharacteristics 関数を呼び出します。

メモ

avrt.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして AvSetMmThreadCharacteristics を定義します。エンコーディング非依存のエイリアスを、エンコーディング非依存でないコードと混在させて使用すると、不一致が生じてコンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、関数プロトタイプの規約を参照してください。

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

各言語での呼び出し定義

// AVRT.dll  (Unicode / -W)
#include <windows.h>

AVRT_TASK_HANDLE AvSetMmThreadCharacteristicsW(
    LPCWSTR TaskName,
    DWORD* TaskIndex
);
[DllImport("AVRT.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern AVRT_TASK_HANDLE AvSetMmThreadCharacteristicsW(
    [MarshalAs(UnmanagedType.LPWStr)] string TaskName,   // LPCWSTR
    ref uint TaskIndex   // DWORD* in/out
);
<DllImport("AVRT.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function AvSetMmThreadCharacteristicsW(
    <MarshalAs(UnmanagedType.LPWStr)> TaskName As String,   ' LPCWSTR
    ByRef TaskIndex As UInteger   ' DWORD* in/out
) As AVRT_TASK_HANDLE
End Function
' TaskName : LPCWSTR
' TaskIndex : DWORD* in/out
Declare PtrSafe Function AvSetMmThreadCharacteristicsW Lib "avrt" ( _
    ByVal TaskName As LongPtr, _
    ByRef TaskIndex As Long) As LongPtr
' 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

AvSetMmThreadCharacteristicsW = ctypes.windll.avrt.AvSetMmThreadCharacteristicsW
AvSetMmThreadCharacteristicsW.restype = ctypes.c_void_p
AvSetMmThreadCharacteristicsW.argtypes = [
    wintypes.LPCWSTR,  # TaskName : LPCWSTR
    ctypes.POINTER(wintypes.DWORD),  # TaskIndex : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('AVRT.dll')
AvSetMmThreadCharacteristicsW = Fiddle::Function.new(
  lib['AvSetMmThreadCharacteristicsW'],
  [
    Fiddle::TYPE_VOIDP,  # TaskName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # TaskIndex : DWORD* in/out
  ],
  Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "avrt")]
extern "system" {
    fn AvSetMmThreadCharacteristicsW(
        TaskName: *const u16,  // LPCWSTR
        TaskIndex: *mut u32  // DWORD* in/out
    ) -> AVRT_TASK_HANDLE;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("AVRT.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern AVRT_TASK_HANDLE AvSetMmThreadCharacteristicsW([MarshalAs(UnmanagedType.LPWStr)] string TaskName, ref uint TaskIndex);
"@
$api = Add-Type -MemberDefinition $sig -Name 'AVRT_AvSetMmThreadCharacteristicsW' -Namespace Win32 -PassThru
# $api::AvSetMmThreadCharacteristicsW(TaskName, TaskIndex)
#uselib "AVRT.dll"
#func global AvSetMmThreadCharacteristicsW "AvSetMmThreadCharacteristicsW" wptr, wptr
; AvSetMmThreadCharacteristicsW TaskName, varptr(TaskIndex)   ; 戻り値は stat
; TaskName : LPCWSTR -> "wptr"
; TaskIndex : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "AVRT.dll"
#cfunc global AvSetMmThreadCharacteristicsW "AvSetMmThreadCharacteristicsW" wstr, var
; res = AvSetMmThreadCharacteristicsW(TaskName, TaskIndex)
; TaskName : LPCWSTR -> "wstr"
; TaskIndex : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; AVRT_TASK_HANDLE AvSetMmThreadCharacteristicsW(LPCWSTR TaskName, DWORD* TaskIndex)
#uselib "AVRT.dll"
#cfunc global AvSetMmThreadCharacteristicsW "AvSetMmThreadCharacteristicsW" wstr, var
; res = AvSetMmThreadCharacteristicsW(TaskName, TaskIndex)
; TaskName : LPCWSTR -> "wstr"
; TaskIndex : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	avrt = windows.NewLazySystemDLL("AVRT.dll")
	procAvSetMmThreadCharacteristicsW = avrt.NewProc("AvSetMmThreadCharacteristicsW")
)

// TaskName (LPCWSTR), TaskIndex (DWORD* in/out)
r1, _, err := procAvSetMmThreadCharacteristicsW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(TaskName))),
	uintptr(TaskIndex),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // AVRT_TASK_HANDLE
function AvSetMmThreadCharacteristicsW(
  TaskName: PWideChar;   // LPCWSTR
  TaskIndex: Pointer   // DWORD* in/out
): AVRT_TASK_HANDLE; stdcall;
  external 'AVRT.dll' name 'AvSetMmThreadCharacteristicsW';
result := DllCall("AVRT\AvSetMmThreadCharacteristicsW"
    , "WStr", TaskName   ; LPCWSTR
    , "Ptr", TaskIndex   ; DWORD* in/out
    , "Ptr")   ; return: AVRT_TASK_HANDLE
●AvSetMmThreadCharacteristicsW(TaskName, TaskIndex) = DLL("AVRT.dll", "void* AvSetMmThreadCharacteristicsW(char*, void*)")
# 呼び出し: AvSetMmThreadCharacteristicsW(TaskName, TaskIndex)
# TaskName : LPCWSTR -> "char*"
# TaskIndex : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "avrt" fn AvSetMmThreadCharacteristicsW(
    TaskName: [*c]const u16, // LPCWSTR
    TaskIndex: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) AVRT_TASK_HANDLE;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc AvSetMmThreadCharacteristicsW(
    TaskName: WideCString,  # LPCWSTR
    TaskIndex: ptr uint32  # DWORD* in/out
): AVRT_TASK_HANDLE {.importc: "AvSetMmThreadCharacteristicsW", stdcall, dynlib: "AVRT.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "avrt");
extern(Windows)
AVRT_TASK_HANDLE AvSetMmThreadCharacteristicsW(
    const(wchar)* TaskName,   // LPCWSTR
    uint* TaskIndex   // DWORD* in/out
);
ccall((:AvSetMmThreadCharacteristicsW, "AVRT.dll"), stdcall, AVRT_TASK_HANDLE,
      (Cwstring, Ptr{UInt32}),
      TaskName, TaskIndex)
# TaskName : LPCWSTR -> Cwstring
# TaskIndex : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
AVRT_TASK_HANDLE AvSetMmThreadCharacteristicsW(
    const uint16_t* TaskName,
    uint32_t* TaskIndex);
]]
local avrt = ffi.load("avrt")
-- avrt.AvSetMmThreadCharacteristicsW(TaskName, TaskIndex)
-- TaskName : LPCWSTR
-- TaskIndex : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('AVRT.dll');
const AvSetMmThreadCharacteristicsW = lib.func('__stdcall', 'AvSetMmThreadCharacteristicsW', 'AVRT_TASK_HANDLE', ['str16', 'uint32_t *']);
// AvSetMmThreadCharacteristicsW(TaskName, TaskIndex)
// TaskName : LPCWSTR -> 'str16'
// TaskIndex : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("AVRT.dll", {
  AvSetMmThreadCharacteristicsW: { parameters: ["buffer", "pointer"], result: "pointer" },
});
// lib.symbols.AvSetMmThreadCharacteristicsW(TaskName, TaskIndex)
// TaskName : LPCWSTR -> "buffer"
// TaskIndex : DWORD* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
AVRT_TASK_HANDLE AvSetMmThreadCharacteristicsW(
    const uint16_t* TaskName,
    uint32_t* TaskIndex);
C, "AVRT.dll");
// $ffi->AvSetMmThreadCharacteristicsW(TaskName, TaskIndex);
// TaskName : LPCWSTR
// TaskIndex : DWORD* in/out
// 構造体/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 Avrt extends StdCallLibrary {
    Avrt INSTANCE = Native.load("avrt", Avrt.class, W32APIOptions.UNICODE_OPTIONS);
    AVRT_TASK_HANDLE AvSetMmThreadCharacteristicsW(
        WString TaskName,   // LPCWSTR
        IntByReference TaskIndex   // DWORD* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("avrt")]
lib LibAVRT
  fun AvSetMmThreadCharacteristicsW = AvSetMmThreadCharacteristicsW(
    TaskName : UInt16*,   # LPCWSTR
    TaskIndex : UInt32*   # DWORD* in/out
  ) : AVRT_TASK_HANDLE
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef AvSetMmThreadCharacteristicsWNative = AVRT_TASK_HANDLE Function(Pointer<Utf16>, Pointer<Uint32>);
typedef AvSetMmThreadCharacteristicsWDart = int Function(Pointer<Utf16>, Pointer<Uint32>);
final AvSetMmThreadCharacteristicsW = DynamicLibrary.open('AVRT.dll')
    .lookupFunction<AvSetMmThreadCharacteristicsWNative, AvSetMmThreadCharacteristicsWDart>('AvSetMmThreadCharacteristicsW');
// TaskName : LPCWSTR -> Pointer<Utf16>
// TaskIndex : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function AvSetMmThreadCharacteristicsW(
  TaskName: PWideChar;   // LPCWSTR
  TaskIndex: Pointer   // DWORD* in/out
): AVRT_TASK_HANDLE; stdcall;
  external 'AVRT.dll' name 'AvSetMmThreadCharacteristicsW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "AvSetMmThreadCharacteristicsW"
  c_AvSetMmThreadCharacteristicsW :: CWString -> Ptr Word32 -> IO AVRT_TASK_HANDLE
-- TaskName : LPCWSTR -> CWString
-- TaskIndex : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
-- ※値渡し構造体は GHC FFI で直接渡せません。Ptr で渡すラッパ(C側)経由にしてください。
open Ctypes
open Foreign

let avsetmmthreadcharacteristicsw =
  foreign "AvSetMmThreadCharacteristicsW"
    ((ptr uint16_t) @-> (ptr uint32_t) @-> returning AVRT_TASK_HANDLE)
(* TaskName : LPCWSTR -> (ptr uint16_t) *)
(* TaskIndex : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library avrt (t "AVRT.dll"))
(cffi:use-foreign-library avrt)

(cffi:defcfun ("AvSetMmThreadCharacteristicsW" av-set-mm-thread-characteristics-w :convention :stdcall) (:struct avrt-task-handle)
  (task-name (:string :encoding :utf-16le))   ; LPCWSTR
  (task-index :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $AvSetMmThreadCharacteristicsW = Win32::API::More->new('AVRT',
    'LPVOID AvSetMmThreadCharacteristicsW(LPCWSTR TaskName, LPVOID TaskIndex)');
# my $ret = $AvSetMmThreadCharacteristicsW->Call($TaskName, $TaskIndex);
# TaskName : LPCWSTR -> LPCWSTR
# TaskIndex : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
使用する型