Search code examples
c++visual-studio-2008compilationclrcrypto++

Error C2711 while trying to compile my project using crypto++


I'm trying to compile a project using crypto++. My project is using clr and when I'm trying to compile the code, I end up with the followings errors:

'main' : this function cannot be compiled as managed, consider using #pragma unmanaged

'int main(cli::array<Type> ^)' : managed type or function cannot be used in an unmanaged function

My project is using clr and I'm using /MD as runtime Library. I setted the same parameters when I compiled crypto++.

Edit: my main function

     int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");

    // Generate keys
    AutoSeededRandomPool rng;

    InvertibleRSAFunction params;
    params.GenerateRandomWithKeySize( rng, 1536 );

    RSA::PrivateKey privateKey( params );
    RSA::PublicKey publicKey( params );

    std::string plain="RSA Encryption", cipher, recovered;


    // Encryption
    RSAES_OAEP_SHA_Encryptor e( publicKey );

    StringSource( plain, true,
        new PK_EncryptorFilter( rng, e,
            new StringSink( cipher )
        ) // PK_EncryptorFilter
     ); // StringSource

    // Decryption
    RSAES_OAEP_SHA_Decryptor d( privateKey );

    StringSource( cipher, true,
        new PK_DecryptorFilter( rng, d,
            new StringSink( recovered )
        ) // PK_DecryptorFilter
     ); // StringSource

    assert( plain == recovered );
    std::cin.ignore();
    return 0;
}

Solution

  • You'll get this error if you attempt to perform any unmanaged instruction in managed code. See here.

    The likes of assert() and std::string are native methods/types respectively which means they deal in raw pointers and don't obey the rules of managed C++. Mixing unmanaged code like this with managed code can be achieved by using PInvoke/DllImport.