dlltoexe.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "std.h"
  2. #include "dlltoexe.h"
  3. #pragma pack( push,1 )
  4. struct Head{
  5. short machine,num_sects;
  6. int timedata,sym_table,num_syms;
  7. short opt_size,chars;
  8. };
  9. struct Opt1{
  10. short magic;
  11. char major,minor;
  12. int code_size,data_size,udata_size;
  13. int entry,code_base,data_base;
  14. };
  15. struct Sect{
  16. char name[8];
  17. int virt_size,virt_addr; //in mem
  18. int data_size,data_addr; //on disk
  19. int relocs,lines; //file ptrs
  20. short num_relocs,num_lines;
  21. int chars;
  22. };
  23. #pragma pack( pop )
  24. bool dllToExe( const char *exe_file,const char *dll_file,const char *entry_func ){
  25. //find proc address of bbWinMain
  26. HMODULE hmod=LoadLibrary( dll_file );if( !hmod ) return false;
  27. int proc=(int)GetProcAddress( hmod,entry_func );
  28. int entry=proc-(int)hmod;FreeLibrary( hmod );
  29. if( !proc ) return false;
  30. //Convert dll to exe
  31. fstream in( dll_file,ios_base::binary|ios_base::in );if( !in.is_open() ) return false;
  32. fstream out( exe_file,ios::binary|ios_base::out|ios_base::trunc );if( !out.is_open() ) return false;
  33. int offs;
  34. in.seekg( 0x3c );
  35. in.read( (char*)&offs,4 );
  36. //copy first bit...
  37. in.seekg( 0 );
  38. for( int k=0;k<offs+4;++k ) out.put( in.get() );
  39. //reader file header
  40. Head head={0};
  41. in.read( (char*)&head,sizeof(head) );
  42. //change DLL to EXE
  43. head.chars=0x10e;
  44. out.write( (char*)&head,sizeof(head) );
  45. //read opts 1
  46. Opt1 opt1={0};
  47. in.read( (char*)&opt1,sizeof(opt1) );
  48. opt1.entry=entry;
  49. out.write( (char*)&opt1,sizeof(opt1) );
  50. //copy rest of file...
  51. while( !in.eof() ){
  52. out.put( in.get() );
  53. }
  54. out.close();
  55. in.close();
  56. return true;
  57. }