Shellexecute Pchars

Please discuss general Delphi programming topics here.

Shellexecute Pchars

Postby Blod » April 26th, 2009, 6:35 pm

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
Junior Member
 
Posts: 33
Joined: June 5th, 2008, 12:54 pm

Re: Shellexecute Pchars

Postby Kambiz » April 27th, 2009, 12:54 am

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
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Shellexecute Pchars

Postby Blod » April 27th, 2009, 8:07 pm

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
Junior Member
 
Posts: 33
Joined: June 5th, 2008, 12:54 pm

Re: Shellexecute Pchars

Postby Kambiz » April 28th, 2009, 12:43 am

Maybe it is because you use OpenDialog instead of OpenDialog.FileName.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Shellexecute Pchars

Postby Blod » April 28th, 2009, 12:59 pm

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
Junior Member
 
Posts: 33
Joined: June 5th, 2008, 12:54 pm

Re: Shellexecute Pchars

Postby Kambiz » April 28th, 2009, 1:49 pm

How do you save the edit1.text in to a file?
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Shellexecute Pchars

Postby Blod » April 28th, 2009, 2:12 pm

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
Junior Member
 
Posts: 33
Joined: June 5th, 2008, 12:54 pm

Re: Shellexecute Pchars

Postby Kambiz » April 28th, 2009, 5:27 pm

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
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Shellexecute Pchars

Postby Blod » April 29th, 2009, 7:39 am

Program Attached

Blod
example.zip
Delphi example
(173.21 KiB) Downloaded 101 times
Blod
Junior Member
Junior Member
 
Posts: 33
Joined: June 5th, 2008, 12:54 pm

Re: Shellexecute Pchars

Postby Kambiz » April 29th, 2009, 1:42 pm

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
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Shellexecute Pchars

Postby Blod » April 29th, 2009, 5:11 pm

Thanks,

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

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


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 15 guests

cron