From 4ca451cc8849d345e9221a7772cf734a25eb875f Mon Sep 17 00:00:00 2001 From: t_max <1172915550@qq.com> Date: Mon, 17 Jun 2024 17:59:14 +0800 Subject: [PATCH] fix: correct api usage --- com/com.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/com/com.go b/com/com.go index d570baf..d09d865 100644 --- a/com/com.go +++ b/com/com.go @@ -2,6 +2,7 @@ package com import ( "errors" + "strings" "syscall" "unsafe" @@ -184,16 +185,18 @@ func SysFreeString(v *uint16) (err error) { } func MakeCOMObjectEx(hostname string, serverLocation CLSCTX, requestedClass *windows.GUID, requestedInterface *windows.GUID) (*IUnknown, error) { - requestedServerInfo := COSERVERINFO{ - PwszName: windows.StringToUTF16Ptr(hostname), - PAuthInfo: nil, - } reqInterface := MULTI_QI{ PIID: requestedInterface, PItf: nil, Hr: 0, } - err := CoCreateInstanceEx(requestedClass, nil, serverLocation, &requestedServerInfo, 1, &reqInterface) + var serverInfoPtr *COSERVERINFO = nil + if serverLocation != CLSCTX_LOCAL_SERVER { + serverInfoPtr = &COSERVERINFO{ + PwszName: windows.StringToUTF16Ptr(hostname), + } + } + err := CoCreateInstanceEx(requestedClass, nil, serverLocation, serverInfoPtr, 1, &reqInterface) if err != nil { return nil, err } @@ -204,7 +207,14 @@ func MakeCOMObjectEx(hostname string, serverLocation CLSCTX, requestedClass *win } func IsLocal(host string) bool { - return host == "" || host == "localhost" || host == "127.0.0.1" + if host == "" || host == "localhost" || host == "127.0.0.1" { + return true + } + name, err := windows.ComputerName() + if err != nil { + return false + } + return strings.ToLower(name) == strings.ToLower(host) } // Initialize initialize COM with COINIT_MULTITHREADED