std::bad_alloc using Botan for PKCS#11 - c++

I'm new in C++ and I really stuck using Botan to connect to a hardware cryptography token. I don't know If I missed any setups for libs or dlls.
I built Botan library based on Building Botan library in Windows 10. botan.lib and botan.dll is created in lib folder after building.
Then I create a consoleApplication in Visual Studio 2019 with this simple code:
#include <iostream>
#include <botan/botan.h>
#include <botan/p11.h>
#include <botan/p11_slot.h>
#include <botan/p11_session.h>
#include <botan/p11_module.h>
#include <botan/p11_object.h>
#include <botan/p11_randomgenerator.h>
#include <botan/p11_x509.h>
#include <botan/x509_dn.h>
using namespace Botan;
using namespace PKCS11;
int main()
{
Botan::PKCS11::Module module("C:\\Windows\\System32\\ShuttleCsp11_3003.dll");
// Sometimes useful if a newly connected token is not detected by the PKCS#11 module
module.reload();
Botan::PKCS11::Info info = module.get_info();
// print library version
std::cout << std::to_string(info.libraryVersion.major) << "."
<< std::to_string(info.libraryVersion.minor) << std::endl;
}
This is the settings I prepared to run:
Configuration Properties→VC++ Directories:
Include Directories → add C:\Botan\include\botan-2;
Executable Directories → add C:\Botan\bin;
Library Directory → add C:\Botan\lib;
Source Directory → add C:\Botan\src;
Additional Include Library → add C:\Botan\include\botan-2
Linker
Additional Library Directory → add C:\Botan\lib;
Input → Additional Dependencies → add C:\Botan\lib\botan.lib
Also I installed token driver which dll is in System32 folder;
As I build the Botan Library with x86 so I debug the project with this config:
The error which I need your help to solve is:
Unhandled exception at 0x74CD2CF2 in ConsoleApplication1.exe: Microsoft C++ exception:
std::bad_alloc at memory location 0x004FF1AC.
This error occurred in this line of Code:
Botan::PKCS11::Module module("C:\\Windows\\System32\\ShuttleCsp11_3003.dll");
And this is the call stack
Note that I copied botan.dll and ShuttleCsp11_3003.dll in debug folder.
Somebody please help, thanks

