Kk so I've been using these functions for quite some time and everytime I write a new program and decide to use them, I have to open up old ones and copy paste them with a whole bunch of include n crap. I decide I'd try to make a DLL that has the functions include and then load it and just use them.. I don't know if I made the DLL right, even though it compiles.. and I don't know how to load it in .Net C++
I plan to make 2 versions of this DLL.. the one below for .Net and another one for Win32 Api. Can anyone check to see if I did the .Net one right and explain to me how to load this? Or what about including it as a resource and loading it from there?
Managed DLL.h
#ifdef MANAGEDDLL_EXPORTS
#define MANAGEDDLL_API __declspec(dllexport)
#else
#define MANAGEDDLL_API __declspec(dllimport)
#endif
#include <Windows.h>
#pragma managed
#using <mscorlib.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::IO;
using namespace System::Windows::Forms;
// This class is exported from the Managed DLL.dll
class MANAGEDDLL_API ManagedDLL {
public:
ManagedDLL(void);
bool extractResource(WORD resourceID, LPCWSTR outputFilename, LPCWSTR resName);
void ResourceInfo(LPTSTR ResourceLocation, LPTSTR ResourceType, int ResourceID);
};
extern void ReplaceFile(String^ fileToMoveAndDelete, String^ fileToReplace, String^ backupOfFileToReplace);
Managed DLL.cpp
// Managed DLL.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "Managed DLL.h"
#include <tchar.h>
#include <iostream>
#pragma comment(lib, "advapi32.lib")
#include <tlhelp32.h>
#include <conio.h>
#include <cstdlib>
#include <Winuser.h>
#pragma comment(lib, "User32.lib")
#pragma comment(lib, "Shell32.lib")
void ReplaceFile(String^ fileToMoveAndDelete, String^ fileToReplace, String^ backupOfFileToReplace)
{
File::Replace(fileToMoveAndDelete, fileToReplace, backupOfFileToReplace, false);
}
MANAGEDDLL_API bool extractResource(WORD resourceID, LPCWSTR outputFilename, LPCWSTR resName)
{
bool success = false;
try {
// Locate the resource
HRSRC hResource = FindResource(NULL, MAKEINTRESOURCE(resourceID), resName);
if (hResource == 0) {return false;} // Checking for errors
// Load the resource
HGLOBAL hFileResource = LoadResource(NULL, hResource);
if (hFileResource == 0) {return false;} // Checking for errors
// Lock the resource so other processes can't use it
LPVOID lpFile = LockResource(hFileResource);
if (lpFile == 0) {return false;} // Checking for errors
// and then get the size on disk of the file.
DWORD dwSize = SizeofResource(NULL, hResource);
if (dwSize == 0) {return false;} // Checking for errors
HANDLE hFile = CreateFile(outputFilename, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hFilemap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwSize, NULL);
if (hFilemap == 0) {return false;} // Checking for errors
LPVOID lpBaseAddress = MapViewOfFile(hFilemap, FILE_MAP_WRITE, 0, 0, 0);
CopyMemory(lpBaseAddress, lpFile, dwSize);
// Unmap the file and close the handles
UnmapViewOfFile(lpBaseAddress);
CloseHandle(hFilemap);
CloseHandle(hFile);
// Everything should have went well. Returning true.
return true;
} catch (...) {}// Catch all errors
return success;
}
MANAGEDDLL_API void ResourceInfo(LPTSTR ResourceLocation, LPTSTR ResourceType, int ResourceID)
{
WORD *resourceID = new WORD;
LPCWSTR *outputFilename = new LPCWSTR;
LPCWSTR *resName = new LPCWSTR;
// Give them some values
*resourceID = 130;
*outputFilename = ResourceLocation;
*resName = ResourceType;
// Call resource extraction function
bool eSuccess = extractResource(*resourceID, *outputFilename, *resName);
if (eSuccess == true) {
//MessageBox::Show("Program Resource successfully extracted. :)");
} else {
MessageBox::Show("Error extracting resource. :(");
}
}
// This is the constructor of a class that has been exported.
// see Managed DLL.h for the class definition
ManagedDLL::ManagedDLL()
{
return;
}