Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 04-21-2007, 05:29 AM
Simon180 Simon180 is offline
Senior Member
 
Join Date: Dec 2003
Posts: 187
Default VB To Delphi Help!

I been trying to convert abit of code from VB to delphi with no luck so am wondering if anyone can help me?

Code:
Private Sub cmdOpen_Click()

On Error GoTo errhandle
Shell ("C:\Program Files\Silkroad\sro_client.exe " & txtNumber.Text & " /18 0 0")

errhandle:

newpath = InputBox("I'm sorry, sro_client.exe not found on default location, please specify another location:", "Error!", "C:\Program Files\Silkroad\sro_client.exe")

Shell (newpath & " " & txtNumber.Text & " /18 0 0")

End Sub
and here my delphi convert

procedure TForm1.cmdOpenClick(Sender: TObject);
var
newpath: String;
Label errhandle;
begin
try

Shell( 'C:\Program Files\Silkroad\sro_client.exe '+txtNumber.text+' /18 0 0');

except

newpath := InputBox( 'I ''m sorry, sro_client.exe not found on default location, please specify another location:' , 'Error!' , 'C:\Program Files\Silkroad\sro_client.exe' );

Shell( newpath+' '+txtNumber.text+' /18 0 0');

end;
end;
[/code]


my problem is I can not work out how to get the Shell funcations working like the vb code can anyone help please?thanks alot
Reply With Quote
  #2  
Old 04-21-2007, 06:27 AM
paulholden paulholden is offline
Senior Member
 
Join Date: Jan 2001
Posts: 714
Default RE: VB To Delphi Help!

You can use the WinExec function like so;
Code:
WinExec(PChar(newpath), SW_SHOW);
Your try..except statement won't work as you're intending either. You should use the FileExists function to check whether the executable exists before trying to run it.

[sup]Hope that helps, please 'Accept as Answer' it if does[/sup] :^D
Reply With Quote
  #3  
Old 04-21-2007, 10:35 PM
digitiger digitiger is offline
Senior Member
 
Join Date: Jan 2001
Posts: 716
Default RE: VB To Delphi Help!

if the only problem you are expecting is missing file then why not check for the existence of file before executing it.

you can do it as follows

........
.......
if FileExists(newpath) then
WinExec(PChar(newpath), SW_SHOW);
else
input..........


Alternatively, you can react based on the result of WinExec function as below

........
.......
if WinExec(PChar(newpath), SW_SHOW) < 31 then
input..........

you may note that the if result of WinExec function is less then 31 then it relates to unavailability of executable for one reason or the other for execution.

Hope it helps and if it does then do accept the answere

hope it helps
Reply With Quote
  #4  
Old 04-22-2007, 06:17 AM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default RE: VB To Delphi Help!

Furst off, let me say that I would NEVER suggest using a
depricated function such as WinExec. This is the proper
Delphi way of doing this:

[delphi]
uses ..., ShellApi;

procedure TForm1.BtnOpen_Click(Sender: TObject);
vat
sei: TShellExecuteInfo;
SFileName: String;
newpath: String;
begin
ZeroMemory(@sei, sizeof(sei));
SetLength(sSysDir, MAX_PATH);
SFileName := 'C:\Program Files\Silkroad\sro_client.exe';

// Check if the file exists
if not FileExists(sFileName) then
begin
// tell them it has not been found
if MessageBox(0, 'I''m sorry, sro_client.exe not '+
'found on default location, please '+
'specify another location.',
'Error!',
MB_ICONERROR or MB_OK) = mrOK then
begin
// set the initialdir of the OpenFileDialog
OpenDialog1.InitialDir := C:\Program Files\Silkroad;
// if they select it, then set sFileName to the
// file returned from the dialog
if OpenDialog1.Execute then
sFileName := OpenDialog1.FileName;
end;
end;

// use ShellExecuteEX to run it, first fill
// the TShellExecuteInfo structure

with sei do
begin
cbSize := SizeOf(sei);
fMask := SEE_MASK_NOCLOSEPROCESS;
Wnd := Form1.Handle;
lpVerb := 'open';
lpFile := PChar('SFileName');
lpParameters := PChar(edtNumber.Text + '/18 0 0');
lpDirectory := PChar(ExtractFilePath(sFileName));
nShow := SW_SHOWNORMAL;
end;
ShellExecuteEX(@sei);
end;
[/delphi]

MrBaseball34
Hook'Em Horns!

2005 College Football National Champions
2005 College Baseball National Champions
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 02:14 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.