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. GXOI&GZOI

    T1 与或和   2s&&512MB   简明题意:求一个矩阵的所有子序列的 \(and\)和 和\(or\)和: 子矩阵的\(and\)和就是所有值\(and\)起来:\(or\)类 ...

  2. DevExpress 控件用法笔记(VB)

    1.ChartControl 显示条形图 ChartControl1.Titles.Clear() ChartControl1.Series.Clear() Dim db As DataTable S ...

  3. must appear in the GROUP BY clause or be used in an aggregate function

    今天在分组统计的时候pgsql报错 must appear in the GROUP BY clause or be used in an aggregate function,在mysql里面是可以 ...

  4. Node: 使用nrm管理npm源

    一.简介 npm是一款非常好用的包管理工具,在前端开发中很多时候都会使用npm安装其他包文件.但是,npm安装某些包时有时会安装地很慢,这是因为npm管理的源中有些是国外的,包下载的时候需要花费很多时 ...

  5. Oracle安装连接常见错误

    oracle安装注意:安装路径url不能带中文C:\app\59428\product\11.2.0\dbhome_1\sqldeveloper打开sqldeveloper的时候,需要输入java.e ...

  6. Mysql-SQL优化-子查询替代LEFT JOIN

    表A:批次信息表, 表B:实际批次明细表, Mysql版本:5.6.36 两表之间的数据体量差异:表B是表A的10000倍. 经过结转,表B通常保留 1千5百万数据.表A就是1千多条数据. 计算近24 ...

  7. 使用远程接口库进一步扩展Robot Framework的测试能力

    引言: Robot Framework的四层结构已经极大的提高了它的扩展性.我们可以使用它丰富的扩展库来完成大部分测试工作.可是碰到下面两种情况,仅靠四层结构就不好使了: 1.有些复杂的测试可能跨越多 ...

  8. python+opencv中最近出现的一些变化( OpenCV 官方的 Python tutorial目前好像还没有改过来?) 记一次全景图像的拼接

    最近在学习过程中发现opencv有了很多变动, OpenCV 官方的 Python tutorial目前好像还没有改过来,导致大家在学习上面都出现了一些问题,现在做一个小小的罗列,希望对大家有用 做的 ...

  9. url的分发

    一.分发 补充:通过查看源码:可以通过二级路由include进行二次分发 位置:urls.py urlpatterns = [ path('admin/', admin.site.urls), pat ...

  10. session跨域丢失问题

    配置一个 filter package com.psm.filter; import javax.servlet.*; import javax.servlet.http.HttpServletReq ...