I'm trying to create a very simple dll and load it to another file for learning purpose. I'm using VC++ same version for both the DLL and the C++ file. Here is my source code:
load.cpp:
#include <iostream>
#include <windows.h>
using namespace std;
typedef int(__stdcall* nsum)(int a, int b);
int main(void)
{
HINSTANCE myDll = LoadLibrary(L".\\DLL1.dll");
nsum sum = (nsum)GetProcAddress(myDll, "sum");
if (!myDll) {
cout << "could not load the dynamic library" << endl;
return EXIT_FAILURE;
}
int xfinal = sum(10, 20);
cout << xfinal << endl;
return 0;
}
and dll.cpp
#include <windows.h>
#include "pch.h"
using namespace std;
int __declspec(dllexport) __stdcall sum(int a, int b)
{
return a + b;
}
pch.h
// pch.cpp: source file corresponding to the pre-compiled header
#include "pch.h"
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
But still I'm getting the exception. I searched for answers but found none, so I am writing this for help
In the load.cpp, in HINSTANCE myDll, I can peek and see the value being set to 0x000000000. Is it the reason? If so, How can i fix it?