Win32 API 日本語リファレンス
ホームUI.Shell › UrlUnescapeA

UrlUnescapeA

関数
URL中のエスケープ表記を元の文字にデコードする。
DLLSHLWAPI.dll文字セットANSI (-A)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// SHLWAPI.dll  (ANSI / -A)
#include <windows.h>

HRESULT UrlUnescapeA(
    LPSTR pszUrl,
    LPSTR pszUnescaped,   // optional
    DWORD* pcchUnescaped,   // optional
    DWORD dwFlags
);

パラメーター

名前方向説明
pszUrlLPSTRinoutURL を表す null 終端文字列へのポインター。dwFlagsURL_UNESCAPE_INPLACE が設定されている場合、変換後の文字列はこのパラメーターを通じて返されます。
pszUnescapedLPSTRoutoptionalpszURL のアンエスケープ済みバージョンを格納する null 終端文字列を受け取るバッファーへのポインター。dwFlagsURL_UNESCAPE_INPLACE が設定されている場合、このパラメーターは無視されます。
pcchUnescapedDWORD*inoutoptionalpszUnescaped が指すバッファー内の文字数。入力時、pcchUnescaped が指す値にはバッファーのサイズを設定します。関数が成功コードを返し、かつ URL_UNESCAPE_INPLACE フラグが設定されていない場合、pcchUnescaped が指す値には、終端の NULL 文字を除いた、そのバッファーに書き込まれた文字数が設定されます。E_POINTER エラーコードが返された場合、バッファーが小さすぎたことを意味し、pcchUnescaped が指す値にはバッファーが格納できる必要のある文字数が設定されます。その他のエラーが返された場合、pcchUnescaped が指す値は未定義です。
dwFlagsDWORDin

どの文字をアンエスケープするかを制御するフラグ。次のフラグの組み合わせを指定できます。

URL_DONT_UNESCAPE_EXTRA_INFO

または ? の文字、およびそれらに続く文字列中のすべての文字を変換しません。

URL_UNESCAPE_AS_UTF8

Windows 8 で導入されましたURL_ESCAPE_AS_UTF8 フラグを使用してエンコードされた URL をデコードします。

URL_UNESCAPE_INPLACE

変換後の文字列を pszUnescaped ではなく pszURL を使用して返します。

戻り値の型: HRESULT

公式ドキュメント

エスケープシーケンスを通常の文字に戻します。(ANSI)

戻り値

型: HRESULT

成功した場合は S_OK を返します。URL_UNESCAPE_INPLACE フラグが設定されていない場合、pcchUnescaped が指す値には pszUnescaped が指す出力バッファー内の文字数が設定されます。URL_UNESCAPE_INPLACE フラグが設定されておらず、出力バッファーが小さすぎる場合は E_POINTER を返します。この場合、pcchUnescaped パラメーターには必要なバッファーサイズが設定されます。それ以外の場合は、標準のエラー値を返します。

解説(Remarks)

エスケープシーケンスは "%xy" の形式を持ちます。

入力文字列は INTERNET_MAX_URL_LENGTH を超えることはできません。

メモ

shlwapi.h ヘッダーは UrlUnescape をエイリアスとして定義しており、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)

各言語での呼び出し定義

// SHLWAPI.dll  (ANSI / -A)
#include <windows.h>

