各位大神,帮忙看看,调用dll内的函数时,函数内有 fmt.Println 的时候,程序运行就会出错。(PS:试了很多次,有一次看到了dll内的输出,但基本上出错概率比较大)
Dll模块代码
```
package main
import "C"
import (
"fmt"
)
//export DCall
func DCall() int {
return 2
}
//export DCallWithFmt
func DCallWithFmt() int {
fmt.Println("Hello world from dll!")
return 3
}
func main() {
}
```
生成Dll命令: `go build -buildmode=c-shared -o plugin.dll main.go`
调用部分代码
```
fmt.Println("Start...")
handler, _ := syscall.LoadLibrary("plugin.dll")
dcall, _ := syscall.GetProcAddress(handler, "DCall")
fmt.Println("DCall")
r1, _, _ := syscall.Syscall(dcall, 0, 0, 0, 0)
fmt.Println("Return:", r1)
dcallwithfmt, _ := syscall.GetProcAddress(handler, "DCallWithFmt")
fmt.Println("DCallWithFmt")
r1, _, _ = syscall.Syscall(dcallwithfmt, 0, 0, 0, 0)
fmt.Println("Return:", r1)
fmt.Println("GO GO GO!")
```
运行出错时的输出信息
```
Start...
DCall
Return: 2
DCallWithFmt
fatal error: unexpected signal during runtime execution
[signal 0xc0000005 code=0x1 addr=0x0 pc=0x67edf258]
goroutine 1 [running, locked to thread]:
runtime.throw(0x67f6bd71, 0x2a)
c:/go/src/runtime/panic.go:1112 +0x79 fp=0xc000077880 sp=0xc000077850 pc=0x67eb3449
runtime.sigpanic()
c:/go/src/runtime/signal_windows.go:240 +0x25a fp=0xc0000778b0 sp=0xc000077880 pc=0x67ec508a
runtime.memmove(0x0, 0xc00007e200, 0x1)
c:/go/src/runtime/memmove_amd64.s:148 +0x108 fp=0xc0000778b8 sp=0xc0000778b0 pc=0x67edf258
runtime.heapBitsSetType(0xc00007e200, 0x200, 0x200, 0x67f51a40)
c:/go/src/runtime/mbitmap.go:1409 +0x4bc fp=0xc0000779c0 sp=0xc0000778b8 pc=0x67e9498c
runtime.mallocgc(0x200, 0x67f51a40, 0xc000006001, 0x4)
c:/go/src/runtime/malloc.go:1066 +0x57d fp=0xc000077a60 sp=0xc0000779c0 pc=0x67e8cced
runtime.makeslice(0x67f51a40, 0x4, 0x4, 0x0)
c:/go/src/runtime/slice.go:49 +0x73 fp=0xc000077a90 sp=0xc000077a60 pc=0x67ec5373
sync.(*Pool).pinSlow(0x67f2fc80, 0x0, 0x0)
c:/go/src/sync/pool.go:227 +0xe4 fp=0xc000077b10 sp=0xc000077a90 pc=0x67ee73c4
sync.(*Pool).pin(0x67f2fc80, 0x43c8f3, 0xc000028000)
c:/go/src/sync/pool.go:206 +0x65 fp=0xc000077b38 sp=0xc000077b10 pc=0x67ee72b5
sync.(*Pool).Get(0x67f2fc80, 0xc000000000, 0x0)
c:/go/src/sync/pool.go:128 +0x36 fp=0xc000077b80 sp=0xc000077b38 pc=0x67ee6f66
fmt.newPrinter(0x44cfee)
c:/go/src/fmt/print.go:137 +0x38 fp=0xc000077ba8 sp=0xc000077b80 pc=0x67f185b8
fmt.Fprintln(0x67f822e0, 0x0, 0xc000077c50, 0x1, 0x1, 0x0, 0x0, 0xc00000a1d0)
c:/go/src/fmt/print.go:263 +0x2d fp=0xc000077c10 sp=0xc000077ba8 pc=0x67f1896d
fmt.Println(...)
c:/go/src/fmt/print.go:274
main.DCallWithFmt(...)
C:/SourceCode/Go/src/Dll_Test/main.go:15
main._cgoexpwrap_c69f6cbd521b_DCallWithFmt(0x0)
_cgo_gotypes.go:60 +0x7c fp=0xc000077c70 sp=0xc000077c10 pc=0x67f1f10c
runtime.call32(0x0, 0x95fb40, 0x95fcb0, 0x8)
c:/go/src/runtime/asm_amd64.s:539 +0x42 fp=0xc000077ca0 sp=0xc000077c70 pc=0x67edc142
runtime.cgocallbackg1(0x0)
c:/go/src/runtime/cgocall.go:332 +0x196 fp=0xc000077d38 sp=0xc000077ca0 pc=0x67e84186
runtime.cgocallbackg(0x0)
c:/go/src/runtime/cgocall.go:207 +0xe2 fp=0xc000077da0 sp=0xc000077d38 pc=0x67e83f42
runtime: unexpected return pc for runtime.cgocallback_gofunc called from 0x45cc37
stack: frame={sp:0xc000077da0, fp:0xc000077dc0} stack=[0xc000070000,0xc000078000)
000000c000077ca0: 0000000000000000 000000000095fb40
000000c000077cb0: 000000000095fcb0 0000000000000008
000000c000077cc0: 0000000067ebd4a6 <runtime.exitsyscallfast+214> 000000c00001e000
000000c000077cd0: 000000c000078280 000000c00000a1d0
000000c000077ce0: 020201000000000d 0000000000000010
000000c000077cf0: 000000000000000d 000000c000077d28
000000c000077d00: 000000c000028000 000000c000077ce5
000000c000077d10: 0000000067f6d8e8 0000000000000000
000000c000077d20: 0000000000000000 000000c000077d90
000000c000077d30: 0000000067e83f42 <runtime.cgocallbackg+226> 0000000000000000
000000c000077d40: 00000000004ed8d0 0000000000403ed5
000000c000077d50: 0000000067f1f2e0 0000000000000000
000000c000077d60: 000000c000077e50 0000000000000001
000000c000077d70: 0000000000000000 0000000000000000
000000c000077d80: 000000c000077dc8 000000c000028000
000000c000077d90: 000000000095fb00 0000000067edd7a2 <runtime.cgocallback_gofunc+178>
000000c000077da0: <0000000000000000 000000000057f5a0
000000c000077db0: 000000c000077dc8 !000000000045cc37
000000c000077dc0: >0000000000403f07 000000000045efe0
000000c000077dd0: 000000000057f868 000000c00004cc30
000000c000077de0: 000000c00000a1d0 000000000057f5a0
000000c000077df0: 000000c000077e30 000000000044ce69
000000c000077e00: 000000000045efe0 000000000057f868
000000c000077e10: 0000000000000000 010000000000000d
000000c000077e20: 000000000057f5a0 00000000004ed8a8
000000c000077e30: 000000c000077f18 000000000049eb81
000000c000077e40: 0000000067f1f2e0 0000000000000000
000000c000077e50: 0000000000000000 0000000000000000
000000c000077e60: 0000000000000000 0000000000000000
000000c000077e70: 0000000000000000 0000000000000000
000000c000077e80: 0000000067e80000 0000000067f1f2e0
000000c000077e90: 0000000067f1f2a0 00000000004afcc0
000000c000077ea0: 00000000004ddda0 00000000004c3ee0
000000c000077eb0: 0000000000500920 00000000004c3ee0
runtime.cgocallback_gofunc(0x403f07, 0x45efe0, 0x57f868, 0xc00004cc30)
c:/go/src/runtime/asm_amd64.s:793 +0xb2 fp=0xc000077dc0 sp=0xc000077da0 pc=0x67edd7a2
goroutine 1 [runnable, locked to thread]:
syscall.getprocaddress(0x7ffa680f0000, 0x1c000180000, 0x0, 0x0)
c:/go/src/runtime/syscall_windows.go:171 +0xe2
syscall.(*DLL).FindProc(0x1c000100000, 0x67f67104, 0x14, 0x67edfa50, 0x68000848, 0x0)
c:/go/src/syscall/dll_windows.go:114 +0x96
syscall.(*LazyProc).Find(0x1c000044b70, 0x0, 0x0)
c:/go/src/syscall/dll_windows.go:295 +0xf5
syscall.(*LazyProc).mustFind(0x1c000044b70)
c:/go/src/syscall/dll_windows.go:309 +0x32
syscall.(*LazyProc).Addr(0x1c000044b70, 0xfffffffffffffff6)
c:/go/src/syscall/dll_windows.go:318 +0x32
syscall.SetHandleInformation(0xa10, 0x1, 0x0, 0x0)
c:/go/src/syscall/zsyscall_windows.go:956 +0x38
syscall.CloseOnExec(...)
c:/go/src/syscall/exec_windows.go:125
syscall.getStdHandle(0xfffffffffffffff6, 0x67f67104)
c:/go/src/syscall/syscall_windows.go:443 +0x4f
syscall.init()
c:/go/src/syscall/syscall_windows.go:436 +0x1222
```
go version:`go version go1.14 windows/amd64`
有疑问加站长微信联系(非本文作者)