Solved

How to use an .exe file as a .res file and then call using submenu

Posted on 2000-02-19
11
349 Views
Last Modified: 2010-04-04
To inthe, yesterday I spent 2 hrs modifying the text file to .exe file, but I still got compiliing errors. It keeps refering me back to the res file I created. To do it the right way, can u advice as to what codes to modify. thanks, u save my life.


Barry's answer
first make a demo project directory  for this then
go to hard drive and find a text file and re-name it test.txt or (make one)

open notepad and type the following line:

TEXT_DEMO  RCDATA C:\test.txt

save this as myres.rc  and put in in delphis bin dir.

TEXT_DEMO is the idendifier ,rcdata is just a datatype and c:\test.txt is the path to your txt file.
 
go to command prompt delphi\bin directory compile myres.rc with brcc32.exe.
if you havent used it before you just at the command prompt type:
brcc32 myres.rc
and you will get a compiled one called myres.res ,put myres.res in your projects directory so it can find it.

now you have your static text file inside a resource file.


in delphi:
create a new project and save it in your project dir that you made where the myres file resides.

drop a richedit on the form
and got to code and add in the private section the following line:
 procedure LoadTextFromResource;
then add in
{myres.rc} in the line below {$R *.DFM}

then add the following procedure:

procedure TForm1.LoadTextFromResource;
var
  ResID  : String;
  FndRes : HRSRC;
  LdRes  : THandle;
  Buffer : PChar;
  Stream : TMemoryStream;
begin
  ResID := 'TEXT_DEMO';
  //Locates the resource and returns a handle to it.
  // If the function fails it returns 0.
  FndRes := FindResource(HInstance,
                        MakeIntResource(ResID),
                        RT_RCDATA);
  if FndRes <> 0 then
  begin
    //Loads the resource into global memory and returns a handle to     //the global memory block containing the data associated with the     //resource. If this function fails it also returns 0.
    LdRes := LoadResource(Hinstance,
                          FndRes);
    if LdRes <> 0 then
    begin
      //If the loaded resource is locked, the return value is a
      // pointer to the first byte of the resource otherwise, it is       // NULL.
      Buffer := LockResource(LdRes);
      // To make this a valid PChar, you should add a null at the       // end of the buffer, but this demo works without error as far       // as I know.
      try
        Stream := TMemoryStream.Create;
        try
          Stream.WriteBuffer(Pointer(Buffer)^, Length(Buffer));
          //Important, must set the stream position to the first byte           //of data. Will not work without this.
          Stream.Position := 0;
          Form1.RichEdit1.Lines.LoadFromStream(Stream);
        finally
          Stream.Free;
        end;
      finally
        FreeResource(LdRes);
      end;
    end;
  end;
end;


now drop a button on the form and  make the buttons click event like:

procedure TForm1.Button1Click(Sender: TObject);
begin
LoadTextFromResource;
end;


now compile and run and when the button is clicked the richedit will be filled with your test file...

your complete unit will be like:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure LoadTextFromResource;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
{$R Myres.RES}

procedure TForm1.LoadTextFromResource;
var
  ResID  : String;
  FndRes : HRSRC;
  LdRes  : THandle;
  Buffer : PChar;
  Stream : TMemoryStream;
begin
  ResID := 'TEXT_DEMO';
  //Locates the resource and returns a handle to it.
  // If the function fails it returns 0.
  FndRes := FindResource(HInstance,
                        MakeIntResource(ResID),
                        RT_RCDATA);
  if FndRes <> 0 then
  begin
    //Loads the resource into global memory and returns a handle to     //the global memory block containing the data associated with the     //resource. If this function fails it also returns 0.
    LdRes := LoadResource(Hinstance,
                          FndRes);
    if LdRes <> 0 then
    begin
      //If the loaded resource is locked, the return value is a
      // pointer to the first byte of the resource otherwise, it is       // NULL.
      Buffer := LockResource(LdRes);
      // To make this a valid PChar, you should add a null at the       // end of the buffer, but this demo works without error as far       // as I know.
      try
        Stream := TMemoryStream.Create;
        try
          Stream.WriteBuffer(Pointer(Buffer)^, Length(Buffer));
          //Important, must set the stream position to the first byte           //of data. Will not work without this.
          Stream.Position := 0;
          Form1.RichEdit1.Lines.LoadFromStream(Stream);
        finally
          Stream.Free;
        end;
      finally
        FreeResource(LdRes);
      end;
    end;
  end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
