doumo3903 2012-04-21 14:24
浏览 103
已采纳

cgo-如何将go字符串转换为LPCWSTR

I would like to use some of windows api, but I have no idea how to start. Is there any tutorial for it?

Anyway I have a simple code. Can you please help me to get this correct?

package mypackage
/*
#cgo LDFLAGS: -luser32
#include <windows.h>
*/
import "C"
import "unsafe"

func MessageBox(m string) {
      cm := C.CString(s)
      defer C.free(unsafe.Pointer(cm))
      C.MessageBoxA(C.HWND(nil), (*C.CHAR)(cm), C.LPCSTR(nil), 0) // It display a message.
}

Edit: I can deal with char* but still do not know what with wchar_t*.

import "syscall"

func MessageBoxU(m string) {
        C.MessageBoxW(C.HWND(nil), (*C.WCHAR)(unsafe.Pointer(syscall.StringToUTF16Ptr(m))), C.LPCWSTR(nil), 0)
}

Please let me know if this is not go idiom.

  • 写回答

1条回答 默认 最新

  • dongyun3897 2012-04-21 16:04
    关注

    The following are two projects which wrap the Windows API to Go:

    A usage example:

        func setWidgetText(hwnd HWND, text string) error {
            if TRUE != go-winapi.SendMessage(hwnd, WM_SETTEXT, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text)))) {
                return newError("WM_SETTEXT failed")
            }
            return nil
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器