Golang调用Dll案例

前言

在家办公已经两个多星期了,目前最大的困难就是网络很差。独自一个人用golang开发调用dll的驱动程序。本来就是半桶水的我,还在为等待打开一个页面而磨平了耐心。本想依葫芦画瓢把这个驱动做了。可网上找到的案例都是一些简单的调用dll。对于各种传参、获取返回值和一些常见错误的文章太少(可能因为网络不好一些优质文章还没有点开就被关掉了)。今天ITDragon就做一个简单的葫,以广播驱动作为案例。

1.The specified module could not be found.

2.%1 is not a valid Win32 application.

3.The operation completed successfully.

4.error: unknown type name 'HWND'、'DWORD'.

5.获取dll返回的结构体

6.dll传参unsigned char* argName, struct _PlayParam* pParam

调用LCAudioThrDll 案例

ITDragon龙 先画一个葫芦。这个dll是在做广播驱动时用到,列举了其中几个有代表性的方法介绍。

package main

/*
#include <stdlib.h>
typedef struct _PlayParam
{
long hWnd; //主窗口的句柄,如果不为0,则线程有事件会向该窗口发送消息
int Priority; //优先级
int MultiGroup; //多播组号
int CastMode; //传输模式,单播,多播,广播
long IP; //ip,如果是广播和多播,此参数是源网卡的IP,如果此地址为0,则由系统决定使用哪个网卡,如果是单播,这是个目标设备的ip地址。
int Volume; //播放音量取值0~100
int Tone; //音调
int Treble; //高音频率
int Bass; //低音频率
int Treble_En; //高音增益
int Bass_En; //低音增益
unsigned short SourceType; //输入源,0为文件,1为声卡
unsigned short OptionByte; //选项字,默认为0;bit0=1 禁止重采样,bit1=1,启动监听,bit2=1,禁用解码功能(仅播放符合要求的音频文件)
int DeviceID; //音频输入ID号 1~N
int MaxBitrate; //允许最大的比特率组合,如果源文件高于此比特率,将被重压缩至此比特率。
unsigned int Option[15]; //选项
int nChannels; //采样的通道 1~2 CodecType
int nSamplesPerSec; //采样频率 8K,11.025K,22.05K,44.1K
int AudioBufferLength; //Audio数据的长度
unsigned char* AudioBuf; //Audio数据的指针
unsigned int PrivateData[128]; //私有信息,lc_init初始化后,用户不能修改里面的内容。
}PlayParam;
*/
import "C"
import (
"fmt"
"os"
"strconv"
"strings"
"syscall"
"unicode"
"unsafe"
) /*
struct _PlayParam* __stdcall lc_play_getmem (void);
int __stdcall lc_init(unsigned char* pFileName, struct _PlayParam* pParam);
int __stdcall lc_play(struct _PlayParam* pParam);
int __stdcall lc_set_volume(struct _PlayParam* pParam, char volume);
int __stdcall lc_addip (struct _PlayParam* pParam,DWORD ip);
*/ var (
lcAudioSdk, _ = syscall.LoadDLL("LCAudioThrDll.dll")
lcAudioSdkPlayGetMemFunc, _ = lcAudioSdk.FindProc("lc_play_getmem")
lcAudioSdkInitFunc, _ = lcAudioSdk.FindProc("lc_init")
lcAudioSdkPlayFunc, _ = lcAudioSdk.FindProc("lc_play")
lcAudioSdkSetVolumeFunc, _ = lcAudioSdk.FindProc("lc_set_volume")
lcAudioSdkAddIPFunc, _ = lcAudioSdk.FindProc("lc_addip")
) func main() {
filePath := `D:\upload\attachment\20200217115847582_-581698856.mp3`
if IsIllegalFile(filePath) {
return
} audioSource := C.CString(filePath)
defer C.free(unsafe.Pointer(audioSource))
var playParam *C.PlayParam
/**
step1 申请PlayParam内存
1. 无参
2. 获取并转换dll 返回结构体指针
*/
playParamMem, _, _ := lcAudioSdkPlayGetMemFunc.Call()
playParam = (*C.PlayParam)(unsafe.Pointer(playParamMem))
playParam.Volume = 80
playParam.SourceType = 0
playParam.CastMode = 0
playParam.IP = C.long(ipAddrToInt("127.0.0.1")) /**
step2 初始化客户端
1. 入参是unsigned char* 和 struct _PlayParam*
2. 获取并转换dll 返回int类型
*/
initResult, _, _ := lcAudioSdkInitFunc.Call(uintptr(unsafe.Pointer(audioSource)), uintptr(unsafe.Pointer(playParam)))
fmt.Println("lcaudio init result : ", int32(initResult)) /**
step3 播放音频
1. 入参是struct _PlayParam*
2. 获取并转换dll 返回int类型
*/
playResult, _, _ := lcAudioSdkPlayFunc.Call(uintptr(unsafe.Pointer(playParam)))
fmt.Println("lcaudio play result : ", int32(playResult)) /**
step4 调整音量
1. 入参是struct _PlayParam* 和 char (疑惑)
2. 获取并转换dll 返回int类型
*/
volumeResult, _, _ := lcAudioSdkSetVolumeFunc.Call(uintptr(unsafe.Pointer(playParam)), uintptr(90))
fmt.Println("lcaudio set volume result : ", int32(volumeResult)) /**
step5 单播模式添加IP设备
1. 入参是struct _PlayParam* 和 DWORD
2. 获取并转换dll 返回int类型
*/
addIpResult, _, _ := lcAudioSdkAddIPFunc.Call(uintptr(unsafe.Pointer(playParam)), uintptr(C.long(ipAddrToInt("192.168.0.5"))))
fmt.Println("lcaudio add ip result : ", int32(addIpResult)) } // ip地址转16进制
func ipAddrToInt(ipAddr string) int {
bits := strings.Split(ipAddr, ".")
b0, _ := strconv.Atoi(bits[0])
b1, _ := strconv.Atoi(bits[1])
b2, _ := strconv.Atoi(bits[2])
b3, _ := strconv.Atoi(bits[3])
var sum int
sum += int(b0) << 24
sum += int(b1) << 16
sum += int(b2) << 8
sum += int(b3) return sum
} // 文件校验
func IsIllegalFile(filePath string) bool {
if IsChineseChar(filePath) {
return true
}
if !PathExists(filePath) {
return true
} return false
} func IsChineseChar(str string) bool {
for _, r := range str {
if unicode.Is(unicode.Scripts["Han"], r) {
return true
}
}
return false
} func PathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}

