当前位置: 移动技术网 > IT编程>开发语言>c# > C#调用EnumDevice获取设备信息

C#调用EnumDevice获取设备信息

2020年04月12日  | 移动技术网IT编程  | 我要评论
本文接上篇文章 C#获取设备(Audio和Video)名称 简单整理,对第四种方式使用整理. EnumDevice.dll是网上下载的,也下载了对应的源代码, 对应dll:https://download.csdn.net/download/QQ81867376/12322158 该dll的源码: ... ...

本文接上篇文章 c#获取设备(audio和video)名称 简单整理,对第四种方式使用整理.

enumdevice.dll是网上下载的,也下载了对应的源代码,

对应dll:https://download.csdn.net/download/qq81867376/12322158

该dll的源码: https://download.csdn.net/download/qq81867376/12322152

由于项目刚好是x86,所以直接使用上面下载的dll,暂未去编译源代码。

c# 调用enumdevice.dll的方法时候遇到不少问题,在此记录下。

查看c++函数信息,可以使用工具dllexportviewer

下载地址:

 

该dll的原型

__declspec(dllimport)enumdevice(capture_device_type type, char * devicelist[], int nlistlen, int & inumcapdevices);

由于未在前面添加extern "c" 一直找不到该方法,暂时使用了索引来进行。

如果函数过多且经常变化使用索引不恰当 ,但这里只有一个方法,因此无碍,就直接使用索引。

c#方法声明 这里耽误点时间,开始仅仅以为按照类型对上即可,定义了如下方法:

          //lpstr、lpwstr、bstr 或 lptstr  
        [dllimport(enumdevicedll, entrypoint = "#1", charset = charset.ansi, callingconvention = callingconvention.cdecl)]
        public extern static int enumdevice(capture_device_type type,
           [in, out, marshalas(unmanagedtype.lparray, arraysubtype = unmanagedtype.lpstr)] string[] devicelist,
           int nlistlen, ref int inumcapdevices);
public static list<string> getdevicelist()
        {
            var list = new string[10];
            int index = 0;           
            int result = enumdevice(capture_device_type.dshow_audio_device, list, list.length, ref index);
            list<string> listaudio = null;
            if (result == 0)
            {
                listaudio = new list<string>();

                foreach (var item in list)
                {
                    if (string.isnullorempty(item))
                    {
                        continue;
                    }
                    listaudio.add(item);
                }
            }
            return listaudio;
        }

 

一运行就报错:其他信息: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏 .

无奈 就查看源代码,发现每一项没有指定大小.就添加了代码

  for (int i = 0; i < list.length; i++)
            {
                list[i] = new string(new char[256]);
            }

运行果然没有报错,

可是有乱码和用ffmpeg调用指令 (ffmpeg -list_devices true -f dshow -i dummy) 输出的结果一样乱码.

于是各种转码无效, 各种定义函数 charset = charset.ansi,

arraysubtype = unmanagedtype.lpstr //lpstr、lpwstr、bstr 或 lptstr 。

试了个遍全都无效,无奈只能换思路了。

 

修改对应类型,c++里面的 char*,正常直接可以用c#里面string或者char *,

之前使用c#调用ffmpeg的api播放rtmp协议的视频和语音的时候 好像使用过,看了下之前代码,重新定义了接口

  [dllimport(enumdevicedll, entrypoint = "#1", callingconvention = callingconvention.cdecl)]
        public extern static int enumdevice(capture_device_type type,
             [in, out, marshalas(unmanagedtype.lparray)]  intptr[] devicelist,
           int nlistlen, ref int inumcapdevices);

 

调用赋值

   var list = new intptr[10];
            int index = 0;
         for (int i = 0; i < list.length; i++)
            {
              list[i] = marshal.allochglobal(256);
              }

直接给每一项分配内存,marshal.allochglobal(256)

结果是 无法或者到底有多个设备列表, 除了正常外 其他全是乱码,

后来就用字符串来代替,

var stringempty = new string(new char[256]);

list[i] = marshal.stringtohglobalansi(stringempty);

终于一切都正常了,c#调用一个vc++的函数,就耽误个把小时。           

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网