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

CoLoadLibrary

関数
指定したCOMサーバーDLLを読み込む。
DLLOLE32.dll呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// OLE32.dll
#include <windows.h>

HINSTANCE CoLoadLibrary(
    LPWSTR lpszLibName,
    BOOL bAutoFree
);

パラメーター

名前方向説明
lpszLibNameLPWSTRinロードするライブラリの名前。
bAutoFreeBOOLinこのパラメーターは 16 ビットアプリケーションとの互換性のために維持されていますが、無視されます。

戻り値の型: HINSTANCE

公式ドキュメント

指定した DLL を呼び出し元のプロセスにロードします。

戻り値

関数が成功した場合、戻り値はロードされたライブラリのハンドルです。それ以外の場合は NULL です。

解説(Remarks)

CoGetClassObject 関数は CoLoadLibrary を呼び出しません。CoLoadLibrary は、lpszLibName パラメーターで指定された DLL を、CoGetClassObject を呼び出したプロセスにロードします。コンテナーは CoLoadLibrary を直接呼び出すべきではありません。

内部的には、ロードされた DLL に対して参照カウントが保持されます。CoLoadLibrary によってカウントが増加し、CoFreeLibrary 関数によってカウントが減少します。

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

各言語での呼び出し定義

// OLE32.dll
#include <windows.h>

HINSTANCE CoLoadLibrary(
    LPWSTR lpszLibName,
    BOOL bAutoFree
);
[DllImport("OLE32.dll", ExactSpelling = true)]
static extern IntPtr CoLoadLibrary(
    [MarshalAs(UnmanagedType.LPWStr)] string lpszLibName,   // LPWSTR
    bool bAutoFree   // BOOL
);
<DllImport("OLE32.dll", ExactSpelling:=True)>
Public Shared Function CoLoadLibrary(
    <MarshalAs(UnmanagedType.LPWStr)> lpszLibName As String,   ' LPWSTR
    bAutoFree As Boolean   ' BOOL
) As IntPtr
End Function
' lpszLibName : LPWSTR
' bAutoFree : BOOL
Declare PtrSafe Function CoLoadLibrary Lib "ole32" ( _
    ByVal lpszLibName As LongPtr, _
    ByVal bAutoFree As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CoLoadLibrary = ctypes.windll.ole32.CoLoadLibrary
CoLoadLibrary.restype = ctypes.c_void_p
CoLoadLibrary.argtypes = [
    wintypes.LPCWSTR,  # lpszLibName : LPWSTR
    wintypes.BOOL,  # bAutoFree : BOOL
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('OLE32.dll')
CoLoadLibrary = Fiddle::Function.new(
  lib['CoLoadLibrary'],
  [
    Fiddle::TYPE_VOIDP,  # lpszLibName : LPWSTR
    Fiddle::TYPE_INT,  # bAutoFree : BOOL
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "ole32")]
extern "system" {
    fn CoLoadLibrary(
        lpszLibName: *mut u16,  // LPWSTR
        bAutoFree: i32  // BOOL
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("OLE32.dll")]
public static extern IntPtr CoLoadLibrary([MarshalAs(UnmanagedType.LPWStr)] string lpszLibName, bool bAutoFree);
"@
$api = Add-Type -MemberDefinition $sig -Name 'OLE32_CoLoadLibrary' -Namespace Win32 -PassThru
# $api::CoLoadLibrary(lpszLibName, bAutoFree)
#uselib "OLE32.dll"
#func global CoLoadLibrary "CoLoadLibrary" sptr, sptr
; CoLoadLibrary lpszLibName, bAutoFree   ; 戻り値は stat
; lpszLibName : LPWSTR -> "sptr"
; bAutoFree : BOOL -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "OLE32.dll"
#cfunc global CoLoadLibrary "CoLoadLibrary" wstr, int
; res = CoLoadLibrary(lpszLibName, bAutoFree)
; lpszLibName : LPWSTR -> "wstr"
; bAutoFree : BOOL -> "int"
; HINSTANCE CoLoadLibrary(LPWSTR lpszLibName, BOOL bAutoFree)
#uselib "OLE32.dll"
#cfunc global CoLoadLibrary "CoLoadLibrary" wstr, int
; res = CoLoadLibrary(lpszLibName, bAutoFree)
; lpszLibName : LPWSTR -> "wstr"
; bAutoFree : BOOL -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	ole32 = windows.NewLazySystemDLL("OLE32.dll")
	procCoLoadLibrary = ole32.NewProc("CoLoadLibrary")
)

// lpszLibName (LPWSTR), bAutoFree (BOOL)
r1, _, err := procCoLoadLibrary.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpszLibName))),
	uintptr(bAutoFree),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HINSTANCE
function CoLoadLibrary(
  lpszLibName: PWideChar;   // LPWSTR
  bAutoFree: BOOL   // BOOL
): THandle; stdcall;
  external 'OLE32.dll' name 'CoLoadLibrary';
