golang go-ole what's wrong with my code and how to modify it.

504 views
Skip to first unread message

xiami china

unread,
Mar 4, 2014, 4:56:25 AM3/4/14
to golan...@googlegroups.com
package main

import (
"fmt"
)

func main() {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("ADODB.Stream")
stream, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer stream.Release()
oleutil.MustPutProperty(stream, "Type", 2)
oleutil.MustPutProperty(stream, "CharSet", "utf-8")
oleutil.MustCallMethod(stream, "Open")
oleutil.MustCallMethod(stream, "WriteText", "abcdefg")
oleutil.MustPutProperty(stream, "Position", 0)
text := oleutil.MustCallMethod(stream, "ReadText")
fmt.Println(text) // i want get value 'abcdefg'
}

many thanks for you help.

egon

unread,
Mar 4, 2014, 5:12:17 AM3/4/14
to golan...@googlegroups.com
Not sure whether it helps... but always check error values returned from functions... (CreateObject/QueryInterface).

+ egon

mattn

unread,
Mar 4, 2014, 8:49:42 AM3/4/14
to golan...@googlegroups.com

xiami china

unread,
Mar 4, 2014, 10:46:36 AM3/4/14
to golan...@googlegroups.com
now, it works! thank you for you update.

but i have a other question about how to trans a buffer value to com method.

like this..

package main

import (
"bytes"
"fmt"
)

func main() {
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("ADODB.Stream")
stream, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer stream.Release()
oleutil.MustPutProperty(stream, "Type", 1)
oleutil.MustCallMethod(stream, "Open")

buffer := bytes.NewBuffer([]byte("abcdefg"))
// Sub Write(ByVal Buffer)
_, err := oleutil.CallMethod(stream, "Write", buffer) // question is there, panic: unknown type
if err != nil {
fmt.Println(err)
}
oleutil.MustPutProperty(stream, "Position", 0)
oleutil.MustPutProperty(stream, "Type", 2)
oleutil.MustPutProperty(stream, "CharSet", "utf-8")
text := oleutil.MustCallMethod(stream, "ReadText").ToString()
fmt.Println(text)
}




mattn

unread,
Mar 6, 2014, 12:56:40 AM3/6/14
to golan...@googlegroups.com
stream is go's object, not an interface of COM.
Reply all
Reply to author
Forward
0 new messages