lxn / walk

A Windows GUI toolkit for the Go Programming Language
Other
6.8k stars 885 forks source link

TTM_ADDTOOL failed when running code using CGO #651

Open yayi2456 opened 4 years ago

yayi2456 commented 4 years ago

I'm running the example 'clipborad' with my cgo code, a C.Type variable and a print statement to print the address of the variable, added to get variables calculated in C.

Although I used the go build -tags walk_use_cgo command to build my code, the following errors would occur when I ran the executable program.

go build -tags walk_use_cgo
./clipboard.exe
2019/11/17 00:10:33 TTM_ADDTOOL failed

Stack:
goroutine 1 [running, locked to thread]:
runtime/debug.Stack(0x0, 0x0, 0x0)
        D:/Go/src/runtime/debug/stack.go:24 +0xae
github.com/lxn/walk.newErr(0x7110dc, 0x12, 0xc000077f68, 0x0)
        D:/Go/src/github.com/lxn/walk/error.go:81 +0x2d
github.com/lxn/walk.newError(0x7110dc, 0x12, 0x0, 0xc000077f68)
        D:/Go/src/github.com/lxn/walk/error.go:85 +0x3c
github.com/lxn/walk.(*ToolTip).addTool(0xc00007ec80, 0x74d540, 0xc000040700, 0x701100, 0x0, 0x74d540)
        D:/Go/src/github.com/lxn/walk/tooltip.go:186 +0xeb
github.com/lxn/walk.(*ToolTip).AddTool(0xc00007ec80, 0x74d540, 0xc000040700, 0x74d540, 0xc000040700)
        D:/Go/src/github.com/lxn/walk/tooltip.go:164 +0x4b
github.com/lxn/walk.(*WidgetBase).init(0xc000040700, 0x74d540, 0xc000040700, 0x74d540, 0xc000040700)
        D:/Go/src/github.com/lxn/walk/widget.go:137 +0xe6
github.com/lxn/walk.InitWindow(0x1204710, 0xc000040700, 0x74a200, 0xc0000b8000, 0x715734, 0x1c, 0x1000050000000, 0x0, 0x0)
        D:/Go/src/github.com/lxn/walk/window.go:637 +0x419
github.com/lxn/walk.InitWidget(0x74d540, 0xc000040700, 0x74a200, 0xc0000b8000, 0x715734, 0x1c, 0x1000050000000, 0x707a60, 0x65ee00)
        D:/Go/src/github.com/lxn/walk/widget.go:110 +0xc3
github.com/lxn/walk.NewCompositeWithStyle(0x74a200, 0xc0000b8000, 0xc000000000, 0x74a200, 0xc0000b8000, 0x74cc00)
        D:/Go/src/github.com/lxn/walk/composite.go:30 +0xd9
github.com/lxn/walk.NewComposite(0x74cc00, 0xc0000b8000, 0xc0000b8000, 0x74cc00, 0xc0000b8000)
        D:/Go/src/github.com/lxn/walk/composite.go:45 +0x68
github.com/lxn/walk.(*FormBase).init(0xc0000b8000, 0x1204388, 0xc0000b8000, 0x1204388, 0xc0000b8000)
        D:/Go/src/github.com/lxn/walk/form.go:104 +0x67
github.com/lxn/walk.InitWindow(0x74a200, 0xc0000b8000, 0x0, 0x0, 0x715c9d, 0x1d, 0x1000000cf0000, 0x0, 0x0)
        D:/Go/src/github.com/lxn/walk/window.go:629 +0xb86
github.com/lxn/walk.NewMainWindowWithName(0x0, 0x0, 0x0, 0x0, 0x0)
        D:/Go/src/github.com/lxn/walk/mainwindow.go:41 +0xb6
github.com/lxn/walk.NewMainWindow(0x6f5e60, 0xc0000b6000, 0x0)
        D:/Go/src/github.com/lxn/walk/mainwindow.go:34 +0x30
github.com/lxn/walk/declarative.MainWindow.Create(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        D:/Go/src/github.com/lxn/walk/declarative/mainwindow.go:63 +0x75
github.com/lxn/walk/declarative.MainWindow.Run(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        D:/Go/src/github.com/lxn/walk/declarative/mainwindow.go:206 +0x75
main.main()
        D:/Go/src/github.com/lxn/walk/examples/clipboard/clipboard.go:54 +0x365

Same error occured when I run other walk programs with cgo code in. I did not modify the manifest file clipboard.exe.manifest.

Why does this happen?What can I do to fix it? Thank you for your reply.

pebble2050 commented 4 years ago

I have same problem with simple code blow.

package garia2

// typedef int (*intFunc) ();
//
// int
// bridge_int_func(intFunc f)
// {
//      return f();
// }
//
// int fortytwo()
// {
//      return 42;
// }
import "C"
import (
    "fmt"
    "os"

    "github.com/lxn/walk"
    "github.com/lxn/walk/declarative"
)

type toolMainWindow struct {
    *walk.MainWindow
}

// Start main
func Start() {
    f := C.intFunc(C.fortytwo)
    fmt.Println(int(C.bridge_int_func(f)))

    var edit *walk.LineEdit // main
    mw := &toolMainWindow{}
    MW := declarative.MainWindow{
        AssignTo: &mw.MainWindow,
        Title:    "Tools Ver:2019100801",
        MinSize:  declarative.Size{Width: 150, Height: 200},
        Size:     declarative.Size{Width: 500, Height: 250},
        Layout:   declarative.VBox{},
        Children: []declarative.Widget{
            declarative.LineEdit{
                AssignTo: &edit,
            },
            declarative.PushButton{
                Text: "Test1",
                OnClicked: func() {
                    path := edit.Text()
                    if len(path) > 0 {
                        walk.MsgBox(mw, "Test1", "Test1 Over",
                            walk.MsgBoxIconError)
                    } else {
                        walk.MsgBox(mw, "Test1", "Test1 Over",
                            walk.MsgBoxIconError)
                    }
                },
            },
        },
    }

    if _, err := MW.Run(); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}
yayi2456 commented 4 years ago

UPDATE: I found that as long as I import a package looks like github.com/username/reponame in the example clipboard, TTM_ADDTOOL failed occured. for example:

//this is ok in clipboard
import (
    "fmt"
    "github.com/lxn/walk"
    . "github.com/lxn/walk/declarative"
)

but if I import another github.com/username/reponame to the file,which looks like:

//TTM_ADDTOOL failed in clipboard
import (
    "fmt"
    "github.com/myname/myrepo"//added
    "github.com/lxn/walk"
    . "github.com/lxn/walk/declarative"
)

TTM_ADDTOOL failed occured.

chuchiy commented 4 years ago

You have to use rsrc tool to generate rsrc.syso file from the clipboard.exe.manifest file. Put a clipboard.exe.manifest in the directory will work without cgo but will get TTM_ADDTOOL error when build with cgo