Skip to content

syscall(windows): callbacks from winapi not firing in Go #10973

Closed
@cromega

Description

@cromega

I see some weirdness while trying to interface with the WaveOut API in Windows (8.1, 64-bit, go1.4.2).

A callback I defined in Go and passed to WaveOut is supposed to be triggered multiple times by the lowlevel library on certain events. After a certain point these callbacks don't seem to run in Go but running my app through apimonitor shows that the lowlevel library actually tries to fire them:

apimonitor_dump

I tried to reproduce the issue with some standalone code, this is what I have (Note: Of course the actual issue might not be related, I don't know exactly what the winmm API and the sound interface driver are doing. Plus my low-level coding skills are lame so the whole thing could be completely wrong):

Sample dll:

#include "stdafx.h"
#include "callbackwtfcpp.h"

BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    return TRUE;
}

typedef int (__stdcall *GoCallback)(int data);

GoCallback _cb;

DWORD WINAPI myThread(LPVOID lpParameter)
{
    _cb(2);
    return 0;
}

extern "C"
{
    void CALLBACK Set(GoCallback cb)
    {
        _cb = cb;
    }

    void CALLBACK Fire(void)
    {
        _cb(1);

        DWORD myThreadID;
        HANDLE myHandle = CreateThread(0, 0, myThread, 0, 0, &myThreadID);
        WaitForSingleObject(myHandle, 500);
        CloseHandle(myThread);
    }
}

Sample Go:

package main

import (
  "fmt"
  "syscall"
)

func main () {
  dll := syscall.MustLoadDLL(`callbackwtfcpp.dll`)
  set := dll.MustFindProc("Set")
  fire := dll.MustFindProc("Fire")

  callback := syscall.NewCallback(func(data int) (ret uintptr) {
    fmt.Printf("from callback: %v\n", data)
    return
  })

  set.Call(callback)
  fire.Call()

  dll.Release()
}

What I expect to see on the terminal is

from callback: 1
from callback: 2

but I only see:

from callback: 1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions