ホーム › System.Com › CoCreateGuid
CoCreateGuid
関数一意なGUIDを新規に生成する。
シグネチャ
// OLE32.dll
#include <windows.h>
HRESULT CoCreateGuid(
GUID* pguid
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pguid | GUID* | out | 要求された GUID へのポインター。 |
戻り値の型: HRESULT
公式ドキュメント
CLSID やインターフェイス識別子に使用される一意の 128 ビット整数である GUID を作成します。
戻り値
| 戻り値 | 説明 |
|---|---|
| GUID が正常に作成されました。 |
UuidCreate が返すエラーは HRESULT としてラップされます。
解説(Remarks)
CoCreateGuid 関数は RPC 関数 UuidCreate を呼び出します。この関数は、グローバルに一意な 128 ビット整数である GUID を作成します。分散環境で永続的な識別子として使用する、絶対的に一意な数値が必要な場合は CoCreateGuid を使用してください。この関数は、極めて高い確実性をもって一意の値を返します。同一またはその他のシステム(ネットワーク接続の有無を問わない)上の他のいかなる呼び出しも、同じ値を返すことはありません。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// OLE32.dll
#include <windows.h>
HRESULT CoCreateGuid(
GUID* pguid
);[DllImport("OLE32.dll", ExactSpelling = true)]
static extern int CoCreateGuid(
out Guid pguid // GUID* out
);<DllImport("OLE32.dll", ExactSpelling:=True)>
Public Shared Function CoCreateGuid(
<Out> ByRef pguid As Guid ' GUID* out
) As Integer
End Function' pguid : GUID* out
Declare PtrSafe Function CoCreateGuid Lib "ole32" ( _
ByVal pguid As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
CoCreateGuid = ctypes.windll.ole32.CoCreateGuid
CoCreateGuid.restype = ctypes.c_int
CoCreateGuid.argtypes = [
ctypes.c_void_p, # pguid : GUID* out
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('OLE32.dll')
CoCreateGuid = Fiddle::Function.new(
lib['CoCreateGuid'],
[
Fiddle::TYPE_VOIDP, # pguid : GUID* out
],
Fiddle::TYPE_INT)#[link(name = "ole32")]
extern "system" {
fn CoCreateGuid(
pguid: *mut GUID // GUID* out
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("OLE32.dll")]
public static extern int CoCreateGuid(out Guid pguid);
"@
$api = Add-Type -MemberDefinition $sig -Name 'OLE32_CoCreateGuid' -Namespace Win32 -PassThru
# $api::CoCreateGuid(pguid)#uselib "OLE32.dll"
#func global CoCreateGuid "CoCreateGuid" sptr
; CoCreateGuid varptr(pguid) ; 戻り値は stat
; pguid : GUID* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "OLE32.dll" #cfunc global CoCreateGuid "CoCreateGuid" var ; res = CoCreateGuid(pguid) ; pguid : GUID* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "OLE32.dll" #cfunc global CoCreateGuid "CoCreateGuid" sptr ; res = CoCreateGuid(varptr(pguid)) ; pguid : GUID* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; HRESULT CoCreateGuid(GUID* pguid) #uselib "OLE32.dll" #cfunc global CoCreateGuid "CoCreateGuid" var ; res = CoCreateGuid(pguid) ; pguid : GUID* out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; HRESULT CoCreateGuid(GUID* pguid) #uselib "OLE32.dll" #cfunc global CoCreateGuid "CoCreateGuid" intptr ; res = CoCreateGuid(varptr(pguid)) ; pguid : GUID* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
ole32 = windows.NewLazySystemDLL("OLE32.dll")
procCoCreateGuid = ole32.NewProc("CoCreateGuid")
)
// pguid (GUID* out)
r1, _, err := procCoCreateGuid.Call(
uintptr(pguid),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HRESULTfunction CoCreateGuid(
pguid: PGUID // GUID* out
): Integer; stdcall;
external 'OLE32.dll' name 'CoCreateGuid';result := DllCall("OLE32\CoCreateGuid"
, "Ptr", pguid ; GUID* out
, "Int") ; return: HRESULT●CoCreateGuid(pguid) = DLL("OLE32.dll", "int CoCreateGuid(void*)")
# 呼び出し: CoCreateGuid(pguid)
# pguid : GUID* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "ole32" fn CoCreateGuid(
pguid: [*c]GUID // GUID* out
) callconv(std.os.windows.WINAPI) i32;proc CoCreateGuid(
pguid: ptr GUID # GUID* out
): int32 {.importc: "CoCreateGuid", stdcall, dynlib: "OLE32.dll".}pragma(lib, "ole32");
extern(Windows)
int CoCreateGuid(
GUID* pguid // GUID* out
);ccall((:CoCreateGuid, "OLE32.dll"), stdcall, Int32,
(Ptr{GUID},),
pguid)
# pguid : GUID* out -> Ptr{GUID}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t CoCreateGuid(
void* pguid);
]]
local ole32 = ffi.load("ole32")
-- ole32.CoCreateGuid(pguid)
-- pguid : GUID* out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('OLE32.dll');
const CoCreateGuid = lib.func('__stdcall', 'CoCreateGuid', 'int32_t', ['void *']);
// CoCreateGuid(pguid)
// pguid : GUID* out -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("OLE32.dll", {
CoCreateGuid: { parameters: ["pointer"], result: "i32" },
});
// lib.symbols.CoCreateGuid(pguid)
// pguid : GUID* out -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t CoCreateGuid(
void* pguid);
C, "OLE32.dll");
// $ffi->CoCreateGuid(pguid);
// pguid : GUID* out
// 構造体/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);
int CoCreateGuid(
Pointer pguid // GUID* out
);
}@[Link("ole32")]
lib LibOLE32
fun CoCreateGuid = CoCreateGuid(
pguid : GUID* # GUID* out
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CoCreateGuidNative = Int32 Function(Pointer<Void>);
typedef CoCreateGuidDart = int Function(Pointer<Void>);
final CoCreateGuid = DynamicLibrary.open('OLE32.dll')
.lookupFunction<CoCreateGuidNative, CoCreateGuidDart>('CoCreateGuid');
// pguid : GUID* out -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CoCreateGuid(
pguid: PGUID // GUID* out
): Integer; stdcall;
external 'OLE32.dll' name 'CoCreateGuid';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CoCreateGuid"
c_CoCreateGuid :: Ptr () -> IO Int32
-- pguid : GUID* out -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let cocreateguid =
foreign "CoCreateGuid"
((ptr void) @-> returning int32_t)
(* pguid : GUID* out -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library ole32 (t "OLE32.dll"))
(cffi:use-foreign-library ole32)
(cffi:defcfun ("CoCreateGuid" co-create-guid :convention :stdcall) :int32
(pguid :pointer)) ; GUID* out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CoCreateGuid = Win32::API::More->new('OLE32',
'int CoCreateGuid(LPVOID pguid)');
# my $ret = $CoCreateGuid->Call($pguid);
# pguid : GUID* out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
公式の関連項目
- f UuidCreate — 新しい一意なUUID(GUID)を生成する。