Delphi Can
Exe veya Dll'yi Unit haline dönüştürmek - Baskı Önizleme

+- Delphi Can (https://www.delphican.com)
+-- Forum: Delphi (https://www.delphican.com/forumdisplay.php?fid=3)
+--- Forum: Genel Programlama (https://www.delphican.com/forumdisplay.php?fid=6)
+--- Konu Başlığı: Exe veya Dll'yi Unit haline dönüştürmek (/showthread.php?tid=4815)



Exe veya Dll'yi Unit haline dönüştürmek - carsoft - 11-05-2020

Arkadaşlar selamlar;

Bazı projemde: yan dosyaları, dll, exe, resim gibi: RESOURCE dosyası haline getirerek çalışan EXE'nin bizzat kendisine gömüp kullanıyorum. Fakat ben resource olarak kullanmak istemiyorum. 

Bir DLL veya exe dosyasını bir filestream ile byte byte okuyup, HEX kodları ile, bunu UNIT haline dönüştürebilirmiyiz? 
Daha sonra bunu  projelerimizde bu UNIT'i uSES ekleyerek kullanabilirmiyiz? Yani bir exe veya dll dosyasını UNIT halide dönüştürüp projeme ekleyip kullanabilirmiyim. Bu konuda yardım etmenizi rica ederim.


Exe veya Dll'yi Unit haline dönüştürmek - mrmarman - 11-05-2020

Yıllar yıllar önce benzer bir ihtiyacım olmuş kaynak kodlarıyla paylaşmıştım.


linki burada


Cvp: Exe veya Dll'yi Unit haline dönüştürmek - carsoft - 11-05-2020

(11-05-2020, Saat: 00:51)mrmarman Adlı Kullanıcıdan Alıntı: Yıllar yıllar önce benzer bir ihtiyacım olmuş kaynak kodlarıyla paylaşmıştım.


linki burada

Çok teşekkür ederim muharrem abi, ama verdiginiz linkteki FileToDelphiUnit.rar dosyası arızalı indirdim açılmıyor. Rica etsem tekrar gönderebilirmisiniz bana.


Exe veya Dll'yi Unit haline dönüştürmek - mrmarman - 11-05-2020

Merhaba.

Gönderdiğim sitenin veritabanı sorunsalıdır. 

Tozlu raflardan çıkardım  Smile kodlar aşağıdaki şekilde.

Dilerseniz diske yazar kullanırsınız dilerseniz MemoryStream'e atar DLL ise LoadLibrary( )  FreeLibrary( ) ile dilerseniz nette bulabileceğiniz şekilde EXE from Stream gibi kullanabilirsiniz.

Var
 xDurdur : Boolean = False;

procedure FileToUnit( aSourceFile: String; aTargetUnit, aFunc:TStrings; aStatusPanel:TStatusPanel; aGauge: TGauge );
var
 fsDosya  : TFileStream;
 strHedef : String;
 bBuffer  : array of byte;
 iSayac,
 iByteSay : Integer;
 strUnit,
 strSatir : String;
 tBas, tTahmin: TDateTime;
 boolIlkSatir : Boolean;
begin
 tBas := Now();
// Dosya yoksa dışarı...
 if NOT FileExists(aSourceFile) then EXIT;

 TThread.CreateAnonymousThread( procedure
   begin
     strUnit  := 'Delphi_' + ExtractFileName(ChangeFileExt(aSourceFile, ''));
     strHedef := ExtractFilePath( aSourceFile ) + strUnit + '.PAS';

     // Func
     With aFunc do begin
       Clear;
       Add( '// Dönüşüm Fonksiyonu //' );
       Add( '// ------------------ //' );
       Add( '' );
       Add('USES ' + strUnit );
       Add( '' );
       Add( 'Function Donusum( strDosyaAdi: String ): Boolean; ');
       Add( 'Var                                               ');
       Add( '  fsDosya  : TFileStream;                         ');
       Add( 'begin                                             ');
       Add( '  Result := False;                                ');
       Add( '  Try                                             ');
       Add( '    fsDosya  := TFileStream.Create( strDosyaAdi, fmCreate ); ');
       Add( '    fsDosya.Write( Dizi_'+strUnit+', High( Dizi_'+strUnit+')+1 );   ');
       Add( '    fsDosya.Free;                                 ');
       Add( '  Finally');
       Add( '    Result := True;                               ');
       Add( '  End;');
       Add( 'end;                                              ');
       Add( '' );
       Add( '// Herhangi bir dosya ise aşağıdaki şekil yeterli //              ');
       Add( 'begin;                                            ');
       Add( '  Donusum( ExtractFilePath(Application.Exename) + strOrjDosya )   ');
       Add( 'end;                                              ');
       Add( '' );
       Add( '' );
     end; // with

     // UNIT
     With aTargetUnit do begin
       Clear;
       Add( Format('Unit %s;', [ strUnit ]) );
       Add( '' );
       Add( 'Interface' );
       Add( '' );
       Add( 'Const' );
       Add( '  strOrjDosya = '+ QuotedStr( ExtractFileName(aSourceFile) )+';' );
       Add( '' );
     end; // with

     fsDosya := TFileStream.Create( aSourceFile, fmOpenread );

     aTargetUnit.Add( Format('  Dizi_%s : Array [0..%d] of Byte = ', [ strUnit, fsDosya.Size - 1 ]) );
     aTargetUnit.Add('  (');

     SetLength( bBuffer, fsDosya.Size );
     fsDosya.Read( bBuffer[0], fsDosya.Size );
     iSayac   := 0;
     iByteSay := 0;
     strSatir := '';

     if aGauge <> nil then
     begin
       aGauge.MinValue := 0;
       aGauge.MaxValue := fsDosya.Size;
       aGauge.Visible  := True;
     end;
     boolIlkSatir := True;

     aTargetUnit.BeginUpdate;
     while (iSayac < fsDosya.Size) AND (NOT xDurdur) do
     begin
       if iByteSay = 0 then
       begin
         if strSatir <> '' then aTargetUnit.Add( strSatir );
         iByteSay := 30;
         strSatir := '';
         // 10   100 byte
         tTahmin  := tBas;
         if iSayac > 0 then
           tTahmin  := IncMilliSecond( tBas, (fsDosya.Size * MilliSecondsBetween( Now(), tBas ) div iSayac) );
         if aStatusPanel <> nil
           then aStatusPanel.Text := Format('%d / %d byte (-%d sn.)', [iSayac, fsDosya.Size, SecondsBetween(tTahmin, Now()) ]);
       end;

       if strSatir = '' then
       begin
         if boolIlkSatir then
         begin
           strSatir :=            Format('  $%.2x', [ bBuffer[iSayac] ]);
           boolIlkSatir := False;
         end else begin
           strSatir :=            Format(' ,$%.2x', [ bBuffer[iSayac] ]);
         end;
       end
       else strSatir := strSatir + Format(',$%.2x', [ bBuffer[iSayac] ]);

       inc(iSayac);
       Dec(iByteSay);

       if aGauge <> nil then
         TThread.Synchronize(nil, procedure
           begin
             aGauge.Progress := iSayac;
           end );
       // Application.ProcessMessages;
     end; // while
     aTargetUnit.EndUpdate;

     if strSatir <> '' then aTargetUnit.Add( strSatir );
     if aGauge    <> nil then aGauge.Visible := False;

     if xDurdur then
     begin
       aTargetUnit.Add('');
       aTargetUnit.Add('// -------------------------------------------- //');
       aTargetUnit.Add('//   !!!   Kullanıcı Tarafından Durduruldu !!!  //');
       aTargetUnit.Add('// -------------------------------------------- //');
     end else
     begin
       aTargetUnit.Add('  );');
       aTargetUnit.Add('implementation');
       aTargetUnit.Add('end.');
     end;

   end ).Start;
 fsDosya.Free;
end;



Kullanımı :
Edit1 : Kaynak Dosya
Memo1 : Üretilen Unit
Memo2 : dönüşüm Function
StatusBar Panel : Durum ve kalan süre
Gauge1 : % olarak durum

- işlem sırasında global değişken xDurdur  değeri TRUE olursa işlem iptal edilip döngüden çıkılır.
 
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
 if TBitBtn(Sender).Caption = 'Dönüştür' then
 begin
   BitBtn3.Enabled := False;
   xDurdur := False;
   TBitBtn(Sender).Caption := 'Durdur';
   Memo1.Visible := False;

   FileToUnit( Edit1.Text, Memo1.Lines, Memo2.Lines, StatusBar1.Panels[0], Gauge1 );

   Memo1.Visible   := True;
   BitBtn3.Enabled := True;
 end else begin
   BitBtn3.Enabled := True;
   xDurdur := True;
   TBitBtn(Sender).Caption := 'Dönüştür';
   Memo1.Visible := True;
 end;
end;

kal6ak7a5mgd9cgv5bv1.png


Cvp: Exe veya Dll'yi Unit haline dönüştürmek - carsoft - 12-05-2020

(11-05-2020, Saat: 09:25)mrmarman Adlı Kullanıcıdan Alıntı: Merhaba.

Gönderdiğim sitenin veritabanı sorunsalıdır. 

Tozlu raflardan çıkardım  Smile kodlar aşağıdaki şekilde.

Dilerseniz diske yazar kullanırsınız dilerseniz MemoryStream'e atar DLL ise LoadLibrary( )  FreeLibrary( ) ile dilerseniz nette bulabileceğiniz şekilde EXE from Stream gibi kullanabilirsiniz.

Var
 xDurdur : Boolean = False;

procedure FileToUnit( aSourceFile: String; aTargetUnit, aFunc:TStrings; aStatusPanel:TStatusPanel; aGauge: TGauge );
var
 fsDosya  : TFileStream;
 strHedef : String;
 bBuffer  : array of byte;
 iSayac,
 iByteSay : Integer;
 strUnit,
 strSatir : String;
 tBas, tTahmin: TDateTime;
 boolIlkSatir : Boolean;
begin
 tBas := Now();
// Dosya yoksa dışarı...
 if NOT FileExists(aSourceFile) then EXIT;

 TThread.CreateAnonymousThread( procedure
   begin
     strUnit  := 'Delphi_' + ExtractFileName(ChangeFileExt(aSourceFile, ''));
     strHedef := ExtractFilePath( aSourceFile ) + strUnit + '.PAS';

     // Func
     With aFunc do begin
       Clear;
       Add( '// Dönüşüm Fonksiyonu //' );
       Add( '// ------------------ //' );
       Add( '' );
       Add('USES ' + strUnit );
       Add( '' );
       Add( 'Function Donusum( strDosyaAdi: String ): Boolean; ');
       Add( 'Var                                               ');
       Add( '  fsDosya  : TFileStream;                         ');
       Add( 'begin                                             ');
       Add( '  Result := False;                                ');
       Add( '  Try                                             ');
       Add( '    fsDosya  := TFileStream.Create( strDosyaAdi, fmCreate ); ');
       Add( '    fsDosya.Write( Dizi_'+strUnit+', High( Dizi_'+strUnit+')+1 );   ');
       Add( '    fsDosya.Free;                                 ');
       Add( '  Finally');
       Add( '    Result := True;                               ');
       Add( '  End;');
       Add( 'end;                                              ');
       Add( '' );
       Add( '// Herhangi bir dosya ise aşağıdaki şekil yeterli //              ');
       Add( 'begin;                                            ');
       Add( '  Donusum( ExtractFilePath(Application.Exename) + strOrjDosya )   ');
       Add( 'end;                                              ');
       Add( '' );
       Add( '' );
     end; // with

     // UNIT
     With aTargetUnit do begin
       Clear;
       Add( Format('Unit %s;', [ strUnit ]) );
       Add( '' );
       Add( 'Interface' );
       Add( '' );
       Add( 'Const' );
       Add( '  strOrjDosya = '+ QuotedStr( ExtractFileName(aSourceFile) )+';' );
       Add( '' );
     end; // with

     fsDosya := TFileStream.Create( aSourceFile, fmOpenread );

     aTargetUnit.Add( Format('  Dizi_%s : Array [0..%d] of Byte = ', [ strUnit, fsDosya.Size - 1 ]) );
     aTargetUnit.Add('  (');

     SetLength( bBuffer, fsDosya.Size );
     fsDosya.Read( bBuffer[0], fsDosya.Size );
     iSayac   := 0;
     iByteSay := 0;
     strSatir := '';

     if aGauge <> nil then
     begin
       aGauge.MinValue := 0;
       aGauge.MaxValue := fsDosya.Size;
       aGauge.Visible  := True;
     end;
     boolIlkSatir := True;

     aTargetUnit.BeginUpdate;
     while (iSayac < fsDosya.Size) AND (NOT xDurdur) do
     begin
       if iByteSay = 0 then
       begin
         if strSatir <> '' then aTargetUnit.Add( strSatir );
         iByteSay := 30;
         strSatir := '';
         // 10   100 byte
         tTahmin  := tBas;
         if iSayac > 0 then
           tTahmin  := IncMilliSecond( tBas, (fsDosya.Size * MilliSecondsBetween( Now(), tBas ) div iSayac) );
         if aStatusPanel <> nil
           then aStatusPanel.Text := Format('%d / %d byte (-%d sn.)', [iSayac, fsDosya.Size, SecondsBetween(tTahmin, Now()) ]);
       end;

       if strSatir = '' then
       begin
         if boolIlkSatir then
         begin
           strSatir :=            Format('  $%.2x', [ bBuffer[iSayac] ]);
           boolIlkSatir := False;
         end else begin
           strSatir :=            Format(' ,$%.2x', [ bBuffer[iSayac] ]);
         end;
       end
       else strSatir := strSatir + Format(',$%.2x', [ bBuffer[iSayac] ]);

       inc(iSayac);
       Dec(iByteSay);

       if aGauge <> nil then
         TThread.Synchronize(nil, procedure
           begin
             aGauge.Progress := iSayac;
           end );
       // Application.ProcessMessages;
     end; // while
     aTargetUnit.EndUpdate;

     if strSatir <> '' then aTargetUnit.Add( strSatir );
     if aGauge    <> nil then aGauge.Visible := False;

     if xDurdur then
     begin
       aTargetUnit.Add('');
       aTargetUnit.Add('// -------------------------------------------- //');
       aTargetUnit.Add('//   !!!   Kullanıcı Tarafından Durduruldu !!!  //');
       aTargetUnit.Add('// -------------------------------------------- //');
     end else
     begin
       aTargetUnit.Add('  );');
       aTargetUnit.Add('implementation');
       aTargetUnit.Add('end.');
     end;

   end ).Start;
 fsDosya.Free;
end;



Kullanımı :
Edit1 : Kaynak Dosya
Memo1 : Üretilen Unit
Memo2 : dönüşüm Function
StatusBar Panel : Durum ve kalan süre
Gauge1 : % olarak durum

- işlem sırasında global değişken xDurdur  değeri TRUE olursa işlem iptal edilip döngüden çıkılır.
 
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
 if TBitBtn(Sender).Caption = 'Dönüştür' then
 begin
   BitBtn3.Enabled := False;
   xDurdur := False;
   TBitBtn(Sender).Caption := 'Durdur';
   Memo1.Visible := False;

   FileToUnit( Edit1.Text, Memo1.Lines, Memo2.Lines, StatusBar1.Panels[0], Gauge1 );

   Memo1.Visible   := True;
   BitBtn3.Enabled := True;
 end else begin
   BitBtn3.Enabled := True;
   xDurdur := True;
   TBitBtn(Sender).Caption := 'Dönüştür';
   Memo1.Visible := True;
 end;
end;

kal6ak7a5mgd9cgv5bv1.png
Çok teşekkür ederim muharrem abi. Zahmet verdim çok sağol