Пример #1
0
// VirtualTableService creates VirtualTable with references to IUnknown methods.
//
// This should be used with Service().
func VirtualTableService() *VirtualTable {
	return &VirtualTable{
		QueryInterface:   syscall.NewCallback(iunknown.QueryInterfaceService),
		AddRef:           syscall.NewCallback(iunknown.AddRefService),
		Release:          syscall.NewCallback(iunknown.ReleaseService),
		GetTypeInfoCount: syscall.NewCallback(GetTypeInfoCountService),
		GetTypeInfo:      syscall.NewCallback(GetTypeInfoService),
		GetIDsOfNames:    syscall.NewCallback(GetIDsOfNamesService),
		Invoke:           syscall.NewCallback(InvokeService)}
}
Пример #2
0
func Example_winsock() {
	queryInterface := func(self *interface{}, interfaceID *com.GUID, client **interface{}) uint32 {
		code := iunknown.QueryInterfaceService(self, interfaceID, *client)
		if code == com.NoInterfaceErrorCode {
			s := com.StringFromClassID(interfaceID)
			if s == "{248DD893-BB45-11CF-9ABC-0080C7E7B78D}" {
				iunknown.AddRefService(self)
				*client = self
				return com.SuccessResponseCode
			}
		}
		return com.NoInterfaceErrorCode
	}

	getIDsOfNames := func(this *iunknown.IUnknown, iid *com.GUID, wnames []*uint16, namelen int, lcid int, pdisp []int32) uintptr {
		for n := 0; n < namelen; n++ {
			pdisp[n] = int32(n)
		}
		return uintptr(com.SuccessResponseCode)
	}

	invoke := func(this *IDispatch, dispid int32, riid *com.GUID, lcid int, flags int16, dispparams *com.DISPPARAMS, result *com.Variant, pexcepinfo *com.EXCEPINFO, nerr *uint) {
		switch dispid {
		case 0:
			log.Println("DataArrival")
			winsock := (*com.EventReceiver)(unsafe.Pointer(this)).host
			var data com.Variant
			com.VariantInit(&data)
			CallMethod(winsock, "GetData", &data)
			s := string(data.ToArray().ToByteArray())
			println()
			println(s)
			println()
		case 1:
			log.Println("Connected")
			winsock := (*com.EventReceiver)(unsafe.Pointer(this)).host
			oleutil.CallMethod(winsock, "SendData", "GET / HTTP/1.0\r\n\r\n")
		case 3:
			log.Println("SendProgress")
		case 4:
			log.Println("SendComplete")
		case 5:
			log.Println("Close")
			this.Release()
		case 6:
			log.Fatal("Error")
		default:
			log.Println(dispid)
		}
		return com.NotImplementedErrorCode
	}

	com.CoInitialize()
	defer com.CoUninitialize()

	var unknown *iunknown.Unknown
	var winsock *Dispatch

	err := com.CreateObject("{248DD896-BB45-11CF-9ABC-0080C7E7B78D}", &unknown)
	if err != nil {
		panic(err.Error())
	}
	unknown.QueryInterface(com.IDispatchInterfaceID, &winsock)
	defer winsock.Release()

	classID, _ := com.ClassIDFromString("{248DD893-BB45-11CF-9ABC-0080C7E7B78D}")

	destination := &com.EventReceiver{}
	destination.VirtualTable = &VirtualTable{
		QueryInterface:   syscall.NewCallback(queryInterface),
		AddRef:           syscall.NewCallback(iunknown.AddRefService),
		Release:          syscall.NewCallback(iunknown.ReleaseService),
		GetTypeInfoCount: syscall.NewCallback(GetTypeInfoCountService),
		GetTypeInfo:      syscall.NewCallback(GetTypeInfoService),
		GetIDsOfNames:    syscall.NewCallback(getIDsOfNames),
		Invoke:           syscall.NewCallback(invoke)}
	destination.Host = winsock

	iconnectionpointcontainer.ConnectObject(winsock, classID, (*iunknown.Unknown)(unsafe.Pointer(destination)))
	_, err = CallMethod(winsock, "Connect", "127.0.0.1", 80)
	if err != nil {
		log.Fatal(err)
	}

	var m com.Msg
	for dest.ReferenceCount != 0 {
		com.GetMessage(&m, 0, 0, 0)
		com.DispatchMessage(&m)
	}
}
Пример #3
0
// VirtualTableService creates VirtualTable with references to IUnknown methods.
//
// This should be used with Service().
func VirtualTableService() *VirtualTable {
	return &VirtualTable{
		QueryInterface: syscall.NewCallback(QueryInterfaceService),
		AddRef:         syscall.NewCallback(AddRefService),
		Release:        syscall.NewCallback(ReleaseService)}
}