【问题标题】:AccessViolationException - When calling Botan::LibraryInitializerAccessViolationException - 调用 Botan::LibraryInitializer 时
【发布时间】:2015-06-12 00:42:25
【问题描述】:

我正在 .NET 中为 Botan crypto 构建托管包装器,并按照此处的入门说明进行操作

Getting Started

还有library reference

我正在尝试首先执行 LibraryInitializer,但是当我调用它时,它会在我的 INIT() 方法中引发 AccessViolationException。

我的代码是这样的。

C# 测试程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotanCrypto_ManagedWrapper;
using System.Numerics;

namespace TestBotanWrapper
{
    class Program
    {
        static void Main(string[] args)
        {
            BotanCrypto BC = new BotanCrypto();
            BC.INIT();


            UInt64 num = 100;

            BigInteger b = BC.Test(num);


            Console.WriteLine(b);
            Console.ReadKey();
        }
    }
}

包装 CPP

// This is the main DLL file.

#include "stdafx.h"

#include "BotanCrypto_ManagedWrapper.h"
using namespace BotanCrypto_ManagedWrapper;

void BotanCrypto::INIT(){
    LibraryInitializer init;

}

BigInteger BotanCrypto::Test(UInt64 x){
    //try{
        BigInt n = 1000000;
        x = 2;
    //}catch(SystemException^ ex){

    //  x = 0;
    //}
    return x;
}

包装标题

// BotanCrypto_ManagedWrapper.h

#pragma once

using namespace System;
using namespace Numerics;
using namespace Botan;

namespace BotanCrypto_ManagedWrapper {

    public ref class BotanCrypto
    {
        // TODO: Add your methods for this class here.
    public:
        BigInteger Test(UInt64 k);
        void INIT();
    };


}

我什至不知道我是否正确调用了 libraryinitializer。我对 C++ 不太熟悉。任何帮助表示赞赏。谢谢。

编辑我在 Win32 控制台应用程序中尝试了相同的操作,但得到了相同的结果

#include "stdafx.h"
#include <botan/botan.h>

int _tmain(int argc, _TCHAR* argv[])
{
   try
      {
      Botan::LibraryInitializer init;

      // ...
      }
   catch(std::exception& e)
      {
      //std::cerr << e.what() << "\n";
      }
    return 0;
}

ConsoleApplication4.exe 中 0x0F12422E (botan.dll) 处未处理的异常:0xC0000005:访问冲突读取位置 0x003B0000。

【问题讨论】:

  • 我会在WinDbg 下得到这个,然后在0x003B0000 上查看加载(或应该加载)的内容。如果它在您的 DLL 中,那么您应该能够获得一个符号。我认为您的另一个选择是在 Visual Studio 下启用本机代码调试。为此,请参阅 How to: Enable Debugging of Unmanaged CodeDebugging unmanaged code while debugging managed code
  • 一个问题 - 该页面提到LibraryInitializer 的生命周期必须超过所有其他 Botan 对象的生命周期。这意味着您应该为您的 BotanCrypto 类创建一个类构造函数,并将LibraryInitializer init 放在那里。您的代码只是通过将其塞入该 INIT 函数来创建和销毁。

标签: c# c++-cli botan


【解决方案1】:

我会改变:

LibraryInitializer init;

到:

try
{
   LibraryInitializer init;
}
catch(std::exception& e)
{
    std::cerr << e.what() << "\n";
}

如入门页面中的陷阱所述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2013-01-02
    • 1970-01-01
    相关资源
    最近更新 更多