Delphi Pages Forums

Delphi Pages Forums (http://www.delphipages.com/forum/index.php)
-   General (http://www.delphipages.com/forum/forumdisplay.php?f=2)
-   -   VB To Delphi Help! (http://www.delphipages.com/forum/showthread.php?t=182339)

Simon180 04-21-2007 05:29 AM

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

paulholden 04-21-2007 06:27 AM

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

digitiger 04-21-2007 10:35 PM

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

MrBaseball34 04-22-2007 06:17 AM

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!
http://www.austinmetrobaseball.com/texas.gif
2005 College Football National Champions
2005 College Baseball National Champions


All times are GMT. The time now is 06:54 PM.

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