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

StrDupA

関数
文字列を複製して新しいバッファに格納する。
DLLSHLWAPI.dll文字セットANSI (-A)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

LPSTR StrDupA(
    LPCSTR pszSrch
);

パラメーター

名前方向説明
pszSrchLPCSTRin定数の null 終端文字列へのポインターです。

戻り値の型: LPSTR

公式ドキュメント

文字列を複製します。(ANSI)

戻り値

型: PTSTR

コピーされた文字列のアドレスを返します。文字列をコピーできない場合は NULL を返します。

解説(Remarks)

StrDup は、元の文字列のサイズ分の記憶域を割り当てます。記憶域の割り当てに成功すると、元の文字列が複製先の文字列へコピーされます。

この関数は LocalAlloc を使用して、文字列のコピー用の記憶域を割り当てます。呼び出し側アプリケーションは、StrDup の呼び出しによって返されたポインターに対して LocalFree 関数を呼び出して、このメモリを解放する必要があります。

使用例

この単純なコンソールアプリケーションは、StrDup の使用方法を示しています。

#include <windows.h>
#include <shlwapi.h>
#include <stdio.h>

void main(void)
{
   char buffer[] = "This is the buffer text";
   char *newstring;

   // Note: Never use an unbounded %s format specifier in printf.
   printf("Original: %25s\n", buffer);

   newstring = StrDup(buffer);
   if (newstring != NULL)
   {
       printf("Copy:     %25s\n", newstring);
       LocalFree(newstring);
   }
}

OUTPUT:
- - - - - - 
Original: This is the buffer text
Copy:     This is the buffer text
メモ

shlwapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして StrDup を定義しています。エンコードに依存しないエイリアスの使用を、エンコードに依存しないように書かれていないコードと混在させると、不一致が生じ、コンパイルエラーや実行時エラーを引き起こす可能性があります。詳細については、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>

LPSTR StrDupA(
    LPCSTR pszSrch
);
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern IntPtr StrDupA(
    [MarshalAs(UnmanagedType.LPStr)] string pszSrch   // LPCSTR
);
<DllImport("SHLWAPI.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function StrDupA(
    <MarshalAs(UnmanagedType.LPStr)> pszSrch As String   ' LPCSTR
) As IntPtr
End Function
' pszSrch : LPCSTR
Declare PtrSafe Function StrDupA Lib "shlwapi" ( _
    ByVal pszSrch As String) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

StrDupA = ctypes.windll.shlwapi.StrDupA
StrDupA.restype = wintypes.LPSTR
StrDupA.argtypes = [
    wintypes.LPCSTR,  # pszSrch : LPCSTR
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHLWAPI.dll')
StrDupA = Fiddle::Function.new(
  lib['StrDupA'],
  [
    Fiddle::TYPE_VOIDP,  # pszSrch : LPCSTR
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "shlwapi")]
extern "system" {
    fn StrDupA(
        pszSrch: *const u8  // LPCSTR
    ) -> *mut u8;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr StrDupA([MarshalAs(UnmanagedType.LPStr)] string pszSrch);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_StrDupA' -Namespace Win32 -PassThru
# $api::StrDupA(pszSrch)
#uselib "SHLWAPI.dll"
#func global StrDupA "StrDupA" sptr
; StrDupA pszSrch   ; 戻り値は stat
; pszSrch : LPCSTR -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "SHLWAPI.dll"
#cfunc global StrDupA "StrDupA" str
; res = StrDupA(pszSrch)
; pszSrch : LPCSTR -> "str"
; LPSTR StrDupA(LPCSTR pszSrch)
#uselib "SHLWAPI.dll"
#cfunc global StrDupA "StrDupA" str
; res = StrDupA(pszSrch)
; pszSrch : LPCSTR -> "str"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procStrDupA = shlwapi.NewProc("StrDupA")
)

// pszSrch (LPCSTR)
r1, _, err := procStrDupA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(pszSrch))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // LPSTR
function StrDupA(
  pszSrch: PAnsiChar   // LPCSTR
): PAnsiChar; stdcall;
  external 'SHLWAPI.dll' name 'StrDupA';
result := DllCall("SHLWAPI\StrDupA"
    , "AStr", pszSrch   ; LPCSTR
    , "Ptr")   ; return: LPSTR
●StrDupA(pszSrch) = DLL("SHLWAPI.dll", "char* StrDupA(char*)")
# 呼び出し: StrDupA(pszSrch)
# pszSrch : LPCSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "shlwapi" fn StrDupA(
    pszSrch: [*c]const u8 // LPCSTR
) callconv(std.os.windows.WINAPI) [*c]const u8;
proc StrDupA(
    pszSrch: cstring  # LPCSTR
): cstring {.importc: "StrDupA", stdcall, dynlib: "SHLWAPI.dll".}
pragma(lib, "shlwapi");
extern(Windows)
const(char)* StrDupA(
    const(char)* pszSrch   // LPCSTR
);
ccall((:StrDupA, "SHLWAPI.dll"), stdcall, Cstring,
      (Cstring,),
      pszSrch)
# pszSrch : LPCSTR -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
const char* StrDupA(
    const char* pszSrch);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.StrDupA(pszSrch)
-- pszSrch : LPCSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const StrDupA = lib.func('__stdcall', 'StrDupA', 'void *', ['str']);
// StrDupA(pszSrch)
// pszSrch : LPCSTR -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHLWAPI.dll", {
  StrDupA: { parameters: ["buffer"], result: "pointer" },
});
// lib.symbols.StrDupA(pszSrch)
// pszSrch : LPCSTR -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
const char* StrDupA(
    const char* pszSrch);
C, "SHLWAPI.dll");
// $ffi->StrDupA(pszSrch);
// pszSrch : LPCSTR
// 構造体/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);
    Pointer StrDupA(
        String pszSrch   // LPCSTR
    );
}
@[Link("shlwapi")]
lib LibSHLWAPI
  fun StrDupA = StrDupA(
    pszSrch : UInt8*   # LPCSTR
  ) : UInt8*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef StrDupANative = Pointer<Utf8> Function(Pointer<Utf8>);
typedef StrDupADart = Pointer<Utf8> Function(Pointer<Utf8>);
final StrDupA = DynamicLibrary.open('SHLWAPI.dll')
    .lookupFunction<StrDupANative, StrDupADart>('StrDupA');
// pszSrch : LPCSTR -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function StrDupA(
  pszSrch: PAnsiChar   // LPCSTR
): PAnsiChar; stdcall;
  external 'SHLWAPI.dll' name 'StrDupA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "StrDupA"
  c_StrDupA :: CString -> IO CString
-- pszSrch : LPCSTR -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let strdupa =
  foreign "StrDupA"
    (string @-> returning string)
(* pszSrch : LPCSTR -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)

(cffi:defcfun ("StrDupA" str-dup-a :convention :stdcall) :string
  (psz-srch :string))   ; LPCSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $StrDupA = Win32::API::More->new('SHLWAPI',
    'LPSTR StrDupA(LPCSTR pszSrch)');
# my $ret = $StrDupA->Call($pszSrch);
# pszSrch : LPCSTR -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い