Lazarus

Programming => General => Topic started by: Jake012345 on June 22, 2020, 10:42:58 am

Title: Dynamic Application Creation
Post by: Jake012345 on June 22, 2020, 10:42:58 am
Hello!

I want to create .exe files (with really function)  dynamically from an other application.
Just example
For example I run an App. and I pressed a button on form, and on the desktop appears an .exe what delete the main application if I run it.
But I have to write the filepath of main Application in the created.
Title: Re: Dynamic Application Creation
Post by: ASerge on June 22, 2020, 11:39:35 am
For example I run an App. and I pressed a button on form, and on the desktop appears an .exe what delete the main application if I run it.
But I have to write the filepath of main Application in the created.
You need to create two projects. In the first project, you will make the necessary removal functionality. And the second one will contain the .exe file from the first project as a resource. At the right time, it will extract it from the resource and save it on disk.
Title: Re: Dynamic Application Creation
Post by: Handoko on June 22, 2020, 11:43:44 am
@jake012345

It sounds you're trying to do something evil.  :D

Anyway, I will achieve the goal like this. First I write the program and generate the exe (that will be put on the desktop later). Then make it as a resource file for combine into the main application.  When the button clicked, I simply export the resource file to desktop.

You can learn more about resource file:
https://wiki.freepascal.org/Lazarus_Resources (https://wiki.freepascal.org/Lazarus_Resources)

Well, I'm late. ASerge already answered.

edit:
You may need to add a new feature in the main program. It should able to scan the hardcoded string in the exported exe and replace it with the path of your main program. It's not hard, I ever done similar thing. You can use stream or the traditional file (block) read/write.
Title: Re: Dynamic Application Creation
Post by: jamie on June 22, 2020, 01:22:42 pm
I dont know about os but I believe windows put a stop to reclcling app creation where by it detects suck hacking attempts to lock a app in position like that.

It has been a common hack to render the pc basically useless so you can justify your need all u want , I dont think it's going happen, at least with the current addition of windows.
Title: Re: Dynamic Application Creation
Post by: Thaddy on June 22, 2020, 01:40:22 pm
You need to create two projects. In the first project, you will make the necessary removal functionality. And the second one will contain the .exe file from the first project as a resource. At the right time, it will extract it from the resource and save it on disk.
Does not work anymore...( at least not here, but I use devel versions of windows 10)
Addition: you can/could also execute the resource directly, without saving to disk, but not with the latest updates to win10 - 64 bit.
Title: Re: Dynamic Application Creation
Post by: Jake012345 on June 24, 2020, 02:17:37 pm
no-no-no!  :)

