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

SymGetSourceFileW

関数
シンボルストアからソースファイルを取得する(Unicode版)。
DLLdbghelp.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり

シグネチャ

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

BOOL SymGetSourceFileW(
    HANDLE hProcess,
    ULONGLONG Base,
    LPCWSTR Params,   // optional
    LPCWSTR FileSpec,
    LPWSTR FilePath,
    DWORD Size
);

パラメーター

名前方向説明
hProcessHANDLEinプロセスへのハンドル。このハンドルは、事前に SymInitialize 関数に渡されている必要があります。
BaseULONGLONGinモジュールのベースアドレス。
ParamsLPCWSTRinoptionalこのパラメーターは使用されません。
FileSpecLPCWSTRinソースファイルの名前。
FilePathLPWSTRoutソースファイルの完全修飾パスを受け取る バッファーへのポインター。
SizeDWORDinFilePath バッファーのサイズ(文字数単位)。

戻り値の型: BOOL

公式ドキュメント

SymGetSourceFileW (Unicode) 関数は、指定されたソースファイルをソースサーバーから取得します。

戻り値

関数が成功した場合、戻り値は TRUE です。

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

解説(Remarks)

ソースファイルを受け取るディレクトリを制御するには、SymSetHomeDirectory 関数を使用します。

この関数を含むすべての DbgHelp 関数はシングルスレッドです。そのため、複数のスレッドからこの関数を呼び出すと、予期しない動作やメモリ破損が発生する可能性があります。これを回避するには、複数のスレッドからこの関数への同時呼び出しをすべて同期する必要があります。

この関数の Unicode バージョンを呼び出すには、DBGHELP_TRANSLATE_TCHAR を定義します。

メモ

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

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

各言語での呼び出し定義

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