HRESULT UrlUnescapeA(
    LPSTR pszUrl,
    LPSTR pszUnescaped,   // optional
    DWORD* pcchUnescaped,   // optional
    DWORD dwFlags
);
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern int UrlUnescapeA(
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszUrl,   // LPSTR in/out
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszUnescaped,   // LPSTR optional, out
    IntPtr pcchUnescaped,   // DWORD* optional, in/out
    uint dwFlags   // DWORD
);
<DllImport("SHLWAPI.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function UrlUnescapeA(
    <MarshalAs(UnmanagedType.LPStr)> pszUrl As System.Text.StringBuilder,   ' LPSTR in/out
    <MarshalAs(UnmanagedType.LPStr)> pszUnescaped As System.Text.StringBuilder,   ' LPSTR optional, out
    pcchUnescaped As IntPtr,   ' DWORD* optional, in/out
    dwFlags As UInteger   ' DWORD
) As Integer
End Function
' pszUrl : LPSTR in/out
' pszUnescaped : LPSTR optional, out
' pcchUnescaped : DWORD* optional, in/out
' dwFlags : DWORD
Declare PtrSafe Function UrlUnescapeA Lib "shlwapi" ( _
    ByVal pszUrl As String, _
    ByVal pszUnescaped As String, _
    ByVal pcchUnescaped As LongPtr, _
    ByVal dwFlags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

UrlUnescapeA = ctypes.windll.shlwapi.UrlUnescapeA
UrlUnescapeA.restype = ctypes.c_int
UrlUnescapeA.argtypes = [
    wintypes.LPSTR,  # pszUrl : LPSTR in/out
    wintypes.LPSTR,  # pszUnescaped : LPSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcchUnescaped : DWORD* optional, in/out
    wintypes.DWORD,  # dwFlags : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHLWAPI.dll')
UrlUnescapeA = Fiddle::Function.new(
  lib['UrlUnescapeA'],
  [
    Fiddle::TYPE_VOIDP,  # pszUrl : LPSTR in/out
    Fiddle::TYPE_VOIDP,  # pszUnescaped : LPSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcchUnescaped : DWORD* optional, in/out
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
  ],
  Fiddle::TYPE_INT)
#[link(name = "shlwapi")]
extern "system" {
    fn UrlUnescapeA(
        pszUrl: *mut u8,  // LPSTR in/out
        pszUnescaped: *mut u8,  // LPSTR optional, out
        pcchUnescaped: *mut u32,  // DWORD* optional, in/out
        dwFlags: u32  // DWORD
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi)]
public static extern int UrlUnescapeA([MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszUrl, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder pszUnescaped, IntPtr pcchUnescaped, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_UrlUnescapeA' -Namespace Win32 -PassThru
# $api::UrlUnescapeA(pszUrl, pszUnescaped, pcchUnescaped, dwFlags)
#uselib "SHLWAPI.dll"
#func global UrlUnescapeA "UrlUnescapeA" sptr, sptr, sptr, sptr
; UrlUnescapeA varptr(pszUrl), varptr(pszUnescaped), varptr(pcchUnescaped), dwFlags   ; 戻り値は stat
; pszUrl : LPSTR in/out -> "sptr"
; pszUnescaped : LPSTR optional, out -> "sptr"
; pcchUnescaped : DWORD* optional, in/out -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SHLWAPI.dll"
#cfunc global UrlUnescapeA "UrlUnescapeA" var, var, var, int
; res = UrlUnescapeA(pszUrl, pszUnescaped, pcchUnescaped, dwFlags)
; pszUrl : LPSTR in/out -> "var"
; pszUnescaped : LPSTR optional, out -> "var"
; pcchUnescaped : DWORD* optional, in/out -> "var"
; dwFlags : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HRESULT UrlUnescapeA(LPSTR pszUrl, LPSTR pszUnescaped, DWORD* pcchUnescaped, DWORD dwFlags)
#uselib "SHLWAPI.dll"
#cfunc global UrlUnescapeA "UrlUnescapeA" var, var, var, int
; res = UrlUnescapeA(pszUrl, pszUnescaped, pcchUnescaped, dwFlags)
; pszUrl : LPSTR in/out -> "var"
; pszUnescaped : LPSTR optional, out -> "var"
; pcchUnescaped : DWORD* optional, in/out -> "var"
; dwFlags : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procUrlUnescapeA = shlwapi.NewProc("UrlUnescapeA")
)

// pszUrl (LPSTR in/out), pszUnescaped (LPSTR optional, out), pcchUnescaped (DWORD* optional, in/out), dwFlags (DWORD)
r1, _, err := procUrlUnescapeA.Call(
	uintptr(pszUrl),
	uintptr(pszUnescaped),
	uintptr(pcchUnescaped),
	uintptr(dwFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HRESULT
function UrlUnescapeA(
  pszUrl: PAnsiChar;   // LPSTR in/out
  pszUnescaped: PAnsiChar;   // LPSTR optional, out
  pcchUnescaped: Pointer;   // DWORD* optional, in/out
  dwFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'SHLWAPI.dll' name 'UrlUnescapeA';
result := DllCall("SHLWAPI\UrlUnescapeA"
    , "Ptr", pszUrl   ; LPSTR in/out
    , "Ptr", pszUnescaped   ; LPSTR optional, out
    , "Ptr", pcchUnescaped   ; DWORD* optional, in/out
    , "UInt", dwFlags   ; DWORD
    , "Int")   ; return: HRESULT
●UrlUnescapeA(pszUrl, pszUnescaped, pcchUnescaped, dwFlags) = DLL("SHLWAPI.dll", "int UrlUnescapeA(char*, char*, void*, dword)")
# 呼び出し: UrlUnescapeA(pszUrl, pszUnescaped, pcchUnescaped, dwFlags)
# pszUrl : LPSTR in/out -> "char*"
# pszUnescaped : LPSTR optional, out -> "char*"
# pcchUnescaped : DWORD* optional, in/out -> "void*"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "shlwapi" fn UrlUnescapeA(
    pszUrl: [*c]u8, // LPSTR in/out
    pszUnescaped: [*c]u8, // LPSTR optional, out
    pcchUnescaped: [*c]u32, // DWORD* optional, in/out
    dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) i32;
proc UrlUnescapeA(
    pszUrl: ptr char,  # LPSTR in/out
    pszUnescaped: ptr char,  # LPSTR optional, out
    pcchUnescaped: ptr uint32,  # DWORD* optional, in/out
    dwFlags: uint32  # DWORD
): int32 {.importc: "UrlUnescapeA", stdcall, dynlib: "SHLWAPI.dll".}
pragma(lib, "shlwapi");
extern(Windows)
int UrlUnescapeA(
    char* pszUrl,   // LPSTR in/out
    char* pszUnescaped,   // LPSTR optional, out
    uint* pcchUnescaped,   // DWORD* optional, in/out
    uint dwFlags   // DWORD
);
ccall((:UrlUnescapeA, "SHLWAPI.dll"), stdcall, Int32,
      (Ptr{UInt8}, Ptr{UInt8}, Ptr{UInt32}, UInt32),
      pszUrl, pszUnescaped, pcchUnescaped, dwFlags)
# pszUrl : LPSTR in/out -> Ptr{UInt8}
# pszUnescaped : LPSTR optional, out -> Ptr{UInt8}
# pcchUnescaped : DWORD* optional, in/out -> Ptr{UInt32}
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t UrlUnescapeA(
    char* pszUrl,
    char* pszUnescaped,
    uint32_t* pcchUnescaped,
    uint32_t dwFlags);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.UrlUnescapeA(pszUrl, pszUnescaped, pcchUnescaped, dwFlags)
-- pszUrl : LPSTR in/out
-- pszUnescaped : LPSTR optional, out
-- pcchUnescaped : DWORD* optional, in/out
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const UrlUnescapeA = lib.func('__stdcall', 'UrlUnescapeA', 'int32_t', ['char *', 'char *', 'uint32_t *', 'uint32_t']);
// UrlUnescapeA(pszUrl, pszUnescaped, pcchUnescaped, dwFlags)
// pszUrl : LPSTR in/out -> 'char *'
// pszUnescaped : LPSTR optional, out -> 'char *'
// pcchUnescaped : DWORD* optional, in/out -> 'uint32_t *'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHLWAPI.dll", {
  UrlUnescapeA: { parameters: ["buffer", "buffer", "pointer", "u32"], result: "i32" },
});
// lib.symbols.UrlUnescapeA(pszUrl, pszUnescaped, pcchUnescaped, dwFlags)
// pszUrl : LPSTR in/out -> "buffer"
// pszUnescaped : LPSTR optional, out -> "buffer"
// pcchUnescaped : DWORD* optional, in/out -> "pointer"
// dwFlags : DWORD -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t UrlUnescapeA(
    char* pszUrl,
    char* pszUnescaped,
    uint32_t* pcchUnescaped,
    uint32_t dwFlags);
C, "SHLWAPI.dll");
// $ffi->UrlUnescapeA(pszUrl, pszUnescaped, pcchUnescaped, dwFlags);
// pszUrl : LPSTR in/out
// pszUnescaped : LPSTR optional, out
// pcchUnescaped : DWORD* optional, in/out
// dwFlags : 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 Shlwapi extends StdCallLibrary {
    Shlwapi INSTANCE = Native.load("shlwapi", Shlwapi.class, W32APIOptions.ASCII_OPTIONS);
    int UrlUnescapeA(
        byte[] pszUrl,   // LPSTR in/out
        byte[] pszUnescaped,   // LPSTR optional, out
        IntByReference pcchUnescaped,   // DWORD* optional, in/out
        int dwFlags   // DWORD
    );
}
@[Link("shlwapi")]
lib LibSHLWAPI
  fun UrlUnescapeA = UrlUnescapeA(
    pszUrl : UInt8*,   # LPSTR in/out
    pszUnescaped : UInt8*,   # LPSTR optional, out
    pcchUnescaped : UInt32*,   # DWORD* optional, in/out
    dwFlags : 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 UrlUnescapeANative = Int32 Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Uint32>, Uint32);
