有没有大神帮下,帮我把这个DLL声明一下,一直在vb.net中调用不成功

nbsmaps 2020-02-05 10:41:03


sing System;
using System.Text;
using System.Runtime.InteropServices;

namespace LCAudioDll
{
#region 文件结构参数定义
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public unsafe struct _Fileinfo
{
public int Duration; //play Duration
public int SampleRate; //audio samplerate
public int BitRate; //audio bitrate
}
#endregion

#region 音频结构参数定义
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public unsafe struct _WaveInInfo
{
public int Index; //The first soundcard is index 0,the second soundcard is index 1, and so on.
public int name; //soundcard's name which is selected.
public int Formats; //(unuse)
public int Channels; //(unuse)
}
#endregion

#region 混音器结构参数定义
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public unsafe struct _MuxInfo
{
public char name; //Mixer Name
}
#endregion

#region 动态库结构参数定义
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public unsafe struct _PlayParam
{
/// Windows窗口句柄,如果不为NULL,线程将事件消息发送到此窗口
public UInt32 hWnd;
/// 音频流优先级,1优先级最低,255优先级最高,音频终端优先播放优先级高的音频。
public Int32 Priority;
/// 组播组号,表示线程向那个组发送音频数据,当播放方式是单播或广播是,此参数无意义。
public Int32 MultiGroup;
/// 播放方式,单播,组播和广播
public Int32 CastMode;
/// IP地址,当为播放方式单播是指的是目标IP地址,为组播和广播时,表示使用的本地网络接口
public UInt32 IP;
///播放音量
public Int32 Volume;
///播放音调
public Int32 Tone;
/// 高音频率
public Int32 Treble;
/// 低音频率
public Int32 Bass;
/// 高音放大因子
public Int32 Treble_En;
/// 低音放大因子
public Int32 Bass_En;
/// 音频数据源,0表示数据源为文件,1表示数据源为声卡输入(Line in 或 mic输入)
public short SourceType;
/// 声卡采样开关变量
public short OptionByte;
/// 声卡ID号,仅在SourcType = 1时有效,表示采用哪个声卡的输入。
public Int32 DeviceID;
/// 混音器名字,表示混音器的那个输入通道最终被采样。不同的声卡有不同的混音器名字。
public fixed byte MuxName[64];
/// 通道,采样的通道数
public Int32 nChannels;
/// 音源为声卡输入时的采样频率
public Int32 nSamplesPerSec;
/// 音频文件的长度,仅SourcType=0时有效
public Int32 AudioBufferLength;
/// 音频数据存储地址
public IntPtr AudioBuf;
/// 私有数据数组,用户不能修改里面的数据。
public fixed Int32 PrivateData[128];
}
#endregion

#region 动态裤函数导入
public unsafe class LCAudioThrDll
{
private const string LCAudioThrDllName = "LCAudioThrDll.dll";

//(1) Initialize the _PlayParam structure.
[DllImport(LCAudioThrDllName, EntryPoint = "lc_init")]
public extern static int Lc_Init(string filename, _PlayParam* pParam);

//(2) Start playing audio data
[DllImport(LCAudioThrDllName, EntryPoint = "lc_play")]
public static extern int Lc_Play(_PlayParam* pParam);

//(3) Stop playing audio data
[DllImport(LCAudioThrDllName, EntryPoint = "lc_stop")]
public static extern int Lc_Stop(_PlayParam* pParam);

//(4) Pause playback audio data
[DllImport(LCAudioThrDllName, EntryPoint = "lc_pause")]
public static extern int Lc_Pause(_PlayParam* pParam);

//(5) Continue playing audio data
[DllImport(LCAudioThrDllName, EntryPoint = "lc_continue")]
public static extern int Lc_Continue(_PlayParam* pParam);

//(6) Specify play time point
[DllImport(LCAudioThrDllName, EntryPoint = "lc_seek")]
public static extern int Lc_Seek(_PlayParam* pParam, Int32 time);

//(7) Wait for the play to finish
[DllImport(LCAudioThrDllName, EntryPoint = "lc_wait")]
public extern static int Lc_Wait(_PlayParam* pParam);

//(8) Dynamically adjust the volume
[DllImport(LCAudioThrDllName, EntryPoint = "lc_set_volume")]
public static extern int Lc_Set_Volume(_PlayParam* pParam, Int32 Volue);

//(9) Get the played time
[DllImport(LCAudioThrDllName, EntryPoint = "lc_get_playtime")]
public static extern int Lc_Get_PlayTime(_PlayParam* pParam);

//(10) Get play status
[DllImport(LCAudioThrDllName, EntryPoint = "lc_get_playstatus")]
public extern static int Lc_Get_PlayStatus(_PlayParam* pParam);

//(11) Get the total play time of the current file
[DllImport(LCAudioThrDllName, EntryPoint = "lc_get_duration")]
public static extern int Lc_Get_Duration(_PlayParam* pParam);

//(12) Get the information of the specified file
[DllImport(LCAudioThrDllName, EntryPoint = "lc_get_fileinfo")]
unsafe public static extern int Lc_Get_FileInfo(string filename, ref _Fileinfo myfileinfo);

//(13) Get the information of the sound card
[DllImport(LCAudioThrDllName, EntryPoint = "lc_rec_devinfo")]
public extern static int Lc_Rec_DevInfo(_WaveInInfo* Info, ref int Number);

//(14) Get the information of the sound mixer
[DllImport(LCAudioThrDllName, EntryPoint = "lc_rec_muxinfo")]
public extern static int Lc_Rec_MuxInfo(_MuxInfo* Info, ref int Number);

//(15) Get the version information of dynamic library
[DllImport(LCAudioThrDllName, EntryPoint = "lc_get_version")]
public extern static int Lc_Get_Version();

//(16) Get the last error message
[DllImport(LCAudioThrDllName, EntryPoint = "lc_getlasterror")]
public extern static int Lc_GetLastError(_PlayParam* pParam);

//(17) Start to record audio data
[DllImport(LCAudioThrDllName, EntryPoint = "lc_record_start")]
public static extern int Lc_Record_Start(_PlayParam* pParam, string filename);

//(18) Stop to record audio data
[DllImport(LCAudioThrDllName, EntryPoint = "lc_record_stop")]
public static extern int Lc_Record_Stop(_PlayParam* pParam);

//(19) Query recording status
[DllImport(LCAudioThrDllName, EntryPoint = "lc_record_status")]
public static extern int Lc_Record_Status(_PlayParam* pParam);

//(20) Allocate memory
[DllImport(LCAudioThrDllName, EntryPoint = "lc_play_getmem")]
public static extern _PlayParam* Lc_Play_GetMem();

//(21) Free memory
[DllImport(LCAudioThrDllName, EntryPoint = "lc_play_freemem")]
public static extern int Lc_Play_FreeMem(_PlayParam* Plm);

//(22) Add unicast device
[DllImport(LCAudioThrDllName, EntryPoint = "lc_addip")]
public static extern int Lc_AddIP(_PlayParam* pParam, uint ip);

//(23) Delete unicast device
[DllImport(LCAudioThrDllName, EntryPoint = "lc_delip")]
public static extern int Lc_DelIP(_PlayParam* pParam, uint ip);

//(24) Input audio data of WAV to the dynamic library
[DllImport(LCAudioThrDllName, EntryPoint = "lc_inputdata")]
public static extern int Lc_InputData(_PlayParam* pParam, byte[] buf, int datalen);

//(25) Input audio data of MP3 to the dynamic library
[DllImport(LCAudioThrDllName, EntryPoint = "lc_inputdata_mp3")]
public static extern int Lc_InputData_Mp3(_PlayParam* pParam, byte[] buf, int datalen);
}
#endregion
}
上面是C# 例子,如何改为VB。net
我自个改了,好象调用不成功,
Option Explicit On
Imports System
Imports System.Text
Imports System.Runtime.InteropServices
Module Module1
'定义网络通信方式
Public Const cUnicast = 0 '单播
Public Const cMulticast = 1 '组播
Public Const cBroadcast = 2 '广播

