Win32 API 日本語リファレンス
ホームSystem.Diagnostics.Debug › OutputDebugStringW

OutputDebugStringW

関数
Unicode文字列をデバッガの出力に送信する。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows XP 以降

シグネチャ

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

void OutputDebugStringW(
    LPCWSTR lpOutputString   // optional
);

パラメーター

名前方向説明
lpOutputStringLPCWSTRinoptional表示する null 終端文字列。

戻り値の型: void

公式ドキュメント

文字列をデバッガーに送信して表示します。(Unicode)

解説(Remarks)

重要

この関数を使用するには、アプリケーションに debugapi.h ではなく Windows.h ヘッダーをインクルードする必要があります。

かつてオペレーティングシステムは、OutputDebugStringW を通じて Unicode 文字列を返しませんでした(代わりに ASCII 文字列が返されていました)。OutputDebugStringW に Unicode 文字列を返させるには、デバッガーが WaitForDebugEventEx 関数を呼び出して新しい動作をオプトインする必要があります。これにより、オペレーティングシステムはデバッガーが Unicode をサポートしており、Unicode 文字列の受信を明示的にオプトインしていることを認識します。

アプリケーションにデバッガーがアタッチされておらず、フィルターマスクがそれを許可している場合、システムデバッガーが文字列を表示します。文字列を表示するために、この関数は DbgPrint 関数を呼び出します。Windows Vista より前のバージョンでは、コンテンツはシステムデバッガーによってフィルタリングされませんでした。

アプリケーションにデバッガーがアタッチされておらず、システムデバッガーもアクティブでない場合、OutputDebugString は何も行いません。

OutputDebugStringW は、現在のシステムロケール情報に基づいて指定された文字列を変換し、表示のために OutputDebugStringA に渡します。その結果、一部の Unicode 文字が正しく表示されない場合があります。

アプリケーションは、デバッグ出力を必要最小限に抑え、その使用を有効または無効にする手段をユーザーに提供する必要があります。トレースの詳細については、Event Tracing を参照してください。

Visual Studio は、これらの文字列の表示方法をリビジョン履歴を通じて変更してきました。お使いのバージョンがこれをどのように扱うかの詳細については、Visual Studio のドキュメントを参照してください。

debugapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして OutputDebugString を定義します。エンコーディング中立のエイリアスの使用を、エンコーディング中立ではないコードと混在させると、不一致やコンパイルエラー、ランタイムエラーにつながる可能性があります。詳細については、Conventions for Function Prototypes を参照してください。

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

各言語での呼び出し定義

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

void OutputDebugStringW(
    LPCWSTR lpOutputString   // optional
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern void OutputDebugStringW(
    [MarshalAs(UnmanagedType.LPWStr)] string lpOutputString   // LPCWSTR optional
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Sub OutputDebugStringW(
    <MarshalAs(UnmanagedType.LPWStr)> lpOutputString As String   ' LPCWSTR optional
)
End Sub
' lpOutputString : LPCWSTR optional
Declare PtrSafe Sub OutputDebugStringW Lib "kernel32" ( _
    ByVal lpOutputString 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

OutputDebugStringW = ctypes.windll.kernel32.OutputDebugStringW
OutputDebugStringW.restype = None
OutputDebugStringW.argtypes = [
    wintypes.LPCWSTR,  # lpOutputString : LPCWSTR optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
OutputDebugStringW = Fiddle::Function.new(
  lib['OutputDebugStringW'],
  [
    Fiddle::TYPE_VOIDP,  # lpOutputString : LPCWSTR optional
  ],
  Fiddle::TYPE_VOID)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "kernel32")]
extern "system" {
    fn OutputDebugStringW(
        lpOutputString: *const u16  // LPCWSTR optional
    );
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode)]
public static extern void OutputDebugStringW([MarshalAs(UnmanagedType.LPWStr)] string lpOutputString);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_OutputDebugStringW' -Namespace Win32 -PassThru
# $api::OutputDebugStringW(lpOutputString)
#uselib "KERNEL32.dll"
#func global OutputDebugStringW "OutputDebugStringW" wptr
; OutputDebugStringW lpOutputString
; lpOutputString : LPCWSTR optional -> "wptr"
#uselib "KERNEL32.dll"
#func global OutputDebugStringW "OutputDebugStringW" wstr
; OutputDebugStringW lpOutputString
; lpOutputString : LPCWSTR optional -> "wstr"
; void OutputDebugStringW(LPCWSTR lpOutputString)
#uselib "KERNEL32.dll"
#func global OutputDebugStringW "OutputDebugStringW" wstr
; OutputDebugStringW lpOutputString
; lpOutputString : LPCWSTR optional -> "wstr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procOutputDebugStringW = kernel32.NewProc("OutputDebugStringW")
)

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

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

typedef OutputDebugStringWNative = Void Function(Pointer<Utf16>);
typedef OutputDebugStringWDart = void Function(Pointer<Utf16>);
final OutputDebugStringW = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<OutputDebugStringWNative, OutputDebugStringWDart>('OutputDebugStringW');
// lpOutputString : LPCWSTR optional -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
procedure OutputDebugStringW(
  lpOutputString: PWideChar   // LPCWSTR optional
); stdcall;
  external 'KERNEL32.dll' name 'OutputDebugStringW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "OutputDebugStringW"
  c_OutputDebugStringW :: CWString -> IO ()
-- lpOutputString : LPCWSTR optional -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let outputdebugstringw =
  foreign "OutputDebugStringW"
    ((ptr uint16_t) @-> returning void)
(* lpOutputString : LPCWSTR optional -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("OutputDebugStringW" output-debug-string-w :convention :stdcall) :void
  (lp-output-string (:string :encoding :utf-16le)))   ; LPCWSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $OutputDebugStringW = Win32::API::More->new('KERNEL32',
    'void OutputDebugStringW(LPCWSTR lpOutputString)');
# my $ret = $OutputDebugStringW->Call($lpOutputString);
# lpOutputString : LPCWSTR optional -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い