github.com/hspan/go-ole@v0.0.0/oleutil/connection_windows.go (about)

     1  // +build windows
     2  
     3  package oleutil
     4  
     5  import (
     6  	"reflect"
     7  	"syscall"
     8  	"unsafe"
     9  
    10  	ole "github.com/hspan/go-ole"
    11  )
    12  
    13  // ConnectObject creates a connection point between two services for communication.
    14  func ConnectObject(disp *ole.IDispatch, iid *ole.GUID, idisp interface{}) (cookie uint32, err error) {
    15  	unknown, err := disp.QueryInterface(ole.IID_IConnectionPointContainer)
    16  	if err != nil {
    17  		return
    18  	}
    19  
    20  	container := (*ole.IConnectionPointContainer)(unsafe.Pointer(unknown))
    21  	var point *ole.IConnectionPoint
    22  	err = container.FindConnectionPoint(iid, &point)
    23  	if err != nil {
    24  		return
    25  	}
    26  	if edisp, ok := idisp.(*ole.IUnknown); ok {
    27  		cookie, err = point.Advise(edisp)
    28  		container.Release()
    29  		if err != nil {
    30  			return
    31  		}
    32  	}
    33  	rv := reflect.ValueOf(disp).Elem()
    34  	if rv.Type().Kind() == reflect.Struct {
    35  		dest := &stdDispatch{}
    36  		dest.lpVtbl = &stdDispatchVtbl{}
    37  		dest.lpVtbl.pQueryInterface = syscall.NewCallback(dispQueryInterface)
    38  		dest.lpVtbl.pAddRef = syscall.NewCallback(dispAddRef)
    39  		dest.lpVtbl.pRelease = syscall.NewCallback(dispRelease)
    40  		dest.lpVtbl.pGetTypeInfoCount = syscall.NewCallback(dispGetTypeInfoCount)
    41  		dest.lpVtbl.pGetTypeInfo = syscall.NewCallback(dispGetTypeInfo)
    42  		dest.lpVtbl.pGetIDsOfNames = syscall.NewCallback(dispGetIDsOfNames)
    43  		dest.lpVtbl.pInvoke = syscall.NewCallback(dispInvoke)
    44  		dest.iface = disp
    45  		dest.iid = iid
    46  		cookie, err = point.Advise((*ole.IUnknown)(unsafe.Pointer(dest)))
    47  		container.Release()
    48  		if err != nil {
    49  			point.Release()
    50  			return
    51  		}
    52  		return
    53  	}
    54  
    55  	container.Release()
    56  
    57  	return 0, ole.NewError(ole.E_INVALIDARG)
    58  }