Public Const AUDIO_TYPE_UNKNOWN = 0
Public Const AUDIO_TYPE_MP3 = 1
Public Const AUDIO_TYPE_WAV = 2
Public Const AUDIO_TYPE_WMA = 3

Public Const SCR_TYPE_FILE = 0 '数据来源是文件
Public Const SCR_TYPE_AUDIOCARD = 1 '数据来源是声卡
Public Const SCR_TYPE_BUFFER = 2 '数据来源是内存缓存
Public Const SCR_TYPE_STREAM = 3 '数据来源是数据流'V2.0.3.0

'定义函数返回值
Public Const R_OK = 0 '成功
Public Const ERR_PARAM = -1 '参数错误
Public Const ERR_OPT = -2 '函数执行错误
Public Const ERR_SOCKET = -3 'socket操作失败
Public Const ERR_CODEC = -4 '初始化编解码器失败。

'Windows 消息定义
Public Const WM_USER = &H400
'public const WM_MSG_EXCEPTION (WM_USER+100)
Public Const WM_MSG_COMPLETED = (WM_USER + 101)
Public Const WM_MSG_PAUSE = (WM_USER + 102)
Public Const WM_MSG_CONTINUE = (WM_USER + 103)
Public Const WM_MSG_AUDIOPOWER = (WM_USER + 104)

