Make Custom Self-Extractor

1.8k views Asked by At

I'll explain what I want, and then I'll explain how I'm trying to achieve it. I want to know if I'm gonig about this the right way, or if there is something much easier.

What I want: a self-extracting executable that happens to ALSO have an additional entry point (which makes the executable suitably usable as if it were a DLL). The additional entry point must not be part of the compressed payload. The entry point, strangely enough, will not execute any LZMA functions (please don't ask why...long story). FYI: making an executable with a DLL entry point is a trivial matter - I already know how to do that.

How I'm pursuing this: I've downloaded the LZMA SDK and will build my own C++ self-extractor. There appears to be no LZMA API documentation. Evidentally, if I want to learn how to use LZMA I must read either .\C\util\7z\7zmain.c or .\cpp\7zip\bundles\lzmaCon\lzmaAlone.cpp. I don't know if studying those is the fastest learning tool.

Once I create the self-extraction code, then I will add the DLL entry point I need and build. The resulting EXE self-extractor I will concatenate with a zip file (a DOS command should suffice to concatenate the two files). This should achieve my goal.

Thoughts?

p.s. Incidentally, I've partly done this years before. I had made a self-extractor (in C# I think) executable, which I then concatenated with a zip file (via a DOS command). Voila, a self-extracting zip executable emerged. What makes my current effort different is that now I'm using C++, and I need this additional / arbitrary DLL entry point.

3

There are 3 answers

1
Jay On

Why jam the two kinds of functionality together? Why don't you just make a dll and an executable that uses it.

0
Vargas On

You can take a look at how NSIS does create a LZMA self-extractor. Then you can probably extend it with a DLL entry point.

Or maybe you could just use a NSIS script to generate the self-extractor and use their script language to create the DLL entry.

0
Shelwien On

As to an exported function in .exe, its not a problem apparently, I tried this and it worked (compile with /FIXED:NO linker option, then copy the executable to 2.exe, then try running original executable and 2.exe)

#include <stdio.h>
#include <windows.h>

//#pragma comment(linker,"/FIXED:NO")

extern "C"
__declspec(dllexport)
void __cdecl func( void ) {
  int a;
  __asm {
    call m0
    m0: pop a
  }
  printf( "dll func! @ %08X\n", a );
}

typedef void (*__cdecl pfunc)( void );

int main( void ) {

  HMODULE h = LoadLibrary( "2.exe" );
  printf( "h=%08X\n", h );

  void* p = GetProcAddress( h, "func" );
  printf( "p=%08X\n", p );

  pfunc f = pfunc(p);
  f();

}

As to lzma, its even less of a problem, try looking at this http://nishi.dreamhosters.com/u/lzma.rar if the original source seems too complicated (lzmadec.c)

I don't quite understand the idea with attaching a zip file... do you mean a .zipx with lzma compression? Or maybe you actually need to look at zlib?