tags:

views:

138

answers:

4

I want to create and then open a txt file using the ShellExecute command.

I have used this code for years with Delphi 7 and it worked:

function Executa(CONST ExeName, Parameters: string): Boolean;
begin
 if Parameters= ''
 then Result:= ShellExecute(0, 'open', PChar(ExeName), NIL              , nil, SW_SHOWNORMAL)> 32
 else Result:= ShellExecute(0, 'open', PChar(ExeName), PChar(Parameters), nil, SW_SHOWNORMAL)> 32;
end;

Now, I switch to Windows 7 (don't like it; they stuck it on my throat with my new laptop) 64 bit and the code is not working anymore when it runs from IDE. Delhi shows the CPU window with the caption "CPU-Process unknown (2352)". I close the CU windows and everything works fine until I close the application, when Delphi shows the CPU window one more time. If I run the app from outside IDE, it works fine.

Looks like the debugger has something to say to me, but I don't know what.

+1  A: 

I had a problem yesterday with the debugger crashing my application, but running it outside the IDE it would run fine. I was using packages in my development.

I used process explorer to verify I found I was loading a copy from another location than expected. I had two copies of the same BPL floating around. Once I removed the one I was not compiling I was fine.

Applying that to this problem, I would check to make sure you don't have any copies of compiled code that includes: .DCU, .DCP, .BPL, .EXE around. Then I would also make sure you you can ctrl-click on "ShellExecute" to and see the declaration. You may have your library path setup in a way that it can't find the source.

Robert Love
This may be my case. I will check. Thanks!
Altar
+4  A: 

Sounds to me like you have the "debug spawned processes" option turned on. When that's enabled, the debugger interrupts the new process at the earliest possible time. Press the "run" button to let it continue running.

You can confirm this hypothesis the next time you debug your program. Compare the process ID (2352, in your example) with the list of processes shown by Task Manager. Which process in that list matches the process ID reported by the debugger?

Rob Kennedy
That was it! But I had that option checked for years! With worked with Win 98 and Win XP! Isn't better to leave this ON?1+ and marked as "accepted". Many thanks.
Altar
Obviously, it's only better to leave it on if you're going to debug the spawned process. Is that what you want to do? If you don't have the source code or any debug information, there's probably not much you'll be able to do, so you may as well leave it turned off so you don't get interrupted with each new process.
Rob Kennedy
+1  A: 

Shot in the dark here, but try running the IDE as administrator, and then not as administrator. That may be a factor. Some users make a shortcut with the administrator option set, so that the auto-update runs successfully. So you may be running the IDE as admin, if you've done that.

Chris Thornton
+2  A: 

This is not the answer for your question (I vote for Rob Kennedy & Chris Thornton), but you can write your routine in a more compact way:

function Executa(const ExeName, Parameters: string): Boolean;
begin
  Result := 
    (ShellExecute(0, 'open', PChar(ExeName), Pointer(Parameters), nil, SW_SHOWNORMAL) > 32);
end;

Note Pointer() instead of PChar() for 4th argument. This is a documented behaviour of PChar/Pointer casts (see help).

Alexander
Thanks! I wanted to do that but I was not sure it will work. Because of the lack of time I chose the safe way. Many thanks again!! 1+
Altar