登录
首页 >  Golang >  Go问答

如何在 Go 中访问 C 变量?

来源:stackoverflow

时间:2024-02-21 22:00:32 304浏览 收藏

大家好,我们又见面了啊~本文《如何在 Go 中访问 C 变量?》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~

问题内容

package main

/*
#include 
#include 
HDC *hdcArr

BOOL CALLBACK EnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
    for (int i = 0; i < (_msize(hdcArr) / sizeof(HDC)); i++) {
        if (hdcArr[i] == NULL) {
            hdcArr[i] = hdcMonitor;
            break;
        }
    }
    return TRUE;
}
void Init() {
    int count = GetSystemMetrics(SM_CMONITORS);
    hdcArr = (HDC*)malloc(sizeof(HDC) * count);
    memset(hdcArr, 0, sizeof(HDC) * count);
}
HDC* GetHDC() {
    return *hdcArr;
}
*/
import "C"
import (
    "fmt"
    "reflect"
    "unsafe"
    ".../w32"
)
func main() {
    var hdc w32.HDC
    hdc = w32.GetDC(0)
    C.Init()
    w32.EnumDisplayMonitors(hdc, nil, reflect.ValueOf(C.EnumProc).Pointer(), 0)
    t := (*[]w32.HDC)(unsafe.Pointer(&C.hdcArr))
    cx := w32.GetDeviceCaps((*t)[0], w32.HORZRES)
    fmt.Println(cx)
}

我按照上面的方式编写了源代码。

我想要的是将cgo的hdc数组导入到w32.hdc数组中,以了解每个显示器的宽度和高度值。

但是,如果导入 t:= (* [] w32.hdc) unsafe.pointer (& c.hdcarr)) 并调用 cx:= w32.getdevicecaps ((* t) [0], w32.horzres)返回 0。

如何使用cgo查找多个显示器的宽度和高度?


解决方案


package main

/*
#cgo ldflags: -lgdi32
#include 
#include 
#include 
#include 
hdc *hdcarr;
int count;

bool callback enumproc(hmonitor hmonitor, hdc hdcmonitor, lprect lprcmonitor, lparam dwdata) {
    int i;
    for (i = 0; i < (_msize(hdcarr) / sizeof(hdc)); i++) {
        if (hdcarr[i] == null) {
            hdcarr[i] = createcompatibledc(hdcmonitor);
            break;
        }
    }
    return true;
}
void init() {
    count = getsystemmetrics(sm_cmonitors);
    hdcarr = (hdc*)malloc(sizeof(hdc) * count);
    memset(hdcarr, 0, sizeof(hdc) * count);
}
*/
import "c"

import (
    "fmt"
    "reflect"
    "unsafe"

    "github.com/jameshovious/w32"
)

func main() {
    c.init()
    hdc := w32.getdc(0)
    w32.enumdisplaymonitors(hdc, nil, reflect.valueof(c.enumproc).pointer(), 0)
    w32.releasedc(0, hdc)
    t := (*[256]w32.hdc)(unsafe.pointer(c.hdcarr))[:c.count:c.count]
    for _, dc := range t {
        cx := w32.getdevicecaps(dc, w32.horzres)
        fmt.println(cx)
        w32.deletedc(dc)
    }
    c.free(unsafe.pointer(c.hdcarr))
}

理解这一点非常重要:指向 c 数组的指针只是一个内存地址,没有任何有关大小的信息(因此你的 t 数组为空)。这就是为什么你必须先将它转换为一个大数组 (*[256]w32.hdc) 然后将其切片到正确的大小 [:c.count:c.count]

这就是为什么我将 count 设置为全局变量。

您遇到的另一个问题是传递给 enumproc 的 hdc 句柄仅在回调内有效。要使它们在回调范围之外永久可用,您必须调用 createcompatibledc(hdcmonitor);
要在 cgo 中使用此函数,您必须通过 #cgo ldflags 包含 lib gdi32:-lgdi32

使用完这些 dc 后,您必须使用 w32.deletedc(dc) 再次释放它们

另外,不要忘记使用 c.free(unsafe.pointer(c.hdcarr)) 释放分配的数组

我的建议:每当您使用 winapi 时,请仔细阅读 msdn 文档。这需要一些时间,但可以为您省去很多麻烦

您也可以完全在 golang 中完成此操作,无需使用 cgo:

package main

import (
    "fmt"
    "syscall"

    "github.com/jameshovious/w32"
)

func enumproc(hmonitor w32.hmonitor, hdcmonitor w32.hdc, lprcmonitor *w32.rect, dwdata w32.lparam) uintptr {
    fmt.println(w32.getdevicecaps(hdcmonitor, w32.horzres))
    return w32.true
}

func main() {
    hdc := w32.getdc(0)
    w32.enumdisplaymonitors(hdc, nil, syscall.newcallback(enumproc), 0)
    w32.releasedc(0, hdc)
}

甚至更流畅:

func EnumProc(hMonitor w32.HMONITOR, hdcMonitor w32.HDC, lprcMonitor *w32.RECT, dwData w32.LPARAM) uintptr {
    horzres := lprcMonitor.Right - lprcMonitor.Left
    fmt.Println(horzres)
    return w32.TRUE
}

func main() {
    w32.EnumDisplayMonitors(nil, nil, syscall.NewCallback(EnumProc), 0)
}

好了,本文到此结束,带大家了解了《如何在 Go 中访问 C 变量?》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>