'Public Const WM_MSG_DATA = (WM_USER + 9901)

#Region "文件结构参数定义"
Public Structure FileInfo
Public Duration As Long '播放时长
Public SampleRate As Long '采样率
Public BitRate As Long '比特率
End Structure
#End Region

#Region "音频结构 声卡信息结构"

Public Structure WaveInInfo
Public Index As Long '输入通道的序号
Public name As Long '名字
Public Formats As Long '支持的格式
Public Channels As Long '通道数 2为立体声输入。
End Structure
#End Region

#Region "混音器名称"
Public Structure MuxInfo
<VBFixedArray(63)> Public name() As Byte '名字
End Structure
#End Region

#Region "线程播放结构"
' <StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)>
Public Structure PlayParam
Public hWnd As Long 'Windows窗口句柄,如果不为NULL,线程将事件消息发送到此窗口主窗口的句柄
Public Priority As Long '音频流优先级 1最低 255最高
Public MultiGroup As Long '组播组号,表示线程向那个组发送音频数据,当播放方式是单播或广播是,此参数无意义。
Public CastMode As Long '播放方式,单播,组播和广播
Public IP As Long 'ip,如果是广播和多播,此参数是源网卡的IP,如果此地址为0,则由系统决定使用哪个网卡,如果是单播,这是个目标设备的ip地址。
Public Volume As Long '播放音量取值0~100
Public Tone As Long '音调
Public Treble As Long
...全文
923 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
良朋 2020-02-17
  • 打赏
  • 举报
回复
C# 中的int是 int32的简写, 对应的int64就是long,int16就是short;
例子中的 public int Index,vb.net中你要写为public index as integer

还有最基本的名字定义你都搞错了,MuxInfo在C#中前面有个_(下划线)。还有很多你都没翻译。

翻这个要耐心。C#能写出来的,vb.net都有对应的语法。这个真心不难。
ansnan 2020-02-09
  • 打赏
  • 举报
回复
看看学习学习下
hotao7673 2020-02-08
  • 打赏
  • 举报
回复
66666666666666666666666
捡破烂攻城狮 2020-02-08
  • 打赏
  • 举报
回复
可以尝试拿人家c#编译好的程序用ilspy等工具反编译,查看的反编译代码改成vb. net
捡破烂攻城狮 2020-02-08
  • 打赏
  • 举报
回复
项目要改成不安全代码
fanruinet 2020-02-08
  • 打赏
  • 举报
回复
还有,C#结构体有个属性[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]你翻译了吗?我没看到VB里有这句话
fanruinet 2020-02-08
  • 打赏
  • 举报
回复
VB.NET的long对应C#的long,64位带符号整数 VB.NET的Integer对应C#的int,32位带符号整数
nbsmaps 2020-02-08
  • 打赏
  • 举报
回复
58627564我的QQ号
nbsmaps 2020-02-08
  • 打赏
  • 举报
回复
另外厂家也就这个样子,只有一个会c#的,花了几天也没搞定。而客户那边原来的软件都是VB.net 要接入他们程序才能测试,才现在这个局面。有空吗,有空的话,帮我写个示例吧。
捡破烂攻城狮 2020-02-08
  • 打赏
  • 举报
回复
引用 15 楼 nbsmaps的回复:
[quote=引用 13 楼 捡破烂程序员 的回复:] 可以尝试拿人家c#编译好的程序用ilspy等工具反编译,查看的反编译代码改成vb. net
我有这个水平,也就不用求助了。。。。。。。水平也就盘底一点了。[/quote] 官方没有测试代码提供给客户端开发人员吗
nbsmaps 2020-02-08
  • 打赏
  • 举报
回复
引用 14 楼 清晨曦月 的回复:
辣就是你粗心哈。。sizeof得到的结果是一样的才对
很感谢你的耐心回复。 不是粗心,只是水平低,学习中,毕竟我的主要工作是硬件的程序,现在为了测试硬件,不得不硬着头皮。不然硬件没法调试,交付。所以才想着花点钱(硬件程序对方给了1K,都两个月没,一直没调试不出来,对不起人家,所以才想着有人帮,分点开发费快速搞搞定)
nbsmaps 2020-02-08
  • 打赏
  • 举报
回复
引用 13 楼 捡破烂程序员 的回复:
可以尝试拿人家c#编译好的程序用ilspy等工具反编译,查看的反编译代码改成vb. net
我有这个水平,也就不用求助了。。。。。。。水平也就盘底一点了。
清晨曦月 元老 2020-02-08
  • 打赏
  • 举报
回复
辣就是你粗心哈。。sizeof得到的结果是一样的才对
清晨曦月 元老 2020-02-07
  • 打赏
  • 举报
