package main

// #include <stdio.h>
// #include <stdlib.h>
import "C"
import (
	"net/http"
	"fmt"
	"syscall"
	"time"
	"unsafe"
)

var callbackFunc uintptr

func other() {
	fmt.Println("c->go->c")
}

//export HelloC
func HelloC(b *C.char, cc unsafe.Pointer) {
	fmt.Println("Hello C from golang.", C.GoString(b))

	fmt.Println(1)
	callbackFunc = uintptr(cc)
	syscall.Syscall(uintptr(cc), 0, 0, 0, 0)
	fmt.Println(2)

	go func() {
		time.Sleep(time.Second)
		syscall.Syscall(callbackFunc, 0, 0, 0, 0)
	}()

	go other()
}


//export StartServer
func StartServer(msg unsafe.Pointer) {
	fmt.Println("StartServer:8080")

	http.HandleFunc("/",func(w http.ResponseWriter, r *http.Request){
		callbackFunc = uintptr(msg)
		p := C.CString(r.URL.Path)
		pp := uintptr(unsafe.Pointer(p))
		fmt.Fprintf(w,"go:0x%x %x\n",&r.URL.Path, pp)
		syscall.Syscall(uintptr(msg), 1, pp, 0, 0)
		C.free(unsafe.Pointer(p))
		})
	go func(){
		fmt.Println( http.ListenAndServe(":8080",nil))
	}()
	fmt.Println("ss emd.")
}

// go build -buildmode=c-archive -o lib.a main.go
func main() {
	fmt.Println("Hello world!")
}

/*
go build -buildmode=c-archive -o out/lib.a main.go
gcc -shared -pthread -o out/goDLL.dll dll/goDLL.c out/lib.a -lWinMM -lntdll -lWS2_32
gcc lib/test.cpp out/lib.a -o out/test.exe -l winmm -l ntdll -l Ws2_32
out\test.exe
*/