Author: Jonas Bilinkevicius
Does anyone have experience using Delphi to create program that can create
standalone exe that contains code and data like Picture2exe? This program creates a
stand alone executable exe file that contains image and sound data that plays them
in a slideshow. What is the approach and techniques used?
Answer:
Try this code where discclone.res includes the file you want to include:
1
2 procedure TMain.mnuCreateClick(Sender: TObject);
3 var
4 MyFile: TFileStream;
5 MyAppend: TMemoryStream;
6 begin
7 if diagOpenSelf.Execute then
8 begin
9 if diagCreateSelf.Execute then
10 begin
11 CopyFile(PChar(ExtractFilePath(ParamStr(0)) + '\Extractor.exe'),
12 PChar(diagCreateSelf.FileName), False);
13 {Create a filestream object for the extractor executable}
14 MyFile := TFileStream.Create(diagCreateSelf.FileName, $0002);
15 try
16 MyAppend := TMemoryStream.Create;
17 try
18 MyAppend.LoadFromFile(diagOpenSelf.FileName);
19 MyFile.Seek(0, soFromEnd);
20 MyFile.CopyFrom(MyAppend, 0);
21 MessageBox(0, 'File was successfully created.', 'File Created',
22 MB_OK + MB_ICONINFORMATION);
23 finally
24 MyAppend.Free;
25 end;
26 finally
27 MyFile.Free;
28 end;
29 end;
30 end;
31 end;
32
33 program Extractor;
34
35 {$R DiscClone.res}
36
37 uses
38 Windows, Classes, ShellAPI, Sysutils;
39
40 const
41 FileSize = 64512;
42 {Or 60416. You may have to change to this number to the size of the
43 compiled Extractor executable - minus the appended executable of course.}
44 var
45 {MyExtract: TFileStream;}
46 MyFile: TMemoryStream;
47 TempStream: TMemoryStream;
48 FileExe: string;
49 Buffer: array[0..260] of Char;
50 Count: DWord;
51 Buf: Pointer;
52 G: THandle;
53 Res: LongBool;
54 begin
55 { ... }
56 {ask to make sure}
57 { ... }
58 {check floppy in drive}
59 { ... }
60 TempStream := TMemoryStream.Create;
61 {Create the memory stream which will hold a copy of this executable in memory}
62 MyFile := TMemoryStream.Create;
63 try
64 SetString(FileExe, Buffer, GetModuleFileName(0, Buffer,
65 SizeOf(Buffer))); {What is the name of this executable?}
66 MyFile.LoadFromFile(FileExe); {Load a copy of the executable into memory}
67 {A filestream which will eventually create the HelloWorld program}
68 // MyExtract := TFileStream.Create('dummy.floppy', fmCreate);
69 try
70 MyFile.Seek(FileSize, 0);
71 Move the stream pointer to the start of the appended executable}
72 {Copy the appended data to our filestream buffer - this creates the file}
73 // MyExtract.CopyFrom(MyFile, MyFile.Size - FileSize);
74 TempStream.CopyFrom(MyFile, MyFile.Size - FileSize);
75 finally
76 // MyExtract.Free; {Free the filestream object}
77 end;
78 {Tell the user that extraction went well and ask to run HelloWorld}
79 G := CreateFile('\\.\A:', GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING,
80 FILE_ATTRIBUTE_NORMAL, 0);
81 // F := CreateFile(PChar('\\.\' + location), GENERIC_READ or GENERIC_WRITE,
82 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
83 GetMem(Buf,1457664); {1457664}
84 // SetFilePointer(F, 0, nil, FILE_BEGIN);
85 // ReadFile(F, Buf^, 1457664, Count, nil);
86 // Buf:=@MyExtract; //new
87 WriteFile(G, Pointer(TempStream)^, 1457664, Count, nil);
88 // WriteFile(G, Buf^, 1457664, Count, nil);
89 // ShowMessage(IntToStr(GetLastError));
90 FreeMem(Buf);
91 // CloseHandle(F);
92 CloseHandle(G);
93
94 { G := CreateFile(PChar('\\.\C:\Work\Boot\TestCenter\Fred.txt'),
95 GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_ALWAYS,
96 FILE_ATTRIBUTE_NORMAL, 0);}
97 G := CreateFile('\\.\A:', GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING,
98 FILE_ATTRIBUTE_NORMAL, 0);
99 GetMem(Buf, 1474560);
100 TempStream.Position := 0;
101 TempStream.read(Buf^, 1474560);
102 res := WriteFile(G, Buf^, 1474560, Count, nil);
103 if not res then
104 MessageBox(0, PChar(IntToStr(GetLastError)),
105 PChar(SysErrorMessage(GetLastError)),
106 MB_OK);
107 CloseHandle(G);
108 FreeMem(Buf);
109 // FlushFileBuffers(MyFileStream.Handle);
110 MessageBox(0, PChar(IntToStr(TempStream.size)), 'Extraction successful!',
111 MB_OK + MB_ICONQUESTION)
112 finally
113 {Free the memoerystream object}
114 MyFile.Free;
115 end;
116 TempStream.Free;
117 end.
|