AccessViolationException - 调用Botan :: LibraryInitializer时

时间:2015-06-12 00:42:26

标签: c# c++-cli botan

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

Getting Started

library reference

我首先尝试执行LibraryInitializer,但是当我调用它时会抛出一个AccessViolationException - 在我的INIT()方法中。

我的代码就像这样。

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();
        }
    }
}

Wrapper 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();
    };


}

我甚至不知道我是否正确地调用了库初始化程序。我对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。

1 个答案:

答案 0 :(得分:0)

我会改变:

LibraryInitializer init;

为:

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

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