• <small id='Xx6uZ'></small><noframes id='Xx6uZ'>

    <i id='Xx6uZ'><tr id='Xx6uZ'><dt id='Xx6uZ'><q id='Xx6uZ'><span id='Xx6uZ'><b id='Xx6uZ'><form id='Xx6uZ'><ins id='Xx6uZ'></ins><ul id='Xx6uZ'></ul><sub id='Xx6uZ'></sub></form><legend id='Xx6uZ'></legend><bdo id='Xx6uZ'><pre id='Xx6uZ'><center id='Xx6uZ'></center></pre></bdo></b><th id='Xx6uZ'></th></span></q></dt></tr></i><div id='Xx6uZ'><tfoot id='Xx6uZ'></tfoot><dl id='Xx6uZ'><fieldset id='Xx6uZ'></fieldset></dl></div>
  • <tfoot id='Xx6uZ'></tfoot>

    1. <legend id='Xx6uZ'><style id='Xx6uZ'><dir id='Xx6uZ'><q id='Xx6uZ'></q></dir></style></legend>
        <bdo id='Xx6uZ'></bdo><ul id='Xx6uZ'></ul>
      1. COM 对象 C# 将 MMDeviceEnumerator 转换为 IMMDeviceEnumerator Inval

        时间:2023-07-24

        <small id='Gz9mv'></small><noframes id='Gz9mv'>

        <legend id='Gz9mv'><style id='Gz9mv'><dir id='Gz9mv'><q id='Gz9mv'></q></dir></style></legend>
          <bdo id='Gz9mv'></bdo><ul id='Gz9mv'></ul>
          • <i id='Gz9mv'><tr id='Gz9mv'><dt id='Gz9mv'><q id='Gz9mv'><span id='Gz9mv'><b id='Gz9mv'><form id='Gz9mv'><ins id='Gz9mv'></ins><ul id='Gz9mv'></ul><sub id='Gz9mv'></sub></form><legend id='Gz9mv'></legend><bdo id='Gz9mv'><pre id='Gz9mv'><center id='Gz9mv'></center></pre></bdo></b><th id='Gz9mv'></th></span></q></dt></tr></i><div id='Gz9mv'><tfoot id='Gz9mv'></tfoot><dl id='Gz9mv'><fieldset id='Gz9mv'></fieldset></dl></div>
              <tbody id='Gz9mv'></tbody>

                <tfoot id='Gz9mv'></tfoot>

                1. 本文介绍了COM 对象 C# 将 MMDeviceEnumerator 转换为 IMMDeviceEnumerator InvalidCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我没有使用 COM 导入的经验,我只是在使用其他人的代码,但对我不起作用

                  I have no experience with COM Imports and am just working with someone else's code that wasn't working for me

                  抛出 InvalidCastException 的代码行:

                  The line of code that is throwing the InvalidCastException:

                      IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
                  

                  COM 导入:

                  [Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
                  internal class MMDeviceEnumerator
                  {
                  }
                  
                  [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
                  internal interface IMMDeviceEnumerator
                  {
                      [PreserveSig]
                      int EnumAudioEndpoints(EDataFlow dataFlow, DEVICE_STATE dwStateMask, out IMMDeviceCollection ppDevices);
                  
                      [PreserveSig]
                      int GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role, out IMMDevice ppEndpoint);
                  
                      [PreserveSig]
                      int GetDevice([MarshalAs(UnmanagedType.LPWStr)] string pwstrId, out IMMDevice ppDevice);
                  
                      [PreserveSig]
                      int RegisterEndpointNotificationCallback(IMMNotificationClient pClient);
                  
                      [PreserveSig]
                      int UnregisterEndpointNotificationCallback(IMMNotificationClient pClient);
                  }
                  

                  截图:

                  推荐答案

                  这不是很接近,您正在创建一个 .NET 类.让 CLR 知道这实际上是一个 COM 声明并在其他地方实现需要使用 [ComImport] 指令.我会给你最低要求的声明:

                  That's not very close, you are creating a .NET class. Letting the CLR know that this is actually a COM declaration and implemented elsewhere requires using the [ComImport] directive. I'll give you the minimum required declarations:

                  [ComImport]
                  [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6")]
                  [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
                  public interface IMMDeviceEnumerator
                  {
                      // etc..
                  }
                  
                  public static class MMDeviceEnumeratorFactory {
                      private static readonly Guid MMDeviceEnumerator = new Guid("BCDE0395-E52F-467C-8E3D-C4579291692E");
                  
                      public static IMMDeviceEnumerator CreateInstance() {
                          var type = Type.GetTypeFromCLSID(MMDeviceEnumerator);
                          return (IMMDeviceEnumerator)Activator.CreateInstance(type);
                      }
                  }
                  

                  并像这样使用它:

                  IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance();
                  

                  强烈避免使用 [PreserveSig],当方法失败时,您需要发出一声巨响.请注意,此接口已被 NAudio 库包装.

                  Do strongly avoid using [PreserveSig], you want a loud bang when a method fails. Do note that this interface is already wrapped by the NAudio library.

                  这篇关于COM 对象 C# 将 MMDeviceEnumerator 转换为 IMMDeviceEnumerator InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否可以在不添加注册表项的情况下为 COM 互操作注册 .NET 程序集? 下一篇:在哪里可以找到有关 C# MSTSCLib 的文档,特别是 MsRdpClient 类?

                  相关文章

                    <small id='Xh6q6'></small><noframes id='Xh6q6'>

                      <legend id='Xh6q6'><style id='Xh6q6'><dir id='Xh6q6'><q id='Xh6q6'></q></dir></style></legend><tfoot id='Xh6q6'></tfoot>
                        <bdo id='Xh6q6'></bdo><ul id='Xh6q6'></ul>
                      <i id='Xh6q6'><tr id='Xh6q6'><dt id='Xh6q6'><q id='Xh6q6'><span id='Xh6q6'><b id='Xh6q6'><form id='Xh6q6'><ins id='Xh6q6'></ins><ul id='Xh6q6'></ul><sub id='Xh6q6'></sub></form><legend id='Xh6q6'></legend><bdo id='Xh6q6'><pre id='Xh6q6'><center id='Xh6q6'></center></pre></bdo></b><th id='Xh6q6'></th></span></q></dt></tr></i><div id='Xh6q6'><tfoot id='Xh6q6'></tfoot><dl id='Xh6q6'><fieldset id='Xh6q6'></fieldset></dl></div>