OpenSSL in a VisualC++ DLL - c++

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.

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.

Related

Unable to call `SetWallpaper()` due to error LNK2019

Intro
First of all, I would like to say that I have read through the previous answers for this type of question, including this excellently written one.
However, I do not understand enough about C++ to be able to use the more "advanced" fixes.
I have ensured that the right type of console has been selected (Console (/SUBSYSTEM:CONSOLE) for those interested), and have the required imports with the possible exception of an IDL mentioned somewhere (that falls into the lack of understanding category).
If this is a duplicate, I would be more than happy to use the post I duplicated, but I have not been able to find anything that can help someone of my skill level.
Technical Information
IDE: Visual Studio
Platform: Windows
Code
headers.h
#pragma once
#include <stdio.h>
#include <iostream>
#include <string>
#include <windows.h>
#include <Shobjidl.h>
#include <time.h>
#include <stdlib.h>
#include <tchar.h>
main.cpp
#include "headers.h"
using namespace std;
int main() {
string x = "C://Users/student/Desktop/i-should-buy-a-boat.jpg";
x.c_str();
wstring tempx = std::wstring(x.begin(), x.end());
LPCWSTR sw = tempx.c_str();
HRESULT SetWallpaper(
LPCWSTR monitorID,
LPCWSTR wallpaper
);
SetWallpaper(NULL, sw);
}
SetWallpaper() is not a standalone function exported by the Win32 API. It is a method of the IDesktopWallpaper interface (see here).
So you need to use code that is more like this instead:
#include "headers.h"
int main()
{
std::wstring x = L"C:\\Users\\student\\Desktop\\i-should-buy-a-boat.jpg";
CoInitialize(NULL);
IDesktopWallpaper *p;
if (SUCCEEDED(CoCreateInstance(__uuidof(DesktopWallpaper), 0, CLSCTX_LOCAL_SERVER, __uuidof(IDesktopWallpaper), (void**)&p)))
{
p->SetWallpaper(NULL, x.c_str());
p->Release();
}
CoUninitialize();
return 0;
}

How to call method EnableStatic() in C++?

Like in the title.
I'm a not very experienced with classes and I have hard time with calling the function EnableStatic() from Win32_NetworkAdapterConfiguration class in C++. So how to do it?
I would really appreciate an example or simple program with it!
Additional Info:
•I've tried use this part of calling code:
#define _WIN32_DCOM
#include <iostream>
#include <winsock.h>
#include <string>
#include <stdint.h>
#include <comdef.h>
#include <Wbemidl.h>
int maint()
{
UINT ipaddr = inet_addr("155.34.22.0");
UINT subnet = inet_addr("255.255.255.0");
Win32_NetworkAdapterConfiguration Adapter;
Adapter.EnableStatic(ipaddr,subnet);
system("pause");
}
•The method's website:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa390383(v=vs.85).aspx
If anyone interested I'm inviting for a C++ project on GitHub

using a dll in ezcad with c++

I have one dll explicitly link and I would like to use.
I create this code:
#include "StdAfx.h"
#include <windows.h>
#include <iostream>
#include <tchar.h>
#include <MarkEzdDll.h>
#include < TCHAR.H >
#include<HLink.h>
typedef int (__stdcall *Thiago)();
int main()
{
HINSTANCE hEzdDLL = LoadLibrary(_T("C:\\Users\\Thiago\\Desktop\\DLL\\Debug\\MarkEzd.dll"));
Thiago lmc1_Initial = (Thiago)GetProcAddress(hEzdDLL , "lmc1_Initial");
lmc1_Initial();
}
But not fuction, the error this:
Unhandled exception at 0x10007f76 (MarkEzd.dll) in DLL.exe: 0xC0000005: Access violation reading location 0x00000004.
what I do?
Check for the return error code, if it's NULL you have an error.
According to these sources (and assuming that's the same library you're using), the signature is actually:
typedef int (*LMC1_INITIAL)(TCHAR* strEzCadPath, BOOL bTestMode, HWND hOwenWnd);

C++/Qt FFmpeg library error: program has unexpectedly finished

I am trying to use ffmpeg library on windows in C++/Qt. This is my main function:
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
#define INT64_C(val) val##LL
#define UINT64_C(val) val##ULL
#include <QtCore>
#include <SDL/SDL.h>
#ifdef __MINGW32__
#undef main
#endif
//--------------- FFMPEG Headers and Libraries ---------------
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
int main(int c, char *args[])
{
av_register_all();
AVFormatContext *ctx;
if(avformat_open_input(&ctx,"salam.avi",NULL,NULL)!=0)
return -1;
return 0;
}
It gets compiled & linked fine. But I get this error when I try to run it:
The program has unexpectedly finished
This happens on *avformat_open_input* function. What's the problem? Is it about my code, or it is a problem with my libraries?
Thanks in advance
Finally I found it. The answer is so simple. ctx should be initialized by NULL.
AVFormatContext *ctx = NULL;
Could be a problem with the AVI. Make sure your avi is supported by FFMPEG. use this tool To check what exactly the format is and look up the FFMPEG library help/support to see if the format is supported or not.

Odd Memory Error -- Bad Alloc

Working on a WinPCap project. Trying to do some basic pointer and memory operations and having lots of errors.
I've included the two lines I'm trying to run along with the includes.
The same lines in another VSC++ project work just fine. This is the error I am getting
Unhandled exception at 0x75a79617 in
pktdump_ex.exe: Microsoft C++
exception: std::bad_alloc at memory
location 0x0012f8e4..
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include "DataTypes.h"
#include <sstream>
#include "EthernetLayer.h"
#include <pcap.h>
int* testPointer = new int[2];
delete[] testPointer;
EDIT:
Found out something useful.
The following code snippet is what is crashing the winpcap library.
EthernetStructPointers* testData;
testData = (EthernetStructPointers*)pkt_data;
EthernetStruct newData;
memcpy(newData.DEST_ADDRESS, testData->DEST_ADDRESS, 6);
These are the definitions of the structs.
struct EthernetStructPointers
{
u_char DEST_ADDRESS[6];
u_char SOURCE_ADDRESS[6];
u_char TYPE[2];
};
struct EthernetStruct
{
u_char DEST_ADDRESS[6];
u_char SOURCE_ADDRESS[6];
u_char TYPE[2];
u_char* dataPointer;
string DestAddress;
string SourceAddress;
string Type;
int length;
};
My guess is the freestore is corrupted by one the previous statements (perhaps by an incorrect use of the pcap interface), and you only learn of the error on the next memory allocation or release, when the manager detects it and throws a bad alloc.
std::bad_alloc should be thrown when you try to new something and have run out of memory. Can you check how much free memory is available to your process?