System.AccessViolationException:试图读取或写入受保护的内存。使用 MbnApi.dll

System.AccessViolationException: Attempted to read or write protected memory. Using MbnApi.dll

我创建了一个关于测试文件 MbnApi.dll 的程序,这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MbnApi;

namespace TestMbnApi
{
    class Program
    {
        static void Main(string[] args)
        {
            MbnInterfaceManager mbnInfMgr = new MbnInterfaceManager();
            IMbnInterfaceManager infMgr = mbnInfMgr as IMbnInterfaceManager;
            IMbnConnectionManager conManager = (IMbnConnectionManager)new MbnConnectionManager();
            // Connection
            IMbnConnection[] connections = conManager.GetConnections();
            IMbnConnection con = connections.First();
            // Get interfaceID
            String interfaceID = con.InterfaceID;
            Console.WriteLine("InterfaceID : {0}", interfaceID);
            // Get interface
            IMbnInterface interfa = infMgr.GetInterface(interfaceID) as IMbnInterface;
            // GetInterfaceCapability
            MBN_INTERFACE_CAPS caps =  interfa.GetInterfaceCapability();
            Console.WriteLine("Caps :{0}", caps);

            Console.ReadLine();
        }
    }
}

当我在笔记本电脑 windows 10 上生成文件 .exe 和 运行 并且有 sim 卡 3G。然后我收到错误:

Unhandled exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.StubHelpers.ValueClassMarshaler.ConvertToManaged(IntPtr dst, IntPtr src, IntPtr pMT)
   at MbnApi.IMbnInterface.GetInterfaceCapability()
   at TestMbnApi.Program.Main(String[] args)

我使用的是 Frameword .Net 4.6.1 和 Visual Studio 2015 社区版。我不明白为什么这个程序会出错?有没有人可以支持我。 谢谢大家!

我刚刚解决了这个问题。 要解决此问题,请使用与您正在使用的操作系统相对应的 MbnApi.dll。我正在使用 window 10 并且我已经为此操作构建了 MbnApi.dll。 为了构建 MbnApi.dll,我在下面的 link 之后得到了 Window SDK 版本 10: [https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk][1]

下载Window SDK后,请访问文件夹: C:\Program Files (x86)\Windows Kits\Lib\winv6.3\um\x64 然后将文件 mbnapi.tlb 复制到包含文件 TlbImp.exe 的文件夹中。 并访问该文件夹和 运行 命令: TlbImp.exe mbnapi.tlb /out:MbnApi.dll 所以你构建 MbnApi.dll 成功。

请尝试一下,如果有问题或成功请反馈给我。

谢谢和最好的问候,