Page 1 of 1
Shellexecute Pchars

Posted:
April 26th, 2009, 6:35 pm
by Blod
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
Re: Shellexecute Pchars

Posted:
April 27th, 2009, 12:54 am
by Kambiz
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);
Re: Shellexecute Pchars

Posted:
April 27th, 2009, 8:07 pm
by Blod
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
Re: Shellexecute Pchars

Posted:
April 28th, 2009, 12:43 am
by Kambiz
Maybe it is because you use OpenDialog instead of OpenDialog.FileName.
Re: Shellexecute Pchars

Posted:
April 28th, 2009, 12:59 pm
by Blod
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
Re: Shellexecute Pchars

Posted:
April 28th, 2009, 1:49 pm
by Kambiz
How do you save the edit1.text in to a file?
Re: Shellexecute Pchars

Posted:
April 28th, 2009, 2:12 pm
by Blod
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
Re: Shellexecute Pchars

Posted:
April 28th, 2009, 5:27 pm
by Kambiz
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.
Re: Shellexecute Pchars

Posted:
April 29th, 2009, 7:39 am
by Blod
Program Attached
Blod
example.zip
- Delphi example
- (173.21 KiB) Downloaded 79 times
Re: Shellexecute Pchars

Posted:
April 29th, 2009, 1:42 pm
by Kambiz
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);
Re: Shellexecute Pchars

Posted:
April 29th, 2009, 5:11 pm
by Blod
Thanks,
I only ended up using oncanclose in desperation because I had tried everything else without success.
Blod