Re: [Selfext-user] Protection of SFX
Brought to you by:
mweth
|
From: Mike W. <mik...@nt...> - 2003-09-13 15:32:53
|
On Saturday 13 September 2003 9:41 am, Ho Kei Lau wrote:
> Thanks for your help.. I'm now working on an SFX without C libraries, C++
> classes... what a headache!
I'm not sure if I've understood you correctly. Presumably that means you wish
you could use the C runtime and C++ libs while working on selfext? or did you
mean you want a self extractor with no depenancies?
If it's the former, then you can link to the C runtime or C++ libs if you
want to. Obviously a self extractor shouldn't depend on any dlls that won't
already be installed on the users machine. So your choices are:
For the C runtime:
1. Do without. The Windows API has an equivalent for just about everything
that you'll find in a CRT.
2. Dynamically link. This will give you a program that depends on MSVCRT.DLL
which is present on all 32-bit verisons of Windows except the first release
of Windows 95.
3. Statically link. Ok but the executable will be larger.
For C++ library:
1. Do without. If you include implementations of 'new' and 'delete' (see
below) then you can write C++ code normally, though no exceptions or rtti or
library classes like std::string.
2. Dynamically link. Not realistic, different versions of Windows and VC++
come with different dlls.
3. Statically link. Ok but the executable will be larger.
Below is a tiny module which defines 'new' and 'delete' which you
can use if you want to write C++ without standard libs.
You can use this module even if you link to the the standard libs, and it will
still reduce the size of your executable by replacing the standard startup
code. This can be useful if you just want a few functions from the standard
libs, though of course anything that needs to have been initialized by the
startup code (like stdio) isn't going to work.
HTH,
Mike
//////////////////////////////////////////////////////////////////////
// Minimal startup and memory functions for programs that don't
// want to link in the runtime library. Define MCRT_FULL if you're
// going to use the command line parameter of WinMain.
//
// Build Options:
//
// Release Link: Ignore all default libraries (optional)
// C++ Language: Disable exception handling ('fraid so)
//
#include <windows.h>
static HANDLE ghHeap; // processes's heap handle
extern "C" void __cdecl main(void) {} // linker complains without this
//////////////////////////////////////////////////////////////////////
// Startup code (extra lean)
#ifndef MCRT_FULL
extern "C" void __cdecl WinMainCRTStartup(void)
{
ghHeap = GetProcessHeap();
ExitProcess(WinMain(GetModuleHandle(NULL), NULL, NULL, SW_SHOWDEFAULT));
}
#endif // !MCRT_FULL
//////////////////////////////////////////////////////////////////////
// Startup code (full)
#ifdef MCRT_FULL
extern "C" void __cdecl WinMainCRTStartup(void)
{
int mainret;
char *lpCmdLine;
STARTUPINFO StartupInfo;
ghHeap = GetProcessHeap();
lpCmdLine = GetCommandLine();
// Skip past program name (first token in command line).
if (*lpCmdLine == '"')
{
while (*lpCmdLine && (*lpCmdLine != '"'))
lpCmdLine++;
if (*lpCmdLine == '"')
lpCmdLine++;
}
else
{
while (*lpCmdLine > ' ')
lpCmdLine++;
}
// Skip past any white space preceeding the second token.
while (*lpCmdLine && (*lpCmdLine <= ' '))
lpCmdLine++;
StartupInfo.dwFlags = 0;
GetStartupInfo(&StartupInfo);
mainret = WinMain(
GetModuleHandle(NULL), NULL, lpCmdLine,
StartupInfo.dwFlags & STARTF_USESHOWWINDOW ?
StartupInfo.wShowWindow : SW_SHOWDEFAULT);
ExitProcess(mainret);
}
#endif // MCRT_FULL
//////////////////////////////////////////////////////////////////////
// new and delete
void * __cdecl operator new(size_t cb)
{
return HeapAlloc(ghHeap, 0, cb);
}
void __cdecl operator delete(void *pv)
{
HeapFree(ghHeap, 0, pv);
}
//////////////////////////////////////////////////////////////////////
// malloc/free
void * __cdecl malloc(size_t cb)
{
return HeapAlloc(ghHeap, 0, cb);
}
void * __cdecl calloc(size_t n, size_t cb)
{
return HeapAlloc(ghHeap, 0, n * cb);
}
void * __cdecl realloc(void *p, size_t cb)
{
return p == NULL ? HeapAlloc(ghHeap, 0, cb) : HeapReAlloc(ghHeap, 0, p, cb);
}
void __cdecl free(void *p)
{
HeapFree(ghHeap, 0, p);
}
|