typedef UrlUnescapeADart = int Function(Pointer<Utf8>, Pointer<Utf8>, Pointer<Uint32>, int);
final UrlUnescapeA = DynamicLibrary.open('SHLWAPI.dll')
    .lookupFunction<UrlUnescapeANative, UrlUnescapeADart>('UrlUnescapeA');
// pszUrl : LPSTR in/out -> Pointer<Utf8>
// pszUnescaped : LPSTR optional, out -> Pointer<Utf8>
// pcchUnescaped : DWORD* optional, in/out -> Pointer<Uint32>
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function UrlUnescapeA(
  pszUrl: PAnsiChar;   // LPSTR in/out
  pszUnescaped: PAnsiChar;   // LPSTR optional, out
  pcchUnescaped: Pointer;   // DWORD* optional, in/out
  dwFlags: DWORD   // DWORD
): Integer; stdcall;
  external 'SHLWAPI.dll' name 'UrlUnescapeA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "UrlUnescapeA"
  c_UrlUnescapeA :: CString -> CString -> Ptr Word32 -> Word32 -> IO Int32
-- pszUrl : LPSTR in/out -> CString
-- pszUnescaped : LPSTR optional, out -> CString
-- pcchUnescaped : DWORD* optional, in/out -> Ptr Word32
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let urlunescapea =
  foreign "UrlUnescapeA"
    (string @-> string @-> (ptr uint32_t) @-> uint32_t @-> returning int32_t)
(* pszUrl : LPSTR in/out -> string *)
(* pszUnescaped : LPSTR optional, out -> string *)
(* pcchUnescaped : DWORD* optional, in/out -> (ptr uint32_t) *)
(* dwFlags : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)

(cffi:defcfun ("UrlUnescapeA" url-unescape-a :convention :stdcall) :int32
  (psz-url :pointer)   ; LPSTR in/out
  (psz-unescaped :pointer)   ; LPSTR optional, out
  (pcch-unescaped :pointer)   ; DWORD* optional, in/out
  (dw-flags :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $UrlUnescapeA = Win32::API::More->new('SHLWAPI',
    'int UrlUnescapeA(LPSTR pszUrl, LPSTR pszUnescaped, LPVOID pcchUnescaped, DWORD dwFlags)');
# my $ret = $UrlUnescapeA->Call($pszUrl, $pszUnescaped, $pcchUnescaped, $dwFlags);
# pszUrl : LPSTR in/out -> LPSTR
# pszUnescaped : LPSTR optional, out -> LPSTR
# pcchUnescaped : DWORD* optional, in/out -> LPVOID
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い