OpenSSL in a VisualC++ DLL

2019-08-10 06:18发布

问题:

i'm trying to write a C++ DLL which uses openSSL to secure a connection to a server.

I'm genuinly puzzled by the fact that this code

#include "stdafx.h"
#include <string.h>
#include <iostream>
//SSL stuff
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#include <openssl/ossl_typ.h>
#include <openssl/applink.c>
//Winsock stuf
#pragma comment(lib, "ws2_32.lib")
{... Create a method in which we set up the SSL connection ...}
char* tSend = "{\"reqtype\":\"keyexchange\"}";
int sendSize = strlen(tSend);
int net_tSend = htonl(sendSize);
SSL_write(ssl, &net_tSend, 4);
SSL_write(ssl, tSend, sendSize);

works fine in a Console application, but crashes in my DLL. Here's my exception:

Exception thrown at 0x00007FF865207DA0 (libeay32.dll) in TestsForPKCSDLL.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

Thanks a lot for your time.

回答1:

After a bit of research, it looks like the problem comes from the htonl() function.

    u_long mylong = 10L;
    int net_tSend = htonl(mylong);

Exception thrown at 0x00007FF863807DA0 (libeay32.dll) in TestsForPKCSDLL.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

Which apparently is not loaded properly. I think that, because my code is in a DLL, it crashes if the calling program doesn't reference SSL dlls. I'll try to link libeay32 and ssleay32 statically see if that works.