Solved

cannot run external program with re-direction parameter ">logfile.txt"

Posted on 2003-11-25
14
1,189 Views
Last Modified: 2010-04-16
Hi Guys,

I'm trying to call gfix.exe(Interbase utility) from Delphi application to shut down my database(I need to backup it afterwards). To get results of running gfix.exe I use syntax like this:

run := 'C:\progra~1\firebird\firebi~1\bin\gfix.exe -shut -force 30 c:\totalpak\data\newfarm.gdb -user duduser -pass fakepassword 2> c:\errout.tm';
RunAndWait32(run, sw_hide);

RunAndWait32 is a quite common procedure using CreateProcess and WaitForSingleObject.

It doesn't work this way and does not redirect gfix output to errout.tm.

If I put RUN string into bat file and then do: RunAndWait32('mybatfile.bat', sw_hide) it doesn't work either(does not create errout.tm file). But if I run this bath file from within windows environment it works just fine.

Anyone has any clues ?

Cheers,
Dmitry
0
Comment
Question by:Trekker72
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 5

Expert Comment

by:snehanshu
ID: 9822629
Can you provide your code of RunAndWait32?
...Shu
0
 
LVL 2

Author Comment

by:Trekker72
ID: 9822699
Here it is:

function WinExecAndWait32(Path: PChar; Visibility: Word;
  Timeout : DWORD): integer;
var
  WaitResult : integer;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  iResult : integer;
begin
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  with StartupInfo do
  begin
    cb := SizeOf(TStartupInfo);
    dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
      { you could pass sw_show or sw_hide as parameter: }
    wShowWindow := visibility;
  end;
  if CreateProcess(nil,path,nil, nil, False,
            NORMAL_PRIORITY_CLASS, nil, nil,
            StartupInfo, ProcessInfo) then
  begin
    WaitResult := WaitForSingleObject(ProcessInfo.hProcess, timeout);
    { timeout is in miliseconds or INFINITE if
        you want to wait forever }
    result := WaitResult;
  end
  else
  { error occurs during CreateProcess see help for details }
    result:=GetLastError;
end;
0
 
LVL 2

Author Comment

by:Trekker72
ID: 9822724
I invoke RunAndWait32(PChar(run), sw_hide, 50000);

Cheers
Dmity aka Terry

0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 5

Expert Comment

by:snehanshu
ID: 9822767
Well WinExecAndWait32 looks fine and I have no clue then.
Perhaps something to do with how gfix.exe works, which I don't have.
I hope some experts see this Q and help.
Good luck,
...Shu
0
 
LVL 2

Author Comment

by:Trekker72
ID: 9822794
I did not mention that my application is a service one. So I invoke RunAndWait32 from TService, if it does make any difference.

Thanks Shu, anyway

Cheers
0
 
LVL 5

Expert Comment

by:snehanshu
ID: 9822875
Trekker72,
Well then, here are a few guesses:
I trued writing a simple app where I created a batch file gfix.bat containing the following:

@echo %1 %2 %3 %4 %5 %6 %7 %8 %9
@dir c:\*.*
then, in my application, I added the following code:

procedure TForm1.Button1Click(Sender: TObject);
Var
  Run: String;
begin
  run := 'C:\progra~1\firebird\firebi~1\bin\gfix.bat -shut -force 30 c:\totalpak\data\newfarm.gdb -user duduser -pass fakepassword 2 > c:\errout.txt';
  ShowMessage(format('Error = %d',[WinExecAndWait32(pchar(run),sw_show, 5000)]));
end;

Then, I did get a text file "c:\errout.txt" as expected.

You could also try this replacing the batch with the exe in a simple app and see whether you get the output file. If you don't, then the problem is perhaps with the way gfix.exe works.

If you do, then check that your service's interactive property is set to true (To allow it to interact with the desktop).
These are just guesses, I hope they help.
...Shu
0
 
LVL 2

Author Comment

by:Trekker72
ID: 9822970
Thanks Shu,

I think you're right it is all about the way gfix.exe works.
Now I get it working(I used some other execandwait function) but it always says "unavailable database".
I get the same error message if I run gbak.exe.
If I run it as normal GUI application it works fine. I set service's interactive property to true but it did not help.

Cheers
0
 
LVL 5

Assisted Solution

by:snehanshu
snehanshu earned 125 total points
ID: 9823176
>>If I run it as normal GUI application it works fine. I set service's interactive property to true but it did not help.
You mean the same code works in a normal app but not from a service?
Perhaps you could try to manually check the "allow service to interact with desktop" checkbox in your service's properties (logon, from service manager) or try uninstalling and reinstalling the service after compiling it with the interactive property set t true.
...Shu
0
 
LVL 17

Accepted Solution

by:
geobul earned 125 total points
ID: 9823245
Hi,

Try setting your service to logon using your account instead of the default 'Local System Account'. Stop it first, open its properties, change the logon account and start it again.

The problem is in access rights (security issue) I think.

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 9823268
BTW. I use this:

    GetExitCodeProcess(ProcessInfo.hProcess, Result);
    CloseHandle(ProcessInfo.hProcess); // you must close these two handles
    CloseHandle(ProcessInfo.hThread);

instead of your

result := WaitResult;

in order to obtain the result code of the executed process.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9823376
I'm not sure, but try to set CREATE_NEW_CONSOLE flag:
dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK or CREATE_NEW_CONSOLE;
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9823518
If the flag will not help you need to redirect child process streams.
Ho to do that read here:
http://oldlook.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20610325.html
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9823586
0
 
LVL 2

Author Comment

by:Trekker72
ID: 9828418
Guys,

problem solved by putting localhost: in front of db name like this: gbak -b localhost:c:\mydb.gdb c:\mydb.gbk
As I mentioned before, gbak worked fine if I run it without localhost: from GUI. For Service application don't forget about localhost: !!!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

770 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