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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line