-3

I have two applications everyone do different tasks, the first application runs the second one using shellExecute.

ShellExecute(Handle, nil, PChar('D:\Project2.exe'), nil, nil, SW_SHOWNORMAL);
ShellExecute(Handle, nil, PChar('cmd.exe'), PChar('/C D:\Project2.exe'), nil, SW_SHOWNORMAL);

the second application extracts resources that already exist inside it.

The problem is : When I run the second application from the first application the resources doesn't extracted Although the application form show to me , but If I run the second directly "double-click" the resources extracted !

I also tried CreateProcess and WinExec, and the resource doesn't extracted too !!

Edit: the project1 have a button only open project2

procedure TForm1.Button1Click(Sender: TObject);
begin
    ShellExecute(Handle, nil, PChar('D:\Project2.exe'), nil, nil, SW_SHOWNORMAL);
end;

this is project2 code , in the create section the resources must be extracted !

procedure TForm1.FormCreate(Sender: TObject);
var
  DllRS: TResourceStream;
begin
    DllRS:= TResourceStream.Create(HInstance, 'dllFile', 'dll');
    try
      DllRS.SaveToFile(GetCurrentDir + '\dllFile.dll');
    finally
      DllRS.Free;
    end;
end;
K.MuS
  • 141
  • 1
  • 10
  • 1
    While you don't show the actual code, I'd say it's the usual working directory problem. – Free Consulting Jun 25 '16 at 15:47
  • Defect in your program. Only somebody with the code can fix it. – David Heffernan Jun 25 '16 at 16:19
  • @FreeConsulting the calling process's working directory does not have any effect on `TResourceStream` whatsoever. The only way `TResourceStream` would fail is if the wrong `HInstance` is being passed to its constructor (assuming the correct resource ID/name is being passed). – Remy Lebeau Jun 25 '16 at 17:50
  • @Remy, sure thing, its not possible affect resources this way and the OP actually sees the form resource successfully loaded. Yet the OP claims what transferring data from `.rsrc` section to filesystem fails. So I suspect filesystem issue rather than resource issue. – Free Consulting Jun 25 '16 at 18:06
  • 1
    @FreeConsulting the OP doesn't say anything about the file system being used. – Remy Lebeau Jun 25 '16 at 18:33
  • @RemyLebeau, "application extracts resources" implies some kind of destonation. – Free Consulting Jun 25 '16 at 18:58
  • 1
    @FreeConsulting could be extracting to memory, not a file. – Remy Lebeau Jun 25 '16 at 19:47
  • @RemyLebeau , I'm sure the resources loaded successfully . I added my source code , please review it . – K.MuS Jun 27 '16 at 09:09
  • @DavidHeffernan I edit my question and added the source code. – K.MuS Jun 27 '16 at 09:10
  • A prime example of what a waste of time it is to post first without relevant code, then edit adding fake code and finally, when OP himself finds the reason, post the actual code that exhibits the error. – Tom Brunberg Jun 27 '16 at 12:00

2 Answers2

1

The problem has been resolved, I used GetCurrentDir to get the project2 directory , if I open project2 from preject1 using ShellExecute , GetCurrentDir function get the project1 directory not project2 !!!!

I use ExtractFilePath(Application.ExeName) insted of GetCurrentDir and it's work fine !

SO, the problem not in the TResourceStream , it's in GetCurrentDir !

K.MuS
  • 141
  • 1
  • 10
0

The only plausible explanation that I can conceive is that the first application has loaded the DLL that that the second application is attempting to write. When an executable file (.exe, .dll, .ocx etc.) is loaded then that file is locked and cannot be written to.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490