result := DllCall("OLE32\CoLoadLibrary"
    , "WStr", lpszLibName   ; LPWSTR
    , "Int", bAutoFree   ; BOOL
    , "Ptr")   ; return: HINSTANCE
●CoLoadLibrary(lpszLibName, bAutoFree) = DLL("OLE32.dll", "void* CoLoadLibrary(char*, bool)")
# 呼び出し: CoLoadLibrary(lpszLibName, bAutoFree)
# lpszLibName : LPWSTR -> "char*"
# bAutoFree : BOOL -> "bool"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "ole32" fn CoLoadLibrary(
    lpszLibName: [*c]const u16, // LPWSTR
    bAutoFree: i32 // BOOL
) callconv(std.os.windows.WINAPI) ?*anyopaque;
proc CoLoadLibrary(
    lpszLibName: WideCString,  # LPWSTR
    bAutoFree: int32  # BOOL
): pointer {.importc: "CoLoadLibrary", stdcall, dynlib: "OLE32.dll".}
pragma(lib, "ole32");
extern(Windows)
void* CoLoadLibrary(
    const(wchar)* lpszLibName,   // LPWSTR
    int bAutoFree   // BOOL
);
ccall((:CoLoadLibrary, "OLE32.dll"), stdcall, Ptr{Cvoid},
      (Cwstring, Int32),
      lpszLibName, bAutoFree)
# lpszLibName : LPWSTR -> Cwstring
# bAutoFree : BOOL -> Int32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void* CoLoadLibrary(
    const uint16_t* lpszLibName,
    int32_t bAutoFree);
]]
local ole32 = ffi.load("ole32")
-- ole32.CoLoadLibrary(lpszLibName, bAutoFree)
-- lpszLibName : LPWSTR
-- bAutoFree : BOOL
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('OLE32.dll');
const CoLoadLibrary = lib.func('__stdcall', 'CoLoadLibrary', 'void *', ['str16', 'int32_t']);
// CoLoadLibrary(lpszLibName, bAutoFree)
// lpszLibName : LPWSTR -> 'str16'
// bAutoFree : BOOL -> 'int32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("OLE32.dll", {
  CoLoadLibrary: { parameters: ["buffer", "i32"], result: "pointer" },
});
// lib.symbols.CoLoadLibrary(lpszLibName, bAutoFree)
// lpszLibName : LPWSTR -> "buffer"
// bAutoFree : BOOL -> "i32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* CoLoadLibrary(
    const uint16_t* lpszLibName,
    int32_t bAutoFree);
C, "OLE32.dll");
// $ffi->CoLoadLibrary(lpszLibName, bAutoFree);
// lpszLibName : LPWSTR
// bAutoFree : BOOL
// 構造体/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 Ole32 extends StdCallLibrary {
    Ole32 INSTANCE = Native.load("ole32", Ole32.class);
    Pointer CoLoadLibrary(
        WString lpszLibName,   // LPWSTR
        boolean bAutoFree   // BOOL
    );
}
@[Link("ole32")]
lib LibOLE32
  fun CoLoadLibrary = CoLoadLibrary(
    lpszLibName : UInt16*,   # LPWSTR
    bAutoFree : Int32   # BOOL
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef CoLoadLibraryNative = Pointer<Void> Function(Pointer<Utf16>, Int32);
typedef CoLoadLibraryDart = Pointer<Void> Function(Pointer<Utf16>, int);
final CoLoadLibrary = DynamicLibrary.open('OLE32.dll')
    .lookupFunction<CoLoadLibraryNative, CoLoadLibraryDart>('CoLoadLibrary');
// lpszLibName : LPWSTR -> Pointer<Utf16>
// bAutoFree : BOOL -> Int32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function CoLoadLibrary(
  lpszLibName: PWideChar;   // LPWSTR
  bAutoFree: BOOL   // BOOL
): THandle; stdcall;
  external 'OLE32.dll' name 'CoLoadLibrary';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "CoLoadLibrary"
  c_CoLoadLibrary :: CWString -> CInt -> IO (Ptr ())
-- lpszLibName : LPWSTR -> CWString
-- bAutoFree : BOOL -> CInt
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let coloadlibrary =
  foreign "CoLoadLibrary"
    ((ptr uint16_t) @-> int32_t @-> returning (ptr void))
(* lpszLibName : LPWSTR -> (ptr uint16_t) *)
(* bAutoFree : BOOL -> int32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library ole32 (t "OLE32.dll"))
(cffi:use-foreign-library ole32)

(cffi:defcfun ("CoLoadLibrary" co-load-library :convention :stdcall) :pointer
  (lpsz-lib-name (:string :encoding :utf-16le))   ; LPWSTR
  (b-auto-free :int32))   ; BOOL
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $CoLoadLibrary = Win32::API::More->new('OLE32',
    'HANDLE CoLoadLibrary(LPCWSTR lpszLibName, BOOL bAutoFree)');
# my $ret = $CoLoadLibrary->Call($lpszLibName, $bAutoFree);
# lpszLibName : LPWSTR -> LPCWSTR
# bAutoFree : BOOL -> BOOL
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

公式の関連項目