Exetools  

Go Back   Exetools > General > Source Code

Notices

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 06-08-2024, 05:35
atom0s's Avatar
atom0s atom0s is offline
Family
 
Join Date: Jan 2015
Location: 127.0.0.1
Posts: 431
Rept. Given: 26
Rept. Rcvd 130 Times in 67 Posts
Thanks Given: 54
Thanks Rcvd at 837 Times in 306 Posts
atom0s Reputation: 100-199 atom0s Reputation: 100-199
Using templates can remove the need to manually define the delete function. It can also be used to further extend into a custom wrapper to deal with the other parts you mentioned as well such as:

PHP Code:
template<typename T>
struct auto_cleanup_impl;

template<>
struct auto_cleanup_impl<HANDLE>
{
    static 
void cleanup(HANDLE value)
    {
        if (
value != INVALID_HANDLE_VALUE) ::CloseHandle(value);
    }
};

template<typename T>
class 
auto_cleanup
{
    
std::shared_ptr<voidvalue_;

public:
    
auto_cleanup(T value)
        : 
value_valueauto_cleanup_impl<T>::cleanup }
    {}
    ~
auto_cleanup(void)
    {}

    
T get(void) const
    {
        return 
this->value_.get();
    }
}; 
Then in the same manner can be used as:
PHP Code:
auto_cleanup val(::OpenProcess(PROCESS_VM_OPERATIONFALSE1)); 
To allow for direct value usage of the underlying shared pointer, one can add the overloads needed:

PHP Code:
    operator T() const { return value_.get(); }

    
auto_cleanup<T>& operator=(const& val)
    {
        
this->value_.reset(valauto_cleanup_impl<T>::cleanup);
        return *
this;
    } 
Then additional constructors can be added if you need default value handling or other kinds of initialization.

PHP Code:
    // Implementations will depend on the supported types etc.
    
auto_cleanup()
        : 
value_nullptrauto_cleanup_impl<T>::cleanup }
    {} 
To extend the types supported, you only need to add an auto_cleanup_impl entry for the given type. For example, to add support for a FILE pointer, it can be added via:

PHP Code:
template<>
struct auto_cleanup_impl<FILE*>
{
    static 
void cleanup(FILEvalue)
    {
        if (
value) ::fclose(value);
    }
}; 
Some examples of using this with a HANDLE type would be:

PHP Code:
    // Wrapped initialization; type determined by the return of the called function..
    
auto_cleanup val1(::OpenProcess(PROCESS_VM_OPERATIONFALSE1));

    
// Default initialization; type determined by the value type..
    
auto_cleanup val2INVALID_HANDLE_VALUE };

    
// Default initialization; type determined by explicit template usage..
    
auto_cleanup<HANDLEval3{};

    
// Value usage after creation..
    
uint8_t buffer[1024]{};
    
auto_cleanup val4INVALID_HANDLE_VALUE };
    
val4 = ::OpenProcess(PROCESS_VM_OPERATIONFALSE1);
    ::
ReadProcessMemory(val4reinterpret_cast<LPCVOID>(0x1234ABCD), &buffer1024nullptr); 
__________________
Personal Projects Site: https://atom0s.com
Reply With Quote
The Following 3 Users Say Thank You to atom0s For This Useful Post:
MarcElBichon (06-08-2024), SofTw0rm (06-11-2024), tonyweb (06-08-2024)
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resource Manager (.NET) v2 CodeCracker Community Tools 2 11-23-2025 22:25


All times are GMT +8. The time now is 10:25.


Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX, chessgod101
( Since 1998 )