7zip/CPP/Windows/DLL.h

104 lines
2.1 KiB
C
Raw Normal View History

2021-12-27 01:00:00 +01:00
// Windows/DLL.h
2023-06-21 02:00:00 +02:00
#ifndef ZIP7_INC_WINDOWS_DLL_H
#define ZIP7_INC_WINDOWS_DLL_H
2021-12-27 01:00:00 +01:00
#include "../Common/MyString.h"
2023-06-21 02:00:00 +02:00
#ifndef _WIN32
typedef void * HMODULE;
// typedef int (*FARPROC)();
// typedef void *FARPROC;
void *GetProcAddress(HMODULE module, LPCSTR procName);
#endif
2021-12-27 01:00:00 +01:00
namespace NWindows {
namespace NDLL {
#ifdef _WIN32
2023-06-21 02:00:00 +02:00
/*
2021-12-27 01:00:00 +01:00
#ifdef UNDER_CE
#define My_GetProcAddress(module, procName) (void *)::GetProcAddressA(module, procName)
#else
#define My_GetProcAddress(module, procName) (void *)::GetProcAddress(module, procName)
#endif
2023-06-21 02:00:00 +02:00
*/
2021-12-27 01:00:00 +01:00
/* Win32: Don't call CLibrary::Free() and FreeLibrary() from another
FreeLibrary() code: detaching code in DLL entry-point or in
destructors of global objects in DLL module. */
class CLibrary
{
HMODULE _module;
2023-06-21 02:00:00 +02:00
// Z7_CLASS_NO_COPY(CLibrary);
// copy constructor is required here
2021-12-27 01:00:00 +01:00
public:
2023-06-21 02:00:00 +02:00
CLibrary(): _module(NULL) {}
2021-12-27 01:00:00 +01:00
~CLibrary() { Free(); }
2023-06-21 02:00:00 +02:00
CLibrary(const CLibrary &c): _module(NULL)
{
if (c._module)
{
// we need non const to reference from original item
// c._module = NULL;
throw 20230102;
}
}
HMODULE Get_HMODULE() const { return _module; }
// operator HMODULE() const { return _module; }
// HMODULE* operator&() { return &_module; }
2021-12-27 01:00:00 +01:00
bool IsLoaded() const { return (_module != NULL); }
void Attach(HMODULE m)
{
Free();
_module = m;
}
HMODULE Detach()
{
2023-06-21 02:00:00 +02:00
const HMODULE m = _module;
2021-12-27 01:00:00 +01:00
_module = NULL;
return m;
}
bool Free() throw();
bool LoadEx(CFSTR path, DWORD flags = LOAD_LIBRARY_AS_DATAFILE) throw();
bool Load(CFSTR path) throw();
// FARPROC
2023-06-21 02:00:00 +02:00
// void *GetProc(LPCSTR procName) const { return My_GetProcAddress(_module, procName); }
2021-12-27 01:00:00 +01:00
};
#else
class CLibrary
{
HMODULE _module;
2023-06-21 02:00:00 +02:00
// Z7_CLASS_NO_COPY(CLibrary);
2021-12-27 01:00:00 +01:00
public:
2023-06-21 02:00:00 +02:00
CLibrary(): _module(NULL) {}
2021-12-27 01:00:00 +01:00
~CLibrary() { Free(); }
2023-06-21 02:00:00 +02:00
HMODULE Get_HMODULE() const { return _module; }
2021-12-27 01:00:00 +01:00
bool Free() throw();
bool Load(CFSTR path) throw();
// FARPROC
2023-06-21 02:00:00 +02:00
// void *GetProc(LPCSTR procName) const; // { return My_GetProcAddress(_module, procName); }
2021-12-27 01:00:00 +01:00
};
#endif
bool MyGetModuleFileName(FString &path);
FString GetModuleDirPrefix();
}}
#endif