CoAllowSetForegroundWindow
関数シグネチャ
// OLE32.dll
#include <windows.h>
HRESULT CoAllowSetForegroundWindow(
IUnknown* pUnk,
void* lpvReserved // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| pUnk | IUnknown* | in | 対象となる COM サーバーのプロキシ上の IUnknown インターフェイスへのポインターです。 |
| lpvReserved | void* | inoptional | このパラメーターは予約済みであり、NULL を指定する必要があります。 |
戻り値の型: HRESULT
公式ドキュメント
この関数は、フォアグラウンド特権(フォアグラウンドウィンドウを設定する特権)をあるプロセスから別のプロセスへ引き渡します。フォアグラウンド特権を持つプロセスは、この関数を呼び出すことで、その特権をローカルの COM サーバープロセスへ引き渡すことができます。
戻り値
この関数は次の値を返すことがあります。
| 戻り値 | 説明 |
|---|---|
| メソッドは成功しました。 | |
| lpvReserved パラメーターが NULL ではありません。 | |
| pUnk パラメーターがフォアグラウンドウィンドウの制御をサポートしていません。 | |
| 呼び出し元のプロセスが現在フォアグラウンド特権を保持していません。 |
解説(Remarks)
システムは、フォアグラウンドウィンドウを設定するために SetForegroundWindow および AllowSetForegroundWindow 関数を呼び出せるプロセスを制限します。その結果、ユーザーが操作している最中であっても、アプリケーションが別のアプリケーションからフォーカスを奪うことは禁止されます。CoAllowSetForegroundWindow を使用すると、フォアグラウンド特権を持つプロセスから、まだ持っていないプロセスへその特権を引き渡すことができます。これは推移的に行うことができます。つまり、あるプロセスから別のプロセスへ、さらに別のプロセスへ、というように特権を引き渡せます。
CoAllowSetForegroundWindow を使用すると、カスタムインターフェイスを持つユーザーが、ウィンドウの変更が想定される OLE インターフェイス(主にリンクと埋め込みに関連します)で発生するのと同じ動作を得られるようになります。
内部的には、プロセス間でフォアグラウンドウィンドウを譲渡するために IForegroundTransfer インターフェイスが使用されます。標準の COM 提供プロキシは既に IForegroundTransfer を実装しているため、標準プロキシを使用している場合は追加の作業を行う必要はありません。任意のアウトプロセス COM オブジェクトにフォアグラウンド特権を転送するには、CoAllowSetForegroundWindow を呼び出すだけです。
例
次の例は、クライアントプロセスがローカル COM サーバーを作成し、CoAllowSetForegroundWindow を呼び出してフォアグラウンド特権を転送し、その後、直接的または間接的に SetForegroundWindow を呼び出す COM サーバー上の関数を呼び出す方法を示しています。
Microsoft::WRL::ComPtr<IExampleInterface> exampleLocalServer;
ThrowIfFailed(::CoCreateInstance(CLSID_ExampleLocalServer,
nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&exampleLocalServer)));
// 成功または失敗に応じて対応できますが、自動的に例外をスローしないでください。特権の引き渡し
// (およびフォアグラウンドの取得)の成功にウィンドウの表示を依存させないでください。
// ウィンドウは、フォーカスを受け取る準備ができたことをユーザーに知らせるものだからです。
HRESULT hr = ::CoAllowSetForegroundWindow(exampleLocalServer.Get(), nullptr);
// それ自体が ::SetForegroundWindow(HWND) を呼び出す例のメソッドを呼び出します。
hr = exampleLocalServer->FunctionThatSetsForegroundWindow();
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// OLE32.dll
#include <windows.h>
HRESULT CoAllowSetForegroundWindow(
IUnknown* pUnk,
void* lpvReserved // optional
);[DllImport("OLE32.dll", ExactSpelling = true)]
static extern int CoAllowSetForegroundWindow(
IntPtr pUnk, // IUnknown*
IntPtr lpvReserved // void* optional
);<DllImport("OLE32.dll", ExactSpelling:=True)>
Public Shared Function CoAllowSetForegroundWindow(
pUnk As IntPtr, ' IUnknown*
lpvReserved As IntPtr ' void* optional
) As Integer
End Function' pUnk : IUnknown*
' lpvReserved : void* optional
Declare PtrSafe Function CoAllowSetForegroundWindow Lib "ole32" ( _
ByVal pUnk As LongPtr, _
ByVal lpvReserved As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
CoAllowSetForegroundWindow = ctypes.windll.ole32.CoAllowSetForegroundWindow
CoAllowSetForegroundWindow.restype = ctypes.c_int
CoAllowSetForegroundWindow.argtypes = [
ctypes.c_void_p, # pUnk : IUnknown*
ctypes.POINTER(None), # lpvReserved : void* optional
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('OLE32.dll')
CoAllowSetForegroundWindow = Fiddle::Function.new(
lib['CoAllowSetForegroundWindow'],
[
Fiddle::TYPE_VOIDP, # pUnk : IUnknown*
Fiddle::TYPE_VOIDP, # lpvReserved : void* optional
],
Fiddle::TYPE_INT)#[link(name = "ole32")]
extern "system" {
fn CoAllowSetForegroundWindow(
pUnk: *mut core::ffi::c_void, // IUnknown*
lpvReserved: *mut () // void* optional
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("OLE32.dll")]
public static extern int CoAllowSetForegroundWindow(IntPtr pUnk, IntPtr lpvReserved);
"@
$api = Add-Type -MemberDefinition $sig -Name 'OLE32_CoAllowSetForegroundWindow' -Namespace Win32 -PassThru
# $api::CoAllowSetForegroundWindow(pUnk, lpvReserved)#uselib "OLE32.dll"
#func global CoAllowSetForegroundWindow "CoAllowSetForegroundWindow" sptr, sptr
; CoAllowSetForegroundWindow pUnk, lpvReserved ; 戻り値は stat
; pUnk : IUnknown* -> "sptr"
; lpvReserved : void* optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "OLE32.dll"
#cfunc global CoAllowSetForegroundWindow "CoAllowSetForegroundWindow" sptr, sptr
; res = CoAllowSetForegroundWindow(pUnk, lpvReserved)
; pUnk : IUnknown* -> "sptr"
; lpvReserved : void* optional -> "sptr"; HRESULT CoAllowSetForegroundWindow(IUnknown* pUnk, void* lpvReserved)
#uselib "OLE32.dll"
#cfunc global CoAllowSetForegroundWindow "CoAllowSetForegroundWindow" intptr, intptr
; res = CoAllowSetForegroundWindow(pUnk, lpvReserved)
; pUnk : IUnknown* -> "intptr"
; lpvReserved : void* optional -> "intptr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
ole32 = windows.NewLazySystemDLL("OLE32.dll")
procCoAllowSetForegroundWindow = ole32.NewProc("CoAllowSetForegroundWindow")
)
// pUnk (IUnknown*), lpvReserved (void* optional)
r1, _, err := procCoAllowSetForegroundWindow.Call(
uintptr(pUnk),
uintptr(lpvReserved),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // HRESULTfunction CoAllowSetForegroundWindow(
pUnk: Pointer; // IUnknown*
lpvReserved: Pointer // void* optional
): Integer; stdcall;
external 'OLE32.dll' name 'CoAllowSetForegroundWindow';result := DllCall("OLE32\CoAllowSetForegroundWindow"
, "Ptr", pUnk ; IUnknown*
, "Ptr", lpvReserved ; void* optional
, "Int") ; return: HRESULT●CoAllowSetForegroundWindow(pUnk, lpvReserved) = DLL("OLE32.dll", "int CoAllowSetForegroundWindow(void*, void*)")
# 呼び出し: CoAllowSetForegroundWindow(pUnk, lpvReserved)
# pUnk : IUnknown* -> "void*"
# lpvReserved : void* optional -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "ole32" fn CoAllowSetForegroundWindow(
pUnk: ?*anyopaque, // IUnknown*
lpvReserved: ?*anyopaque // void* optional
) callconv(std.os.windows.WINAPI) i32;proc CoAllowSetForegroundWindow(
pUnk: pointer, # IUnknown*
lpvReserved: pointer # void* optional
): int32 {.importc: "CoAllowSetForegroundWindow", stdcall, dynlib: "OLE32.dll".}pragma(lib, "ole32");
extern(Windows)
int CoAllowSetForegroundWindow(
void* pUnk, // IUnknown*
void* lpvReserved // void* optional
);ccall((:CoAllowSetForegroundWindow, "OLE32.dll"), stdcall, Int32,
(Ptr{Cvoid}, Ptr{Cvoid}),
pUnk, lpvReserved)
# pUnk : IUnknown* -> Ptr{Cvoid}
# lpvReserved : void* optional -> Ptr{Cvoid}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t CoAllowSetForegroundWindow(
void* pUnk,
void* lpvReserved);
]]
local ole32 = ffi.load("ole32")
-- ole32.CoAllowSetForegroundWindow(pUnk, lpvReserved)
-- pUnk : IUnknown*
-- lpvReserved : void* optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('OLE32.dll');
const CoAllowSetForegroundWindow = lib.func('__stdcall', 'CoAllowSetForegroundWindow', 'int32_t', ['void *', 'void *']);
// CoAllowSetForegroundWindow(pUnk, lpvReserved)
// pUnk : IUnknown* -> 'void *'
// lpvReserved : void* optional -> 'void *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("OLE32.dll", {
CoAllowSetForegroundWindow: { parameters: ["pointer", "pointer"], result: "i32" },
});
// lib.symbols.CoAllowSetForegroundWindow(pUnk, lpvReserved)
// pUnk : IUnknown* -> "pointer"
// lpvReserved : void* optional -> "pointer"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t CoAllowSetForegroundWindow(
void* pUnk,
void* lpvReserved);
C, "OLE32.dll");
// $ffi->CoAllowSetForegroundWindow(pUnk, lpvReserved);
// pUnk : IUnknown*
// lpvReserved : void* optional
// 構造体/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 CoAllowSetForegroundWindow(
Pointer pUnk, // IUnknown*
Pointer lpvReserved // void* optional
);
}@[Link("ole32")]
lib LibOLE32
fun CoAllowSetForegroundWindow = CoAllowSetForegroundWindow(
pUnk : Void*, # IUnknown*
lpvReserved : Void* # void* optional
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef CoAllowSetForegroundWindowNative = Int32 Function(Pointer<Void>, Pointer<Void>);
typedef CoAllowSetForegroundWindowDart = int Function(Pointer<Void>, Pointer<Void>);
final CoAllowSetForegroundWindow = DynamicLibrary.open('OLE32.dll')
.lookupFunction<CoAllowSetForegroundWindowNative, CoAllowSetForegroundWindowDart>('CoAllowSetForegroundWindow');
// pUnk : IUnknown* -> Pointer<Void>
// lpvReserved : void* optional -> Pointer<Void>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function CoAllowSetForegroundWindow(
pUnk: Pointer; // IUnknown*
lpvReserved: Pointer // void* optional
): Integer; stdcall;
external 'OLE32.dll' name 'CoAllowSetForegroundWindow';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "CoAllowSetForegroundWindow"
c_CoAllowSetForegroundWindow :: Ptr () -> Ptr () -> IO Int32
-- pUnk : IUnknown* -> Ptr ()
-- lpvReserved : void* optional -> Ptr ()
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let coallowsetforegroundwindow =
foreign "CoAllowSetForegroundWindow"
((ptr void) @-> (ptr void) @-> returning int32_t)
(* pUnk : IUnknown* -> (ptr void) *)
(* lpvReserved : void* optional -> (ptr void) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library ole32 (t "OLE32.dll"))
(cffi:use-foreign-library ole32)
(cffi:defcfun ("CoAllowSetForegroundWindow" co-allow-set-foreground-window :convention :stdcall) :int32
(p-unk :pointer) ; IUnknown*
(lpv-reserved :pointer)) ; void* optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $CoAllowSetForegroundWindow = Win32::API::More->new('OLE32',
'int CoAllowSetForegroundWindow(LPVOID pUnk, LPVOID lpvReserved)');
# my $ret = $CoAllowSetForegroundWindow->Call($pUnk, $lpvReserved);
# pUnk : IUnknown* -> LPVOID
# lpvReserved : void* optional -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。