Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using ShellExecute() - Not working quite right 1

Status
Not open for further replies.

subzero349

Technical User
Oct 21, 2003
16
0
0
CA
Hi,

I'm using the following code to execute a dos executable (FullPath) with some arguments and switches (ActiveFileNameAndSwitches):

"ShellExecute(Handle,'open',Pchar(FullPath),Pchar(ActiveFileNameAndSwitches), nil,SW_SHOWNORMAL);"

The code does work, and it does execute the dos program. BUT my problem is this:

I have code above this that sets the PATH in the registry. I also have code that updates windows with the new path immediately (tested and working). The ShellExecute() does not seem to take the new reg changes though, it only uses the original settings. I know windows is updating immediately because I can open the command prompt (start->run->cmd) and with a few tests I can easily see that the PATH is changing right away.

So... why is this happening? Do i have any other options to run code in a dos window? do I have to close the cmd when it's finished? do I have to "create" a process?

Please help! I have no idea why it's not working!
 
Here is a tip:
Go to this address in your computer:

<Delphi folder>\demos\doc\filmanex

There ou will find the unit: Fmxutils

In youre application call Executefile (After adding the fmxutils unit to youre uses list)
 
Can anyone offer a little more information on the various ways to execute a program while within a delphi program?

I trid putting fmxutils in the use list, but I get this error:

&quot;[Fatal Error] Main.pas(8): File not found: 'fmxutils.dcu'&quot;

I have the folder filnamex...
 
Is this the function for Execute file:

&quot;function TForm.ExecuteFile(const FileName, Params, DefaultDir: string; ShowCmd: Integer): THandle;
var
zFileName, zParams, zDir: array[0..79] of Char;
begin
Result := ShellExecute(Application.MainForm.Handle, nil,
StrPCopy(zFileName, FileName), StrPCopy(zParams, Params),
StrPCopy(zDir, DefaultDir), ShowCmd);
end;&quot;

If so... It looks like it just uses ShellExecute anyways.... how is this different from what I was doing before??
 
No,

&quot;[Fatal Error] Main.pas(8): File not found: 'fmxutils.dcu'&quot;

But it's ok... I think I've found a way around this problem.

Thank you for your help
 
move fnxutils to your source dir or change the delphi dcupath to include it's path.

HTH
TonHu
 
Just to expand on what TonHu said, the 'File not found...' message means that the compiler cannot find the file it needs in this case fmxutils.dcu. If you click the Tools|Environment Options menu you will see a Library tab, and one of the settings on this tab is the Library path. This is a list of directories that Delphi will use when searching for source files. Alternatively if you only want this particular project to use a directory, then rather than adding it to the Library path you can add it to the Search Path on the Directories/Conditionals tab under the Project|Options menu.
And going back to the original problem, are FullPath and ActiveFileNameAndSwitches variables, or did you just not want to type the whole lot into your question? If they are variables, then make sure that FullPath contains the updated value before it is used in ShellExecute.

Steve
 
Thank you for the answers.

Donvittorio:
FullPath and ActiveFileNameAndSwitches are indeed variables. And I have made sure that they contain the most up to date values.

I am still using:
&quot;ShellExecute(Handle,'open',Pchar(FullPath),Pchar(ActiveFileNameAndSwitches), nil,SW_SHOWNORMAL);&quot;

My problem was that I needed to update the Delphi environment variable PATH instead of the windows environment variable PATH. So that problem is solved.

I have another question you might be able to help me with:

The FullPath is a path to an executable. I want to run the executable, wait for it to finish, receive an &quot;error code&quot; from it, and check the error code. Is this possible with shellexecute?

I know the exe I'm running returns values when it's done, but I don't know how to receive them. Please help!

 
MORE help required:

I use:
SetEnvironmentVariable(PChar('PATH'),PChar(RegistryPathChange));

to write to the registry. this works great within the delphi &quot;run&quot;

but once I have an exe file will this still work? will I be able to use this on w2k and win98?

Please help me :(
 
Take a look at the following document:


It tells you lots of stuff about running exes from Delphi, and more to the point it has a ShellExecute wrapper function called ShellExec which includes a Wait parameter, which you can use to wait for the exe to finish before carrying on. It also returns the error code, so it should be exactly what you need.

I don't know anything about SetEnvironmentVariable, but it shouldn't matter whether you run your program inside or outside of Delphi, the result will be the same.

Steve
 
Thanks, I'll look at the link.
The SetEnvironmentVariable issue has been sorted out (I hope)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top