填坑

1. The specified module could not be found.

在执行syscall.LoadDLL时报错。如果报这个错,可以考虑从两个方面找问题:

第一:有可能是dll路径不对。

第二:有可能是当前dll所需要的其他dll丢失。

第一种情况很好解决,换一个全英文路径试一试。第二种情况需要借助DependenciesGui工具查找dll的依赖项。不推荐用depends22这个工具。将缺失的dll下载并放在当前dll同一层目录即可(或者放在系统目录),只要黄色感叹号消失即可。

2. %1 is not a valid Win32 application.

一般是在64位下执行32位的dll会出现这种情况,配置编译环境即可。GOARCH=386;CGO_ENABLED=1

3. The operation completed successfully.

在执行.Call()方法会返回三个参数。其中第三个参数就是error。并且这个error始终不为nil,打印的错误信息是操作已完成???

Golang调用Dll案例的更多相关文章

  1. golang调用c++的dll库文件

    最近使用golang调用c++的dll库文件,简单了解了一下,特作此笔记:一.DLL 的编制与具体的编程语言及编译器无关 dll分com的dll和动态dll,Com组件dll:不管是何种语言写的都可以 ...

  2. Golang调用windows下的dll动态库中的函数

    Golang调用windows下的dll动态库中的函数 使用syscall调用. package main import ( "fmt" "syscall" & ...

  3. Golang调用windows下的dll动态库中的函数 Golang 编译成 DLL 文件

    Golang调用windows下的dll动态库中的函数 package main import ( "fmt" "syscall" "time&quo ...

  4. Windows平台Go调用DLL的坑

    最近的项目中,使用了GO来开发一些服务中转程序.业务比较简单,但是有一些业务需要复用原有C++开发的代码.而在WINDOWS,用CGO方式来集成C/C++代码并不是太方便.所以用DLL把C++的代码封 ...

  5. Windows平台Go调用DLL的坑(居然有这么多没听过的名词)

    最近的项目中,使用了GO来开发一些服务中转程序.业务比较简单,但是有一些业务需要复用原有C++开发的代码.而在WINDOWS,用CGO方式来集成C/C++代码并不是太方便.所以用DLL把C++的代码封 ...

  6. 全面总结: Golang 调用 C/C++,例子式教程

    作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...

  7. C# 远程调用实现案例

    C#远程调用实现案例 2007年11月19日 13:45:00 阅读数:6012 C#实现远程调用主要用到“System.Runtime.Remoting”这个东西.下面从三个方面给于源码实例. ·服 ...

  8. C#生成DLL,在Unity中导入/调用DLL

    网上搜了一些DLL的创建.编写.使用的学习资料,感觉比较的凌乱.或是复杂抽象,或是关键地方一笔带过,不是很适合萌新.于是决定还是图文记录一下该过程,尽量精简而又明确. 学习资料: https://do ...

  9. Golang 调用 C/C++,例子式教程

    大部分人学习或者使用某样东西,喜欢在直观上看到动手后的结果,才会有继续下去的兴趣. 前言: Golang 调用 C/C++ 的教程网上很多,就我目前所看到的,个人见解就是比较乱,坑也很多.希望本文能在 ...

随机推荐

  1. 探究Dubbo的拓展机制: 下

    承接上篇, 本篇博文的主题就是认认真真捋一捋, 看一下 Dubbo是如何实现他的IOC / AOP / 以及Dubbo SPI这个拓展点的 总览: 本篇的话总体上分成两部分进行展开 第一点就是 Dub ...

  2. mongo repository

    using MongoDB.Driver; namespace Dben.Invoice.Repository { /// <summary> /// 仓储基类 /// </summ ...

  3. VMware Workstation CentOS7 Linux 学习之路(2)--.net core环境安装

    1.为了安装.NET,需要注册微软签名密钥和添加微软相关的支持.这个操作每台机器只能做一次. Add the dotnet product feed(其实就是向微软提交投名状,表示我这台服务器要用co ...

  4. OpenGLES思维导图

    两本书到头来就只剩下了这三张图了吧.想要原图:https://github.com/wangwangla/biji/blob/master/%E8%AF%BB%E4%B9%A6%E7%AC%94%E8 ...

  5. 想玩转JAVA高并发,这些概念你必须懂!

    我们在找工作时,经常在招聘信息上看到有这么一条:有构建大型互联网服务及高并发等经验,你第一时间想到的是媒体常说的双十一吗?带着问题,我们一起思考技术…. 高并发高并发 它是互联网分布式系统架构设计中必 ...

  6. Go 每日一库之 fsnotify

    简介 上一篇文章Go 每日一库之 viper中,我们介绍了 viper 可以监听文件修改进而自动重新加载. 其内部使用的就是fsnotify这个库,它是跨平台的.今天我们就来介绍一下它. 快速使用 先 ...

  7. Oracle GoldenGate 12.3微服务架构指北

    Microservices Architecture introduction Microservices Architecture is a method or approach to develo ...

  8. 关于selenium无法在chrome中自动播放flash的问题

    最近用selenium写个小脚本,遇到flash不能自动播放问题 我遇到的情况,直接提示 请确认是否安装flash,其实已经安装,点击下载flash,然后提示是否允许. 整了好久,发现终极方法: ## ...

  9. VSCODE更改文件时,提示EACCES permission denied的解决办法(mac电脑系统)

    permission denied:权限问题 具体解决办法: 1.在项目文件夹右键-显示简介-点击右下角解锁 2.权限全部设置为读与写 3.最关键一步:点击"应用到包含的项目",这 ...

  10. 使用luabind绑定box2d的lua接口

    最近在使用luabind绑定box2d的lua接口,发现不少问题.写在这里与大家分享. 1. body,fixture,joint的userdata.box2d的userdata的数据类型是void* ...