Exemple #1
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)
	}
}
Exemple #2
0
// This tests more than one function. It tests all of the functions needed in
// order to retrieve a SafeArray populated with Strings.
func ExampleGetElementString_quickbooks() {
	com.CoInitialize()
	defer com.CoUninitialize()

	clsid, err := com.ClassIDFromProgramID("QBXMLRP2.RequestProcessor.1")
	if err != nil {
		if err.(*com.OleError).Code() == com.COMObjectClassStringErrorCode {
			return
		}
		t.Log(err)
		t.FailNow()
	}

	var unknown *iunknown.Unknown
	var dispatch *idispatch.Dispatch

	err := com.CoCreateInstance(clsid, iunknown.InterfaceID, &unknown)
	if err != nil {
		return
	}
	defer unknown.Release()

	err := unknown.QueryInterface(idispatch.InterfaceID, &dispatch)
	if err != nil {
		return
	}

	var result *com.Variant
	_, err = idispatch.CallMethod(dispatch, "OpenConnection2", "", "Test Application 1", 1)
	if err != nil {
		return
	}

	result, err = idispatch.CallMethod(dispatch, "BeginSession", "", 2)
	if err != nil {
		return
	}

	ticket := result.ToString()

	result, err = idispatch.GetProperty(dispatch, "QBXMLVersionsForSession", ticket)
	if err != nil {
		return
	}

	//
	// Example begins.
	//

	var qbXMLVersions *COMArray
	var qbXMLVersionStrings []string
	qbXMLVersions = result.ToArray().Array

	// Release Safe Array memory
	defer Destroy(qbXMLVersions)

	// Get array bounds
	var LowerBounds int64
	var UpperBounds int64
	LowerBounds, err = GetLowerBound(qbXMLVersions, 1)
	if err != nil {
		return
	}

	UpperBounds, err = GetUpperBound(qbXMLVersions, 1)
	if err != nil {
		return
	}

	totalElements := UpperBounds - LowerBounds + 1
	qbXMLVersionStrings = make([]string, totalElements)

	for i := int64(0); i < totalElements; i++ {
		qbXMLVersionStrings[int32(i)], _ = GetElementString(qbXMLVersions, i)
	}

	//
	// Example ends.
	//

	result, err = idispatch.CallMethod(dispatch, "EndSession", ticket)
	if err != nil {
		return
	}

	result, err = idispatch.CallMethod(dispatch, "CloseConnection")
	if err != nil {
		return
	}
}