Wednesday, May 14, 2025
HomeGolangConversion C code to GoLang - Getting Assist

Conversion C code to GoLang – Getting Assist


Hello,

I need to convert the C code, that reads a DLL generated from Matlab/Simulink:

#embrace <home windows.h>    
void important() {
    // varieties
    void (*mdl_initialize)(void); // perform
    double* mdl_G; // worth

    // Dll load
    void* dll = LoadLibrary("./teste_win64.dll");

    // GetProcAddress
  	mdl_initialize = (void(*)(void))GetProcAddress(dll , "initialize");
   	mdl_G = ((double*)GetProcAddress(dll, "G"));
   
    // Run initialize and seize the G worth
    mdl_initialize();
    printf("G = %.2f",*mdl_G);
}

to Golang.

I simply tried this tip:

func important() {
    dll, _ := syscall.LoadDLL("./teste_win64.dll")
    mdl_init, _ := syscall.GetProcAddress(dll.Deal with, "initialize")
    mdl_G, _ := syscall.GetProcAddress(dll.Deal with, "G")
    real_init := (*func())(unsafe.Pointer(&mdl_G))
    real_G := (*float64)(unsafe.Pointer(&mdl_G))
    real_init()
    log.Print(*real_G)
}

However no success. Any tip?

From WindowsDLLs · golang/go Wiki · GitHub it appears to be like such as you want uintptr as an alternative of unsefe.Pointer and syscall as an alternative of attempting to transform the pointer to a *func.

I had success utilizing NewLazyDll and NewProc:

dll := syscall.NewLazyDLL("teste_win64.dll")
mdl_G := dll.NewProc("G")
G := (*float64)(unsafe.Pointer(mdl_G.Addr()))

However VSCode report that unsafe.Pointer(mdl_G.Addr()) is misuse. There`s a greater kind to do that?

1 Like

See cmd/vet: “doable misuse of unsafe.Pointer” verify false optimistic price could also be too excessive · Problem #41205 · golang/go · GitHub and so on

This implies you possibly can’t work with the pointers from syscall with out go vet falsely complaining about it.

1 Like

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments