Golang调用Dll案例
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案例的更多相关文章
- golang调用c++的dll库文件
最近使用golang调用c++的dll库文件,简单了解了一下,特作此笔记:一.DLL 的编制与具体的编程语言及编译器无关 dll分com的dll和动态dll,Com组件dll:不管是何种语言写的都可以 ...
- Golang调用windows下的dll动态库中的函数
Golang调用windows下的dll动态库中的函数 使用syscall调用. package main import ( "fmt" "syscall" & ...
- Golang调用windows下的dll动态库中的函数 Golang 编译成 DLL 文件
Golang调用windows下的dll动态库中的函数 package main import ( "fmt" "syscall" "time&quo ...
- Windows平台Go调用DLL的坑
最近的项目中,使用了GO来开发一些服务中转程序.业务比较简单,但是有一些业务需要复用原有C++开发的代码.而在WINDOWS,用CGO方式来集成C/C++代码并不是太方便.所以用DLL把C++的代码封 ...
- Windows平台Go调用DLL的坑(居然有这么多没听过的名词)
最近的项目中,使用了GO来开发一些服务中转程序.业务比较简单,但是有一些业务需要复用原有C++开发的代码.而在WINDOWS,用CGO方式来集成C/C++代码并不是太方便.所以用DLL把C++的代码封 ...
- 全面总结: Golang 调用 C/C++,例子式教程
作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...
- C# 远程调用实现案例
C#远程调用实现案例 2007年11月19日 13:45:00 阅读数:6012 C#实现远程调用主要用到“System.Runtime.Remoting”这个东西.下面从三个方面给于源码实例. ·服 ...
- C#生成DLL,在Unity中导入/调用DLL
网上搜了一些DLL的创建.编写.使用的学习资料,感觉比较的凌乱.或是复杂抽象,或是关键地方一笔带过,不是很适合萌新.于是决定还是图文记录一下该过程,尽量精简而又明确. 学习资料: https://do ...
- Golang 调用 C/C++,例子式教程
大部分人学习或者使用某样东西,喜欢在直观上看到动手后的结果,才会有继续下去的兴趣. 前言: Golang 调用 C/C++ 的教程网上很多,就我目前所看到的,个人见解就是比较乱,坑也很多.希望本文能在 ...
随机推荐
- Python学习中的“按位取反”笔记总结
| 疑惑 最近在学习Python的过程中了解到位运算符,但对于按位取反有点迷糊,就比如说~9(按位取反)之后的结果是-10,为什么不是6呢?所以下面就来看看为什么不是6,正确结果是如何计算出来的呢? ...
- Go Web 编程之 静态文件
概述 在 Web 开发中,需要处理很多静态资源文件,如 css/js 和图片文件等.本文将介绍在 Go 语言中如何处理文件请求. 接下来,我们将介绍两种处理文件请求的方式:原始方式和http.File ...
- 原生js面向对象编程-选项卡(点击)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 状态压缩 hdu #10
You are playing CSGO. There are n Main Weapons and m Secondary Weapons in CSGO. You can only choose ...
- 三分钟带你入门GitHub
一,首先,我们来说一下什么是GitHub GitHub是一个基于git打造的开源社区 ,同时也是一个大型同性交友平台 ,作为一个专业的程序员,你非常有必要知道并使用GitHub:作为一个国际化社区,所 ...
- java 赋值运算
注意:在赋值运算的时候,会自动发生数据类型转变 例子 public class test{ public static void main(String[] args){ byte num = 5; ...
- python-review01
# 1.使用while循环输出 1 2 3 4 5 6 8 9 10 count = 0 while count < 10: count += 1 if count == 7: continue ...
- Redis系列-存储hash主要操作命令
Redis系列-存储篇hash主要操作函数小结 hash是一些列key value(field value)的映射表.常常用其存储一些对象实例.相对于把一个对象的各个字段存储为string,存储为ha ...
- 5分钟搭建网站实时分析:Grafana+日志服务实战
原文地址:https://yq.aliyun.com/articles/227006 阿里云日志服务是针对日志类数据一站式服务,用户只需要将精力集中在分析上,过程中数据采集.对接各种存储计算.数据索引 ...
- pc端的弹性布局适配方案
方案及原理:使用rem单位,通过window.onresize来监听浏览器窗口,获取窗口宽度,并改变跟字体大小来达到弹性适配效果. function adaptor(){ //为了便于计算,这里以19 ...