Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: OuTLawZGoSu on March 11, 2004, 10:35 PM

Title: Whats wrong with this?
Post by: OuTLawZGoSu on March 11, 2004, 10:35 PM
I cant get this to work .



'frmMain
Private Declare Function ShellExecute Lib "SHELL32.DLL" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Const SW_SHOWNORMAL = 1

Private Sub cmdRunProgram_Click()
  Shell frmMain.txtProgramAddress.Text, vbNormalFocus
End Sub


For some reason, when I click cmdRunProgram, an error displays " Invalid Procedure, Call, or Argument " and highlights " Shell frmMain.txtProgramAddress.Text, vbNormalFocus "

I cant get wats wrong.

Help?
Title: Re:Whats wrong with this?
Post by: Grok on March 11, 2004, 11:01 PM
Hmm, works here.  Maybe textbox is empty?  Add:

   If Len(frmMain.txtProgramAddress.Text) = 0 Then Exit Sub

Title: Re:Whats wrong with this?
Post by: OuTLawZGoSu on March 11, 2004, 11:17 PM
Nope, still showing the same thing.
Title: Re:Whats wrong with this?
Post by: o.OV on March 11, 2004, 11:49 PM
I noticed you declared the API function..
Why don't you use it?

Add-On:
What did the variable contain exactly?
When you use the native Shell function..
you should imagine you are in MS-DOS.

For example..
You are running DOS-Prompt.
You would type:

notepad c:\thefiledirectory\readme.txt

notepad readme.txt
(if you are currently in the same directory as file)
Title: Re:Whats wrong with this?
Post by: Flame on March 12, 2004, 03:12 PM
Quote
Private Declare Function ShellExecute Lib "SHELL32.DLL" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Const SW_SHOWNORMAL = 1

Private Sub cmdRunProgram_Click()
  Shell frmMain.txtProgramAddress.Text, vbNormalFocus
End Sub

For some reason, when I click cmdRunProgram, an error displays " Invalid Procedure, Call, or Argument " and highlights " Shell frmMain.txtProgramAddress.Text, vbNormalFocus "

I believe you meant ShellExecute instead of Shell for one, second you're missing a few of the arguments.  I have mine set similar to the code below, and it works fine (I edited it to open what seems to be the file you want to open):
ShellExecute Me.HWnd, vbNullString, frmMain.txtProgramAddress.Text, vbNullString, "C:\", SW_SHOWNORMAL
This code would go into the same form that your button is located, I hope it helps some

Edit: Oops... I forgot about Shell, anyways that is the code to use ShellExecute, if you choose to use it, I've never even used the Shell command, but I suppose you could if you wanted to
Title: Re:Whats wrong with this?
Post by: o.OV on March 12, 2004, 03:30 PM
Quote from: Flame on March 12, 2004, 03:12 PM
ShellExecute Me.HWnd, vbNullString, frmMain.txtProgramAddress.Text, vbNullString, "C:\", SW_SHOWNORMAL

Just an opinion. :)
but if it were me..
I wouldn't predefine the drive.
Title: Re:Whats wrong with this?
Post by: OuTLawZGoSu on March 12, 2004, 09:23 PM
That works, but if the address is invalid, it wont do anything. I need to notify the user that the program isnt there.

Msgbox "The requested program could not be found."
'Just an example.


Help?
Title: Re:Whats wrong with this?
Post by: Spht on March 12, 2004, 09:32 PM
   If Len(Dir(frmMain.txtProgramAddress.Text)) Then
       ' Program exists, launch
   Else
       ' Doesn't exist, show error
   End If
Title: Re:Whats wrong with this?
Post by: Newby on March 12, 2004, 11:19 PM
Quote from: Spht on March 12, 2004, 09:32 PM
   If Len(Dir(frmMain.txtProgramAddress.Text)) Then
       ' Program exists, launch
   Else
       ' Doesn't exist, show error
   End If


Another way.

If Dir$(frmMain.txtProgramAddress.Text) <> vbNullString Then
   'Program exists, gogogo!
Else
   'Program doesn't exist, nonono!
End If
Title: Re:Whats wrong with this?
Post by: o.OV on March 13, 2004, 02:23 AM
I use LenB instead of Len
(A speed difference maybe.. but I use it since code optimize sites suggest it when checking for empty strings.)
and I don't think Dir$ makes a difference
since is a function that returns a string anyways.
ending the function with a "$" is more commonly used with Left, Right, and Mid
(maybe some others)
because they can be used with various datatypes.
Title: Re:Whats wrong with this?
Post by: Grok on March 13, 2004, 04:16 AM
Quote from: Spht on March 12, 2004, 09:32 PM
   If Len(Dir(frmMain.txtProgramAddress.Text)) Then
       ' Program exists, launch
   Else
       ' Doesn't exist, show error
   End If


Bad.  What if user put "calc.exe" in the text box?  If you just use Dir(), you're missing out on the machine's search path.
Title: Re:Whats wrong with this?
Post by: Spht on March 13, 2004, 11:29 AM
I figured program "address" included the full path.
Title: Re:Whats wrong with this?
Post by: Spht on March 13, 2004, 12:31 PM
Quote from: Newby on March 12, 2004, 11:19 PM
Another way.

If Dir$(frmMain.txtProgramAddress.Text) <> vbNullString Then
   'Program exists, gogogo!
Else
   'Program doesn't exist, nonono!
End If


Yes, that is another way, but it requires more typing, so your point is? IIRC, checking length is faster than checking string content anyway.
Title: Re:Whats wrong with this?
Post by: g0dFraY on March 19, 2004, 05:33 PM
Hey, some of you may know that im just starting to learn DLL's and i was reading this post and found:

Private Declare Function ShellExecute Lib "SHELL32.DLL" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Const SW_SHOWNORMAL = 1


is the Alias "ShellExecuteA" needed and if so what does it do, just other functions you may use instead of "ShellExecute" or what?
Title: Re:Whats wrong with this?
Post by: o.OV on March 19, 2004, 07:27 PM
"ShellExecuteA" is the entry point (is needed)

ShellExecute is whatever you want to name the entry point to assuming it isnt a conflicting name.

You should read up on a tutorial..
Google.