I want to create fast 'suspend-er' .exe-s for my project (https://forum.lazarus.freepascal.org/index.php/topic,50168.msg365697.html#msg365697)

When I lock a folder, the locker App. Create a fast unlock exe in the folder, what want the password and send the suspend/un-suspend command to the locker.
Title: Re: Dynamic Application Creation
Post by: Warfley on June 24, 2020, 03:37:33 pm
As I already stated in your other thread (https://forum.lazarus.freepascal.org/index.php/topic,50168.15.html), I think you are doing it wrong, because you are doing some needlessly over complicated stuff that does not even work, because there is no such thing as an unstoppable process. If you would do it the other way around it would be much easier, portable, and you wouldn't run into such niche problems you are doing now.

That said, what you are trying to do will probably not work as soon as you try running it on a machine that has some antivirus installed, because this is very often used by malware (you have a program that ships the malware, and extracts it).
That you want to dynamically write things into it doesn't make it better, because this is also a common practice for malware, that they modify themselves to a. add information to the executable or b. change their checksums so they don't get detected by antivirus programs.
So it would be a miracle if you get your program running on any properly updated windows computer with an antivirus.

But lets say you are on a computer with no such protection and the OS let's you do this, then this is easy:
1. create your program you want to pack.
2. Add a pattern string as a hardcoded constant to your program, which will be used as the thing you want to change
3. Put this program into the resources of your shipping program (multiple possibilities, e.g. encode it to base64 and put it into a string constant)
4. the shipping program then extracts the program, makes a simple text search-and-replace for the pattern hardcoded in step 2 with the information you want to put in (like your path) and writes it to the filesystem.

And voila, you have a customized executable shipped with your program
Title: Re: Dynamic Application Creation
Post by: Jake012345 on June 29, 2020, 08:08:52 pm
How can I use the resources?
I see what's that - otherwise I wanted to do something with that before, but I didn't know, that's what I need - but I can't use it.

I saw the wiki(s) but It's a bit hard to understand (I coudn't use the 'lazres' [?]), and I saw the example in Root folder of lazarus But.... For me..? :D

BTW: Thanks for unstoppable support for everyone!
Title: Re: Dynamic Application Creation
Post by: Handoko on June 30, 2020, 06:14:59 am
Yes, that is the wiki, which has all the information you need.

Do you use Lazarus? If yes, simply forget all the things mentioned by the wiki, forget about LRS Explorer and forget about lazres. Instead, you can go directly to this page:

https://wiki.freepascal.org/IDE_Window:_Project_Options#Resources (https://wiki.freepascal.org/IDE_Window:_Project_Options#Resources)

Need example code of using the resource? You can download my glSlideshow, in the Form Create event it loads an image from resource stream and uses it as the form's background.

https://forum.lazarus.freepascal.org/index.php/topic,35313.msg256719.html#msg256719 (https://forum.lazarus.freepascal.org/index.php/topic,35313.msg256719.html#msg256719)
Title: Re: Dynamic Application Creation
Post by: Jake012345 on June 30, 2020, 03:19:27 pm
And what's your unit that enable theese to you?
I'm using LResources. And the ide can't find the 'RT_RCDATA'.
So something's wrong in here!
Title: Re: Dynamic Application Creation
Post by: Jake012345 on June 30, 2020, 04:03:10 pm
And Can I modify/create the resources dinamycally?

And How is the similar technic the using "bin" files works?
Title: Re: Dynamic Application Creation
Post by: Handoko on June 30, 2020, 04:28:01 pm
And Can I modify/create the resources dinamycally?

Yes, you can. Load the resource to memory, then you can modify it as you like. Pascal is great in modifying data in memory.

Creating resource dynamically? I don't think so.

I'm using LResources. And the ide can't find the 'RT_RCDATA'.
So something's wrong in here!

It's working good here. I'm sure you did it wrong.

And How is the similar technic the using "bin" files works?

Not fully understand what you meant.
Title: Re: Dynamic Application Creation
Post by: PascalDragon on June 30, 2020, 04:30:03 pm
And Can I modify/create the resources dinamycally?

And How is the similar technic the using "bin" files works?

To modify resources in another binary you should take a look at FCL-Res (https://www.freepascal.org/docs-html/fclres/basic%20usage.html).
Title: Re: Dynamic Application Creation
Post by: Jake012345 on June 30, 2020, 04:32:13 pm
I mean a separately resource file with the executable but not IN.
For example MyData.bin=[file1.asd,file2.ttr,file.hgd,...]
Title: Re: Dynamic Application Creation
Post by: Handoko on June 30, 2020, 05:02:53 pm
Did you mean you want to store some data in a separate file?
If yes, then that is easy. You can use a database, manual binary file access, TFilestream, etc.
Title: Re: Dynamic Application Creation
Post by: winni on June 30, 2020, 05:14:06 pm
Did you mean you want to store some data in a separate file?
If yes, then that is easy. You can use a database, manual binary file access, TFilestream, etc.

And  a ZIP file. fpc can read that.

Winni
Title: Re: Dynamic Application Creation
Post by: Handoko on June 30, 2020, 05:23:46 pm
Did you mean you want to store some data in a separate file?
If yes, then that is easy. You can use a database, manual binary file access, TFilestream, etc.

And  a ZIP file. fpc can read that.

Ah, yes. Almost forget.
I just learned how to use Tcompressionstream (unit zstream) some time ago. Not too difficult, just need to spend some time to understand it.
Title: Re: Dynamic Application Creation
Post by: Mr.Madguy on June 30, 2020, 05:53:32 pm
Again my old-ProcessExplorer-style launcher:
Code: Pascal  [Select][+][-]
  1. program Launcher;
  2.  
  3. {$R 'Prog.res' 'Prog.rc'}
  4.  
  5. uses
  6.   Windows,
  7.   SysUtils,
  8.   ShellAPI,
  9.   Classes;
  10.  
  11. {$R *.res}
  12.  
  13. type
  14.   TIsWOW64Process = function(hProcess:THandle;var IsWOW64:Boolean):Boolean;stdcall;
  15.  
  16. const
  17.   FileName = 'Prog.exe';
  18.   Res32 = 'Prog32';
  19.   Res64 = 'Prog64';
  20.  
  21. function GetTempFolder: String;
  22. var
  23.   Buffer: array [0..MAX_PATH+1] of WideChar;
  24. begin
  25.   GetTempPath(MAX_PATH, Buffer);
  26.   Result := IncludeTrailingPathDelimiter(String(Buffer));
  27. end;
  28.  
  29. function Is64Process:Boolean;
  30.   var Kernel:HMODULE;IsWOW64Process:TIsWOW64Process;Temp:Boolean;
  31. begin
  32.   Result := false;
  33.   Kernel := LoadLibrary('kernel32');
  34.   if Kernel = 0 then Exit;
  35.   IsWOW64Process := GetProcAddress(Kernel, 'IsWow64Process');
  36.   if not Assigned(IsWow64Process) then Exit;
  37.   IsWOW64Process(GetCurrentProcess, Temp);
  38.   Result := Temp;
  39.   FreeLibrary(Kernel);
  40. end;
  41.  
  42. var ResName, FilePath, Params:String;
  43.   Source:TResourceStream;Dest:TStream;
  44.   I:Integer;StartupInfo:TStartupInfo;
  45.   ProcessInfo:TProcessInformation;
  46.  
  47. begin
  48.   if Is64Process then begin
  49.     ResName := Res64;
  50.   end
  51.   else begin
  52.     ResName := Res32;
  53.   end;
  54.   FilePath := GetTempFolder + FileName;
  55.   Source := TResourceStream.Create(hInstance, ResName, RT_RCDATA);
  56.   Dest := TFileStream.Create(FilePath, fmCreate);
  57.   Dest.CopyFrom(Source, 0);
  58.   Dest.Write(Source.Memory, Source.Size);
  59.   Dest.Free;
  60.   Source.Free;
  61.   Params := '"'+FilePath+'"';
  62.   for I := 1 to ParamCount do begin
  63.     Params := Params + ' ' + ParamStr(I);
  64.   end;
  65.   GetStartupInfo(StartupInfo);
  66.   if CreateProcess(PWideChar(FilePath),
  67.     PWideChar(Params),
  68.     nil,
  69.     nil,
  70.     false,
  71.     CREATE_UNICODE_ENVIRONMENT,
  72.     nil,
  73.     PWideChar(GetCurrentDir),
  74.     StartupInfo,
  75.     ProcessInfo) then begin
  76.       if ProcessInfo.hProcess <> 0 then begin
  77.         WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
  78.       end;
  79.   end;
  80.   DeleteFile(FilePath);
  81. end.
  82.  
Prog.rc:
Code: [Select]
Prog32 RCDATA ..\Prog\Win32\Release\Prog.exe
Prog64 RCDATA ..\Prog\Win64\Release\Prog.exe
But there are problems:
1) Resource file isn't updated automatically - you need to delete it to update
2) Clunky stuff with command line parameters is used because anti-viruses didn't like GetCommandLine.
3) Anti-viruses no longer like CreateProcess - use ShellExecute instead. But you won't be able to delete your temp file in this case, as ShellExecute returns control immediately.
4) Some other stuff, related to passing correct environment to that program, isn't right and has been fixed in future versions.
Title: Re: Dynamic Application Creation
Post by: Handoko on June 30, 2020, 06:05:03 pm
Again my old-ProcessExplorer-style launcher:

Cool!

I should copy and back up it, it has many rare to seen tricks. Unfortunately I do not do Windows things now.

Anyway, thank you for sharing it.
Title: Re: Dynamic Application Creation
Post by: alaa123456789 on June 30, 2020, 07:24:20 pm
hi

you can learn here about using resources , i think this what are you looking for

https://www.youtube.com/watch?v=sBcS9hfCfKI&list=PLUIPhGMf8-xogHPrOc5rt5-7ALxJzQesr

thanks
Title: Re: Dynamic Application Creation
Post by: Jake012345 on June 30, 2020, 09:56:45 pm
Okay!

I tried to make an installer (now just the decompress, so I have to add the files in a zip file to resources manually) but I can't get the count of compressed files.

I'm using zipper->TUnZipper

I used the .examine too but nothing.

I'm always got 0.
Title: Re: Dynamic Application Creation
Post by: TRon on July 01, 2020, 08:16:36 am
I'm always got 0.
I'm speechless...
TinyPortal © 2005-2018