Use Vcpkg, which is a tool created by Microsoft that helps acquire and build open source C and C++ libraries, to install botan automatically using a one liner shell command line and integrate to your VS 2019 project.
After installing vcpkg from GitHub, type the following command from a PowerShell prompt to download and install the library including all the dependencies:
.\vcpkg install botan:x86-windows
Use this to automatically (or you can do it manually) integrate the library to your VS project.
.\vcpkg integrate install
This is serious error here, which is the reason why the namespace and include files are not recognized by your project, Include Directories → add C:\Botan\include\botan-2 is incorrect Check the directory/file name botan-2, it should not exist.
should be C:\Botan\include; as your program includes botan in the folder path (eg #include "botan/botan.h")
Copy the dll files to your project directory( for debug testing) and to your application folder (debug or release version) and don't forget to correct the dll folder path while loading PKCS#11 shared library.

Related

compiling CGAL draw_polygon and linking qt5

I am trying to learn meshing algorithm for CFD puposes and I found CGAL to be a good library to learn everything from ground. My vcpkg cgal[qt5] installation failes due to some problem in installing boost. But other library installation works. I also tried to follow this tutorial but OpenGR and libpointmatcher library generation fails and results cmake error. So I have to setup everything manually. I am using Visual Studio 2022 and Qt5.15.2. I have qt extension downloaded and configured in visual studio. I have also QTDIR variable created in enviroment variables and Qt bin directory in system path.
What I did is as follows-
(1). first I created an empty console application called polygon and added various example code provided by cgal as a source and configuration x64, Release. changed
(2). created a folder library inside solution directory and extracted downloaded cgal-5.5.zip(has include, data, demo, etc ....) to library directory and added $(SolutionDir)library\cgal-5.5\include to additional include path in property manager.
(3). downloaded boost_1_80 from boost.org and compiled with the foolowing steps-
.\bootstrap
open project-config.jam and add-
using mpi ;
using python ;
run the command-
.\b2 --build-dir=build\x64 address-model=64 threading=multi --stagedir=stage/x64 --toolset=msvc -j 16 link=static,shared runtime-link=static,shared --variant=debug,release
added the boost_1_80 folder in include path and stage/x64/lib to additional library directory.
(4). I have built gmp and mpfr with vcpkg with .\vcpkg install gmp:x64-windows-static & .\vcpkg install mpfr:x64-windows-staticcommand and copied the folders gmp_x64-windows-static and mpfr_x64-windows-static to the library directory. I added include and lib folder of both library to respective path and in linker->additional dependency included the two lib file gmp.lib and mpfr.lib
(5). copied images, meshes, points_3 folder from data to solution directory
I can add eigen3, zlib to my project using the same process and all the examples I tried depend on the above libraries compiled and ran properly.
The problem comes when I try to do examples involves qt5. For example program draw_polygon.c-
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/draw_polygon_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Polygon_2<K> Polygon_2;
typedef CGAL::Point_2<K> Point;
int main()
{
// create a polygon and put some points in it
Polygon_2 p;
p.push_back(Point(0,0));
p.push_back(Point(4,0));
p.push_back(Point(4,4));
p.push_back(Point(2,2));
p.push_back(Point(0,4));
CGAL::draw(p);
return EXIT_SUCCESS;
}
I tried following this tutorial but it's made for older version of Qt(Qt4) and Qt5 does not have Qt folder(..\path to\Qt<QT-version>\include) inside includes. This program does not any form file but I added the rest of the includes and lib files accordingly. When I try to compile, it says CGAL_USE_BASIC_VIEWER not defined. Given in this article if I write #define CGAL_USE_BASIC_VIEWER before the includes the generated are like -
Then I tried doing from qt empty project in visual studio interface which still gives -
Obviously my linking failed. How do I fix it for my case? I have went through several other articles none had definite answers.

Using Armadillo C++ library in Swift 5

I'm trying to use Armadillo C++ library in my swift code to create sinusoidal curved arrow. Earlier it worked well with Objective C. But when I'm trying to do the same implementation in Swift, it's showing 'armadillo' file not found error.
I've downloaded the file from https://github.com/gadomski/armadillo/tree/master/branch-5.600/include path and copied both armadillo_bits folder and armadillo file into the project.
I've created a Objective C++ Wrapper around the C++ class too.
Objective C++ Wrapper DrawSinusoidal.h file
#import <Foundation/Foundation.h>
#interface DrawSinusoidal : NSObject
+(NSArray *)bezierPathsForPoints:(NSArray *)points;
Objective C++ Wrapper DrawSinusoidal.mm file
#import "DrawSinusoidal.h"
#import "DrawSinusoidalMath.h"
#implementation DrawSinusoidal
+(NSArray *)bezierPathsForPoints:(NSArray *)points {
...
}
C++ file - DrawSinusoidalMath.h
#include "armadillo"
std::vector<std::vector<arma::vec2>> bezierPathsForPoints(const std::vector<arma::vec2> &points);
C++ file - DrawSinusoidalMath.cpp file
#include <iostream>
#include "DrawSinusoidalMath.h"
using namespace arma;
std::vector<arma::vec2> bezierPathsForPoints(const arma::mat &tValues, const std::vector<arma::vec2> &points)
{
...
...
return points
}
Finally I found the solution to it. Sharing the steps which I followed.
We need to install the pre-built Armadillo packages to macOS which can be installed via MacPorts or HomeBrew
I installed using HomeBrew.
$ brew install armadillo
Once it is completed, please keep a note on the installed path from the last line.
In my machine, it is installed on path /usr/local/Cellar/armadillo/10.5.1
Next, we need to provide the Header Search Paths.
Headers are available in below location, so just copy the path and paste in Xcode.
/usr/local/Cellar/armadillo/10.5.1/include/armadillo_bits
Next, we need to link the libarmadillo.dylib library that is available in the installed path to the sdk path in the Xcode. Open terminal and type following command.
sudo ln -s /usr/local/Cellar/armadillo/10.5.1/lib/libarmadillo.dylib /Applications/Xcode_12.4.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/usr/lib/libarmadillo.dylib
Once done, now you can go to Targets > Build Phases > Link Binary with Libraries
You should see libarmadillo.dylib
Select and add it to your project.
Final step.. Copy armadillo text file and armadillo_bits folder, from below path to the project.
/usr/local/Cellar/armadillo/10.5.1/include
That's it.. The setup is over and you can use the Armadillo library in C++ files inside Objective-C/Swift projects.

Installing the FFTW3 library on Windows 10 using Code::Blocks (C++)

I want to install the FFTW3 library in my system which runs on Windows 10. My compiler is the minGW GCC 10.1 and I am using Code::Blocks.
I have downloaded the 64bit version from http://www.fftw.org/install/windows.html and unzipped it in C:\Program Files (x86)\CodeBlocks\myLibraries\FFTW3.
Then I followed the steps described here https://www.learncpp.com/cpp-tutorial/a3-using-libraries-with-codeblocks/. In step 4 it says "Tell the linker where to look for the library file(s) for the library."
Since there is no .lib file, it had to be created. I copied the libfftw3-3.def file and pasted it inside the bin folder of the mingw64 compiler, so that the dlltool can be used. Note that inside the same bin folder there exists the as.exe executable. I added this directory, namely C:\Users\User\Downloads\minGW_GCC_10_1\mingw64\bin to system PATH as described in MinGW dlltool creates empty file.
Then I opened the cmd window as admin and executed dlltool -v -d nlib32.def -l nlib32.lib. The 2KB libfftw3-3.lib file was created inside the compiler's bin folder.
Then, again, I followed the steps that are described in the link I provided above. For convinience, I am posting the steps directly below.
Once per library:
Acquire the library. Download it from the website or via a package manager.
Install the library. Unzip it to a directory or install it via a package manager.
Tell the compiler where to look for the header file(s) for the library.
Here I set the path to be C:\Program Files (x86)\CodeBlocks\myLibraries\FFTW3
Tell the linker where to look for the library file(s) for the library.
Here I set the path to be C:\Users\User\Downloads\minGW_GCC_10_1\mingw64\bin
Once per project:
Tell the linker which static or import library files to link.
Again the path was set to C:\Users\User\Downloads\minGW_GCC_10_1\mingw64\bin
#include the library’s header file(s) in your program.
Make sure the program know where to find any dynamic libraries being used.
When I execute the following code
#include <iostream>
#include <fftw3.h>
int main(){
int N = 100;
fftw_complex *in;
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
}
I am getting the following error: "Undefined reference to '__imp_fftw_malloc' ".
I don't know what I am doing wrong so that the compiler cannot find the function that I am calling, but I guess that the problem is in step 7, that is in the "Make sure the program know where to find any dynamic libraries being used." step. Note that this is the first time I had to deal with dynamic-link libraries etc so I am still confused.
I managed to make your code run in three simple steps (I suppose you have dowloaded and extracted FFTW properly and installed Code::Blocks too):
Indicate the FFTW directory so the header fftw3.h can be read. Build options > Search directories > Compiler and specify where the header file is. To me it's C:\Program Files\FFTW.
Copy the libfftw3-3.dll file from the FFTW directory to next to the .exe of your application. Tp me it's C:\projets\fftwEx\bin\Debug.
Copy and rename the libfftw3-3.dll file in the original installed directory to libfftw3-3.dll.a. Then indicate it's path in Build options > Linker settings > Link libraries. To me it's C:\Program Files\FFTW\libfftw3-3.dll.a.

Qt : Unit Test with Visual Studio

I have a Visual Studio Project and want to write some Unit Test for it.
I tried doing that by using a "Native Unit Test Project".
The Problem is, that when I use a QString in the Test, the Test fails with following message:
Message: Failed to set up the execution context to run the test
Any suggestions how I can write Unit Test using Qt?
#include "stdafx.h"
#include "CppUnitTest.h"
#include <QtCore/QCoreApplication>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace Test
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
QString a = "Test";
Assert::IsTrue(true);
}
};
}
I have tried with the above example of yours and i am able to compile the unit test. These are the following changes I've done apart from copying your code.
Check if your QT is built with 32 bit or 64 bit compiler.
You need to set the same in Solution Platforms.
You need to add include directory path and the lib file path(Qt5Cored.lib(for
debug)/Qt5Core.lib(for release)) in the Project properties.
To run the unit test you need to copy the
Qt5Cored.dll/Qt5Core.dll file in the folder where your unit test dll file is generated(After compilation).
PS:- I am using VS 2015 but this doesn't matter.
I have also ran into this issue with Qt6 with a project created using the Qt Visual Studio Tools extension for Visual Studio 2019.
QString was one of the things popping up as an error during the build process, but there was various more errors. My solution was as follows:
Gone to the Microsoft Unit Testing Framework for C++ project's Properties (right-click on solution, bottom option of the menu "Properties")
As mentioned in #sonulohani's response, I had it running in x64 by default.
Under Configuration Properties > Linker > General Additional Library Directories I added the path to my QT installation location libs folder (for me this was C:\Qt\6.1.2\msvc2019_64\lib) at the top of the list.
Under Configuration Properties > Linker > Input Additional Dependencies I added a link to all the "*.lib" files from that very same QT installation (for me, this was C:\Qt\6.1.2\msvc2019_64\lib\*.lib) AND a link to the build output .obj files for the Qt project ($(SolutionDir)<MY PROJECT NAME>\$(IntDir)*.obj where <MY PROJECT NAME> was my project name). This second part may not necessarily be needed.
Copying just the Qt6Core.dll/Qt6Cored.dll as suggested by #sonulohani did not work. It was still missing stuff. So in the properties, under Configuration Properties > Build Events > Post-Build Event Command Line I added this script to copy all the Qt6 dll files to the test project .dll output directory:
for %%f in (C:\Qt\6.1.2\msvc2019_64\bin\Qt6*.dll) do #copy /y %%f $(SolutionDir)$(Platform)\$(Configuration)\Tests\
Now at that point I had another issue, since both my test project and my actual Qt project was dumping its files into the same output directory, and the Qt project did not like having all those dlls in there and did not run anymore. So I created the $(SolutionDir)$(Platform)\$(Configuration)\Tests\ directory within the regular output directory, and also made this \Tests\ subdirectory the output directory for the test project (Configuration Properties > General Output Directory).

