https://github.com/georgiifirsov/dllhandler
Class that provides automatic DLL handle release and allow you to use exported functions much more easily in comparison to standard WinAPI way.
https://github.com/georgiifirsov/dllhandler
cplusplus cplusplus-14 dll library winapi windows
Last synced: about 1 month ago
JSON representation
Class that provides automatic DLL handle release and allow you to use exported functions much more easily in comparison to standard WinAPI way.
- Host: GitHub
- URL: https://github.com/georgiifirsov/dllhandler
- Owner: GeorgiiFirsov
- License: gpl-3.0
- Created: 2019-11-22T21:44:30.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-26T09:58:47.000Z (over 6 years ago)
- Last Synced: 2026-04-30T13:34:26.563Z (about 1 month ago)
- Topics: cplusplus, cplusplus-14, dll, library, winapi, windows
- Language: C++
- Homepage:
- Size: 46.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DllHandler
[![Version][]]() [![Tests][]]() [![License][]]()
[Version]: https://img.shields.io/badge/Version-v1.0-blue
[Tests]: https://img.shields.io/badge/Tests-passed-brightgreen
[License]: https://img.shields.io/badge/License-GNU%20GPL%20v.3-blue
This is a wrapper over Windows' dll `HMODULE`. This class provides automatic release of handle at exit from scope.
### Usage
This class has really simple usage:
```cpp
CDllHandler hDll;
hDll = LoadLibraryW( L"Ntdll.dll" );
// Here you can use it as normal HMODULE handle
// No need to release
```
But the main feature of this small library is possibility to easily use exported functions.
Here is an example:
```cpp
// ...
using PRtlComputeCrc32 = DWORD(__stdcall *)( DWORD, const BYTE*, INT );
// It is a class declaration.
// This class can be local - it contains no static members
DLL_BEGIN( CNtLib, L"Ntdll.dll" )
DLL_FUNCTION( PRtlComputeCrc32, RtlComputeCrc32 )
DLL_END;
try
{
CNtLib NtLib; // <- this object contains dll
DWORD dwHash = NtLib.RtlComputeCrc32( 0, pData, GetSize( pData ) );
// That's it! Nothing more to do to use exported functions!
// ...
}
catch( const CWin32Error& e )
{
// Handle error
}
```
The main goal is to write code like above (just 5 lines) to call a dll's function instead of this:
```cpp
using PRtlComputeCrc32 = DWORD(__stdcall *)( DWORD, const BYTE*, INT );
HMODULE hDll = LoadLibrary( L"Ntdll.dll" );
if(!hDll) {
// Handle error
}
auto pRtlComputeCrc32 = reinterpret_cast(
GetProcAddress( hDll, "RtlComputeCrc32" )
);
if(pRtlComputeCrc32) {
// Handle error
}
DWORD dwHash = pRtlComputeCrc32( 0, pData, GetSize( pData ) );
```
This solution is less memory-leak-safe and more complicated to use.
In case of any error during library loading or function search this class (and helper template) generates a `CWin32Error` exception,
that contains an error code and its description:
```cpp
try
{
// ...
}
catch( const CWin32Error& e )
{
e.Code() // Obtain error code
e.Description() // Obtain error description (available only for system errors)
}
```