BOOL SymGetSourceFileW(
    HANDLE hProcess,
    ULONGLONG Base,
    LPCWSTR Params,   // optional
    LPCWSTR FileSpec,
    LPWSTR FilePath,
    DWORD Size
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("dbghelp.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool SymGetSourceFileW(
    IntPtr hProcess,   // HANDLE
    ulong Base,   // ULONGLONG
    [MarshalAs(UnmanagedType.LPWStr)] string Params,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string FileSpec,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder FilePath,   // LPWSTR out
    uint Size   // DWORD
);
<DllImport("dbghelp.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SymGetSourceFileW(
    hProcess As IntPtr,   ' HANDLE
    Base As ULong,   ' ULONGLONG
    <MarshalAs(UnmanagedType.LPWStr)> Params As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> FileSpec As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> FilePath As System.Text.StringBuilder,   ' LPWSTR out
    Size As UInteger   ' DWORD
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' hProcess : HANDLE
' Base : ULONGLONG
' Params : LPCWSTR optional
' FileSpec : LPCWSTR
' FilePath : LPWSTR out
' Size : DWORD
Declare PtrSafe Function SymGetSourceFileW Lib "dbghelp" ( _
    ByVal hProcess As LongPtr, _
    ByVal Base As LongLong, _
    ByVal Params As LongPtr, _
    ByVal FileSpec As LongPtr, _
    ByVal FilePath As LongPtr, _
    ByVal Size 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

SymGetSourceFileW = ctypes.windll.dbghelp.SymGetSourceFileW
SymGetSourceFileW.restype = wintypes.BOOL
SymGetSourceFileW.argtypes = [
    wintypes.HANDLE,  # hProcess : HANDLE
    ctypes.c_ulonglong,  # Base : ULONGLONG
    wintypes.LPCWSTR,  # Params : LPCWSTR optional
    wintypes.LPCWSTR,  # FileSpec : LPCWSTR
    wintypes.LPWSTR,  # FilePath : LPWSTR out
    wintypes.DWORD,  # Size : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('dbghelp.dll')
SymGetSourceFileW = Fiddle::Function.new(
  lib['SymGetSourceFileW'],
  [
    Fiddle::TYPE_VOIDP,  # hProcess : HANDLE
    -Fiddle::TYPE_LONG_LONG,  # Base : ULONGLONG
    Fiddle::TYPE_VOIDP,  # Params : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # FileSpec : LPCWSTR
    Fiddle::TYPE_VOIDP,  # FilePath : LPWSTR out
    -Fiddle::TYPE_INT,  # Size : DWORD
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "dbghelp")]
extern "system" {
    fn SymGetSourceFileW(
        hProcess: *mut core::ffi::c_void,  // HANDLE
        Base: u64,  // ULONGLONG
        Params: *const u16,  // LPCWSTR optional
        FileSpec: *const u16,  // LPCWSTR
        FilePath: *mut u16,  // LPWSTR out
        Size: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("dbghelp.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SymGetSourceFileW(IntPtr hProcess, ulong Base, [MarshalAs(UnmanagedType.LPWStr)] string Params, [MarshalAs(UnmanagedType.LPWStr)] string FileSpec, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder FilePath, uint Size);
"@
$api = Add-Type -MemberDefinition $sig -Name 'dbghelp_SymGetSourceFileW' -Namespace Win32 -PassThru
# $api::SymGetSourceFileW(hProcess, Base, Params, FileSpec, FilePath, Size)
#uselib "dbghelp.dll"
#func global SymGetSourceFileW "SymGetSourceFileW" wptr, wptr, wptr, wptr, wptr, wptr
; SymGetSourceFileW hProcess, Base, Params, FileSpec, varptr(FilePath), Size   ; 戻り値は stat
; hProcess : HANDLE -> "wptr"
; Base : ULONGLONG -> "wptr"
; Params : LPCWSTR optional -> "wptr"
; FileSpec : LPCWSTR -> "wptr"
; FilePath : LPWSTR out -> "wptr"
; Size : DWORD -> "wptr"
; ※HSP3.7は int64 引数(64bit値渡し)に非対応です。
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "dbghelp.dll"
#cfunc global SymGetSourceFileW "SymGetSourceFileW" sptr, int64, wstr, wstr, var, int
; res = SymGetSourceFileW(hProcess, Base, Params, FileSpec, FilePath, Size)
; hProcess : HANDLE -> "sptr"
; Base : ULONGLONG -> "int64"
; Params : LPCWSTR optional -> "wstr"
; FileSpec : LPCWSTR -> "wstr"
; FilePath : LPWSTR out -> "var"
; Size : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
; ※int64 引数の DLL 値渡しは x64 ランタイム(hsp3_64)のみ対応(x86 は未対応)。
出力引数:
; BOOL SymGetSourceFileW(HANDLE hProcess, ULONGLONG Base, LPCWSTR Params, LPCWSTR FileSpec, LPWSTR FilePath, DWORD Size)
#uselib "dbghelp.dll"
#cfunc global SymGetSourceFileW "SymGetSourceFileW" intptr, int64, wstr, wstr, var, int
; res = SymGetSourceFileW(hProcess, Base, Params, FileSpec, FilePath, Size)
; hProcess : HANDLE -> "intptr"
; Base : ULONGLONG -> "int64"
; Params : LPCWSTR optional -> "wstr"
; FileSpec : LPCWSTR -> "wstr"
; FilePath : LPWSTR out -> "var"
; Size : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	dbghelp = windows.NewLazySystemDLL("dbghelp.dll")
	procSymGetSourceFileW = dbghelp.NewProc("SymGetSourceFileW")
)

// hProcess (HANDLE), Base (ULONGLONG), Params (LPCWSTR optional), FileSpec (LPCWSTR), FilePath (LPWSTR out), Size (DWORD)
r1, _, err := procSymGetSourceFileW.Call(
	uintptr(hProcess),
	uintptr(Base),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(Params))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(FileSpec))),
	uintptr(FilePath),
	uintptr(Size),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SymGetSourceFileW(
  hProcess: THandle;   // HANDLE
  Base: UInt64;   // ULONGLONG
  Params: PWideChar;   // LPCWSTR optional
  FileSpec: PWideChar;   // LPCWSTR
  FilePath: PWideChar;   // LPWSTR out
  Size: DWORD   // DWORD
): BOOL; stdcall;
  external 'dbghelp.dll' name 'SymGetSourceFileW';
result := DllCall("dbghelp\SymGetSourceFileW"
    , "Ptr", hProcess   ; HANDLE
    , "Int64", Base   ; ULONGLONG
    , "WStr", Params   ; LPCWSTR optional
    , "WStr", FileSpec   ; LPCWSTR
    , "Ptr", FilePath   ; LPWSTR out
    , "UInt", Size   ; DWORD
    , "Int")   ; return: BOOL
●SymGetSourceFileW(hProcess, Base, Params, FileSpec, FilePath, Size) = DLL("dbghelp.dll", "bool SymGetSourceFileW(void*, qword, char*, char*, char*, dword)")
# 呼び出し: SymGetSourceFileW(hProcess, Base, Params, FileSpec, FilePath, Size)
# hProcess : HANDLE -> "void*"
# Base : ULONGLONG -> "qword"
# Params : LPCWSTR optional -> "char*"
# FileSpec : LPCWSTR -> "char*"
# FilePath : LPWSTR out -> "char*"
# Size : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

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

typedef SymGetSourceFileWNative = Int32 Function(Pointer<Void>, Uint64, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Uint32);
typedef SymGetSourceFileWDart = int Function(Pointer<Void>, int, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, int);
final SymGetSourceFileW = DynamicLibrary.open('dbghelp.dll')
    .lookupFunction<SymGetSourceFileWNative, SymGetSourceFileWDart>('SymGetSourceFileW');
// hProcess : HANDLE -> Pointer<Void>
// Base : ULONGLONG -> Uint64
// Params : LPCWSTR optional -> Pointer<Utf16>
// FileSpec : LPCWSTR -> Pointer<Utf16>
// FilePath : LPWSTR out -> Pointer<Utf16>
// Size : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SymGetSourceFileW(
  hProcess: THandle;   // HANDLE
  Base: UInt64;   // ULONGLONG
  Params: PWideChar;   // LPCWSTR optional
  FileSpec: PWideChar;   // LPCWSTR
  FilePath: PWideChar;   // LPWSTR out
  Size: DWORD   // DWORD
): BOOL; stdcall;
  external 'dbghelp.dll' name 'SymGetSourceFileW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SymGetSourceFileW"
  c_SymGetSourceFileW :: Ptr () -> Word64 -> CWString -> CWString -> CWString -> Word32 -> IO CInt
-- hProcess : HANDLE -> Ptr ()
-- Base : ULONGLONG -> Word64
-- Params : LPCWSTR optional -> CWString
-- FileSpec : LPCWSTR -> CWString
-- FilePath : LPWSTR out -> CWString
-- Size : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let symgetsourcefilew =
  foreign "SymGetSourceFileW"
    ((ptr void) @-> uint64_t @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> returning int32_t)
(* hProcess : HANDLE -> (ptr void) *)
(* Base : ULONGLONG -> uint64_t *)
(* Params : LPCWSTR optional -> (ptr uint16_t) *)
(* FileSpec : LPCWSTR -> (ptr uint16_t) *)
(* FilePath : LPWSTR out -> (ptr uint16_t) *)
(* Size : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library dbghelp (t "dbghelp.dll"))
(cffi:use-foreign-library dbghelp)

(cffi:defcfun ("SymGetSourceFileW" sym-get-source-file-w :convention :stdcall) :int32
  (h-process :pointer)   ; HANDLE
  (base :uint64)   ; ULONGLONG
  (params (:string :encoding :utf-16le))   ; LPCWSTR optional
  (file-spec (:string :encoding :utf-16le))   ; LPCWSTR
  (file-path :pointer)   ; LPWSTR out
  (size :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SymGetSourceFileW = Win32::API::More->new('dbghelp',
    'BOOL SymGetSourceFileW(HANDLE hProcess, UINT64 Base, LPCWSTR Params, LPCWSTR FileSpec, LPWSTR FilePath, DWORD Size)');
# my $ret = $SymGetSourceFileW->Call($hProcess, $Base, $Params, $FileSpec, $FilePath, $Size);
# hProcess : HANDLE -> HANDLE
# Base : ULONGLONG -> UINT64
# Params : LPCWSTR optional -> LPCWSTR
# FileSpec : LPCWSTR -> LPCWSTR
# FilePath : LPWSTR out -> LPWSTR
# Size : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。