## Shellexecute Pchars

Please discuss general Delphi programming topics here.

### Shellexecute Pchars

Hello,

The user of my program will enter information into an edit box.
How can I copy this edit box.text into a Pchar variable that I can then use in the Shellexecute function?
I've tried a few ways and keep getting errors.

Thanks

Blod
Blod
Junior Member

Posts: 33
Joined: June 5th, 2008, 12:54 pm

### Re: Shellexecute Pchars

When a function/procedure expects a PChar as parameter, just typecast the string argument as PChar.

For example:

Code: Select all
ShellExecute(0, 'open', PChar(UserCommandEditBox.Text), nil, nil, SW_NORMAL);
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: Shellexecute Pchars

Thanks Kambiz,

It was simpler than I thought.
Can you help with this as well?

When I programatically code something like...

Edit1.text:=extractfilename(opendialog);

Edit1.text contains something like MYPROG.EXE but I'm unable to save that information to file.
If I actually type myprog.exe into the edit box then I am able to save this to file.
What is the reason for this?

Blod
Blod
Junior Member

Posts: 33
Joined: June 5th, 2008, 12:54 pm

### Re: Shellexecute Pchars

Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: Shellexecute Pchars

This is the code that I actually use...

edit1.text:=extractfilename(opendialog1.filename);

It does put the name of the chosen file into the editbox.text as required.
However, when I try to save the editbox.text to file it fails to do so.
Although, if I physically type the filename into the editbox then it is saved for persistant use.

Blod
Blod
Junior Member

Posts: 33
Joined: June 5th, 2008, 12:54 pm

### Re: Shellexecute Pchars

How do you save the edit1.text in to a file?
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: Shellexecute Pchars

If I only have one or two items to save to file then I normally use an invisible memo in my program
and do something like...

Memo1.lines[0]:=edit1.text;
memo1.lines.savetofile('Myfile.txt');

I know that this isn't the approved way of doing things but it generally works ok.

Blod
Blod
Junior Member

Posts: 33
Joined: June 5th, 2008, 12:54 pm

### Re: Shellexecute Pchars

All codes seems to be fine, but for sure there is a mistake somewhere.

If you attach a sample program, it would be much easier to trace the problem.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: Shellexecute Pchars

Program Attached

Blod
example.zip
Delphi example
Blod
Junior Member

Posts: 33
Joined: June 5th, 2008, 12:54 pm

### Re: Shellexecute Pchars

You only need to specify full path to your storage file, because OpenDialog changes the current working directory.

Code: Select all
memo1.lines.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'edit.txt');memo1.lines.SaveToFile(ExtractFilePath(ParamStr(0)) + 'edit.txt');

In addition, using OnCanClose event of OpenDialog is not the correct way to get filename. Use this instead:

Code: Select all
if OpenDialog1.Execute then  path1.text := ExtractFilepath(OpenDialog1.filename);
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: Shellexecute Pchars

Thanks,

I only ended up using oncanclose in desperation because I had tried everything else without success.

Blod
Blod
Junior Member

Posts: 33
Joined: June 5th, 2008, 12:54 pm