回复
[DllImport(LCAudioThrDllName, EntryPoint = "lc_play_getmem")]
public static extern _PlayParam* Lc_Play_GetMem();
就是分配一个内存,只要你不会收再重新申请,那它就满足他所说的条件。注意把这个指针声明为与播放过程相同的生存期,免得被垃圾回收了。
这样,其实不一定非得用指针,_PlayParam* 可以用intptr来保存,使用时也用intptr。。

你这个指望大家给你改肯定不行,没有dll没法测试。你先把那个lc_getlaseterror声明对了。然后按照你调用的过程,逐步调试直到这个API返回的值表明其它API调用正确,你的播放内核部分也就做完了。
nbsmaps 2020-02-07
  • 打赏
  • 举报
回复
另附上DLL厂家提供的文档及示例: 链接:https://pan.baidu.com/s/1Y5kAqrfN93ULhObXgbiAoQ 提取码:2m6d 问下,这里不能有偿发布吗?若真有朋友帮完成,表达一下心意也应该啊。
nbsmaps 2020-02-07
  • 打赏
  • 举报
回复
谢谢版主,两天试下来,感觉问题还是出在结构体大小不一样的原因,在c#中用SIZEof函数,取得结构体大小为0x0000000C 即12字节,而同样结构体在VB.net中却有24字节,代码如下 vb.net 结构体定义: Public Structure FileInfo Public Duration As Long '播放时长 Public SampleRate As Long '采样率 Public BitRate As Long '比特率 End Structure 窗体加载中运行: Dim afileinfo As New FileInfo aaa = System.Runtime.InteropServices.Marshal.SizeOf(afileinfo) MsgBox(Len(afileinfo), vbExclamation, "错误") aaa为24 按这个应是正确,LONG是8字节 但在C#中:[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public struct _Fileinfo { public int Duration; //play Duration public int SampleRate; //audio samplerate public int BitRate; //audio bitrate } private int DelayNum = 0; //Current count of delay _Fileinfo abc; DelayNum = sizeof (_Fileinfo); DelayNum 却=12 按C#提示 INt32,应是对应LONG 也是8字节?
清晨曦月 元老 2020-02-07
  • 打赏
  • 举报
回复
你要明白,C#保留了使用*的方法,但实际上不用*也可以用指针,指针是一个指向内存地址的变量,归根结底它就是一个变量而已。C#即使不使用不安全代码也可以使用指针,只不过是被封装过的指针,在IL解释之后,它还是那个指向内存地址的变量,.NET只是包装了一下,附加了其它信息而已,而运行时会解析这些信息。C#与VB.NET只有少数不同的地方,VB.NET无法使用的特性也非常少,并且VB.NET完全可以操作指针,即使不使用marshal也可以得到自己的函数地址,可以得到数组地址,字符串地址…………如果使用封装好的marshal类,那么就能更简单的获取和使用这些东西。这些都不是问题,但是VB.NET中就是没有*,用Intptr就可以了,甚至可以用Int64,integer这都可以。


如果想深入理解指针,你可以用vc++写一段代码,声明几个变量,试一下* & 之类的操作符。并不一定要在代码中输出地址,可以 用一些调试工具,反编译一下,看看数据在哪,指着存在哪个地址里,如果能够理解ASM那就更好了,能够看到编译后的代码让CPU如何通过指针操作数据,如何通过变量操作数据,对应起来看看就全都明白了。指针不是多么难多么难,至少看看这些东西之后简单的指针操作还是可以理解的非常好的。

归根结底,编程语言就是一种工具,里面的各种东西也都是工具,使用哪种工具时,有兴趣就可以把它拆了看看内部构造,玩不坏的,还能学不少东西。例如你这个DLL,声明有问题,出在哪个变量里面,到底是指针还是结构,到底是byval还是byref完全可以把这个DLL反编译一下,浏览一下它的原型,最多看一下对参数的操作过程,非常容易就把调用方式确定下来了。
nbsmaps 2020-02-06
  • 打赏
  • 举报
回复
好象是不安全的代码只有在UNLSafeg下出现
nbsmaps 2020-02-06
  • 打赏
  • 举报
回复
谢谢,试过了,vs2010 VS2019提示“未知故障” 然后就没了。还有一次提示别的,具体忘了。我晚上再详细试下
jhonsonzhang 2020-02-06
  • 打赏
  • 举报
回复
新建c#winform类库项目: 把厂家给的初始类库代码粘贴覆盖上去。删掉原来的设计代码: 点击生成。 vb.net项目中,引用成功生成的类库: 如上图的LCAudioDll.dll。 ok了,最后效果
加载更多回复(3)

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