LoadTextFromResource;
end;

end.


0
Comment
Question by:jonlaukt
  • 5
  • 5
11 Comments
 

Author Comment

by:jonlaukt
ID: 2538494
the question is open to all other experts out there as well. I am still stuck on the problem.
0
 
LVL 17

Accepted Solution

by:
inthe earned 30 total points
ID: 2538823
hi,
for a exe file it is bit different,you use EXEFILE instead etc ..

make a .rc file that looks like:

 TESTFILE EXEFILE C:\Windows\Notepad.exe

and save as myres.rc.
compile with brcc32 to get myres.res

then in your unit you would do

{$R *.DFM}
{$R MYRES.RES}

procedure ExtractRes(ResType, ResName, ResNewName : String);
      var
        Res : TResourceStream;
      begin
        Res := TResourceStream.Create(Hinstance, Resname, Pchar(ResType));
        Res.SavetoFile(ResNewName);
        Res.Free;
      end;


procedure TForm1.Button1Click(Sender: TObject);
begin
ExtractRes('EXEFILE','TESTFILE','C:\TEST_EXE_FROM_RES.EXE');
ShellExecute(Form1.Handle,'open','c:\Test_exe_from_res.exe',nil,nil,SW_SHOWNORMAL);
end;


if you need more help just shout
cheers Barry
0
 
LVL 17

Expert Comment

by:inthe
ID: 2538830
oh hello,
i just seen your other question about tthe compiler error.
i see you already have the exe code i pasted above.
the code should compile fine with the button1 declared as snoopdog said.if not i send a demo project to you.
 
0
 

Author Comment

by:jonlaukt
ID: 2539413
I get a compiler error that shellexecure is an undeclared identifier, pls help
0
 

Author Comment

by:jonlaukt
ID: 2539414
To save u trouble, maybe can just send a sample program to me and I will try and figure it out. Thanks pal.email:jonlaukt@singnet.com.sg
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:inthe
ID: 2539502
"shellexecute is an undeclared identifier "

all you have to do is add "Shellapi" to the uses section.

or even delete the shellexecute line,all it is doing is executing the newly extracted exe file..so to show the exe file works.
0
 

Author Comment

by:jonlaukt
ID: 2540133
one more try, I have feeling that compiler has bugs,
notepad save this as display1.rc
TESTFILE1 EXEFILE e:\acload\display1.EXE
go to dos promt e:\delphi\bin-brcc32 display1.rc

type the delphi codes as above with shellapi
create file called test,save delphi file,display.rc and .res, compile delphi still get message,warning duplicate resources,tyoe10 (rcdata),id,tform1.. and file\test\includexe.dfm resource kept.
file\test\includexe.dfm resource discarded,does not compile,I am using delphi2, the applications done in 2 should be compatible in delphi 4 right?
0
 
LVL 17

Expert Comment

by:inthe
ID: 2540303
i have only used exe in res friom d3 onwards but for this code there shouldnt be a problem between d2 and d4..

would you mind making a project with all files you need for this example and i can test it in d4.

did it work putting notepad,exe inside as a resource?

0
 

Author Comment

by:jonlaukt
ID: 2540469
hold on I getting something, but everytime/sometimes when  I compile a .pas or .dpr I get "cannot run a unit or dll", whats that, buddy?
0
 
LVL 17

Expert Comment

by:inthe
ID: 2540499
for the moment i suggest removing shellapi from the uses section and remove ( or comment out) the shellexecute lines.

(i read there is problem with shellapi unit in d2 so we need to be sure this is not whats causing the problem).

if you havent got it work by this time tommorrow i make a demo and send you to try in d2.i might have pc at work with d2 still on i have 2 check in morning.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2924508
Bought Q...
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now