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);
Related
Here's a minimal reproducible example of what I'm trying to accomplish:
I'm trying to set up a standard vector that will contain a list of all the possible operations my program can use. These operations are defined using my class Option, which is defined in my classes.cpp file. I want it to be set initially in my main.cpp file, but it should be usable in my setter.cpp file, in which I fill the vector.
Here is my code:
Main.h
#pragma once
#include <vector>
#include "classes.h"
extern std::vector<Option*> OPTIONS;
Main.cpp
#include <iostream>
#include <vector>
#include "classes.h"
#include "setter.h"
std::vector<Option*> OPTIONS = {};
int main()
{
initOptions(); //function from setter.cpp
OPTIONS[0]->pFunc(); // EXCEPTION at 0xCCCCCCCC in Main.exe: 0xC0000005: Acccess infraction when executing 0xCCCCCCCC.
return 0;
}
setter.h
#pragma once
void foo();
void initOptions();
setter.cpp
#include <iostream>
#include <string>
#include "Main.h"
void foo()
{
std::cout << "Hello world :)" << std::endl;
}
void initOptions()
{
OPTIONS = {
&Option("Test String", &foo)
};
}
classes.h
#pragma once
#include <string>
struct Option
{
std::string name;
void (*pFunc)();
Option(std::string n, void (*pF)()) : name(n), pFunc(pF) {};
};
I'm using Visual Studio 2019, all Main and setter files are in the same project, but classes.h is in another one (I know maybe this is not the most efficient way of doing things, but this is one of my first "interesting" projects and I'm trying to experiment a bit).
It compiles just fine, but when I execute it throw the exception: EXCEPTION at 0xCCCCCCCC in Main.exe: 0xC0000005: Acccess infraction when executing 0xCCCCCCCC.. Using the inspection tool from VS I can see that the OPTIONS vector has one element, but it is empty (string is "" and function pointer is 0x00000000). Am I declaring the global vector wrong? Did I not link projects correctly? Thanks in advance!
P.D: I tried to minimize and translate my code (not native speaker), so if there is incorrect spelling it's probably my translation and not the actual code :P
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;
}
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.
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
I've included the TlHelp32.h header properly though I keep getting the message:
"Error: identifier "CreateToolhelp32Snapshot" is undefined"
when attempting to use CreateToolhelp32Snapshot. When I used the "peek definition" feature in VS I found that there are errors within this header where in certain areas it says:
"Error expected a ';'"
Any ideas how to fix this?
#include "stdafx.h"
#include <TlHelp32.h>
#include <Windows.h>
#include <iostream>
using namespace std;
class Functions{
public:
void playerHealthPrinter(){
HANDLE hProcess;
DWORD dwPID, dwProtection, dwCaveAddress;
BOOL bPOn, bIOn, bProt;
HANDLE hPID = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
Never include Win32 headers before <windows.h>. The following should work, instead:
#include <Windows.h>
#include <TlHelp32.h> // <-- include *after* windows.h
If it still doesn't work then please post an MCVE including the relevant headers and version of VS.