Xcode External Build System Project Can't Find wchar.h

I'm trying to figure out how to use Xcode's "External Build System" project template to do some C++ coding. I've set it up with a very simple test project, just to make sure that I understand the configuration. I'm using make to build the project. When I try to run it, the build fails with the error message 'wchar.h' file not found.
Here are the steps I took to set up the project:
Create a new Project, using the Other -> External Build System template.
Set the Build Tool option to /usr/bin/make.
Copy my source file, ex_3_4.cpp to the directory where the Xcode project is located, and add it to the project using the Add files to "TestProject"... menu option.
Copy the makefile to the directory where the Xcode project is located.
Everything works if I run make from the terminal - I get an executable that runs fine, and there is no trouble finding wchar.h or any other headers. So I know that this is a problem with something in the project settings.
Here is the code in the source file:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
}
And here is my makefile:
all:
clang++ ex_3_4.cpp -o ex_3_4
clean:
rm ex_3_4
I can't find any place in the project settings where I can specify a search path for the wchar.h file (or any other header files for that matter), so I'm not sure how to fix this. I feel like it must be something simple.
If it makes any difference, I've been using Xcode 7.3, but I've also tried it with the new Xcode 8.0. I get the same error message using either version.
Perhaps you want to add a library target to your project, add it as a dependency on your project and then, your .h into the Headers section