Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / COM / January 2004



Tip: Looking for answers? Try searching our database.

Problem with VB's Shell command (on Win2K only ) ???

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Setya Nugraha Djajadinata - 20 Jan 2004 12:01 GMT
Hi all,

I need to execute Notepad.exe with the following routine :

Public Const HKEY_LOCAL_MACHINE = &H80000002

Dim lHandleKey As Long
Dim sResult As String * 256

lHandleKey = 256

Call RegOpenKey(HKEY_LOCAL_MACHINE,
"SOFTWARE\Classes\txtfile\shell\open\command",
lHandleKey)
Call RegQueryValueEx(lHandleKey, "", 0, 0, sResult, 256)

If Len(Trim(sResult)) > 0 Then
 Call Shell(s_fctGetAPIResult(sResult, "%1")
 & sFileName, vbNormalFocus)        
 Exit Sub
End If

Public Function s_fctGetAPIResult(sApiResult As String, Optional sToFind
As String)
   Dim lPos As Long
   
   If Len(Trim(sToFind)) < 1 Then sToFind = Chr(0)
   lPos = InStr(1, sApiResult, sToFind, 1)

   s_fctGetAPIResult = IIf(lPos > 0,
   Left(sApiResult,lPos - 1), "")
End Function

The above code runs well on Win98 but when I try to run them on Win2K it
always throws message "File Not Found". I also try ShellExecute API with
the same result.

If I try with absolute path it runs well on both platforms.

Any ideas ?

Thanks in advance

Setya
Mark Alexander Bertenshaw - 22 Jan 2004 11:18 GMT
> The [...] code runs well on Win98 but when I try to run them on Win2K
> it always throws message "File Not Found". I also try ShellExecute
[quoted text clipped - 7 lines]
>
> Setya

Setya -

Did you try the obvious thing, and stepped over each line of your code in
the IDE?  For instance, what is the value of sResult, or
s_fctGetAPIResult(sResult) for that matter?  This value is obviously the one
which is wrong.  Now work back to find the real problem.

Having given you that bit of advice, I can't give you the answer (there are
far too many unknowns), however I can comment on your code style and
useages.
This looks like code that has been crudely ported from Win16 i.e. Windows
3.1.  There are things that you should change anyway:

> Public Const HKEY_LOCAL_MACHINE = &H80000002
>
> Dim lHandleKey As Long
> Dim sResult As String * 256

* Don't use: Dim sResult As String * 256.  Instead, declare this as a normal
string, and then do:
 sResult = Space$(256).
 You should only use Fixed length strings inside a User Defined Type.

> lHandleKey = 256

* What's the point of setting this to 256?  This value will be written over
by the next function without reading this.

> Call RegOpenKey(HKEY_LOCAL_MACHINE,
> "SOFTWARE\Classes\txtfile\shell\open\command",
> lHandleKey)

* Don't use: RegOpenKey(), use RegOpenKeyEx().  The documentation says that
RegOpenKey() is only there for backwards compatibility.
* Don't use: *Call* RegOpenKey().  Both open key versions return a Long
value which you should examine.  Only if it is ERROR_SUCCESS, should you
continue with the next line.

> Call RegQueryValueEx(lHandleKey, "", 0, 0, sResult, 256)

* You should also examine the return value of RegQueryValueEx(), and only
continue if it returns ERROR_SUCCESS.
* The last parameter should be a Long variable, rather than a constant,
because it rather helpfully returns the numbers of characters copied into
sResult.  This
way, you can simply use Left$(lLenResult - 1) to give you the string you
want, rather than using your s_fctGetAPIResult() function.
* Minor quibble: in this case, you are better off using the constant
vbNullString, rather than "" for the key name.

> If Len(Trim(sResult)) > 0 Then

* You shouldn't be using this test to determine whether the functions
succeeded.  RegQueryValueEx() has helpfully given you a return code.. Use
it.

>   Call Shell(s_fctGetAPIResult(sResult, "%1")
>   & sFileName, vbNormalFocus)
[quoted text clipped - 3 lines]
> Public Function s_fctGetAPIResult(sApiResult As String, Optional
> sToFind As String)

* I know that I have said that you shouldn't need to use this routine,
however I ought to point out that you should type the return value of the
value to be "As String", otherwise it treats it converts your string to a
variant.

>     Dim lPos As Long
>
>     If Len(Trim(sToFind)) < 1 Then sToFind = Chr(0)

* Likewise, in future use Trim$() rather than Trim(), because it converts
the string to a Variant.
* Actually, I have just noticed that you can effectively combine the
previous line with the function header to get:
  Public Function s_fctGetAPIResult(sApiResult As String, Optional sToFind
As String = vbNullChar)

>     lPos = InStr(1, sApiResult, sToFind, 1)
>
>     s_fctGetAPIResult = IIf(lPos > 0,
>     Left(sApiResult,lPos - 1), "")
> End Function

Finally, there is a small possibility that your declarations for the API
calls might be slightly iffy.

> *** Sent via Developersdex http://www.developersdex.com ***

Just interested, but what's the deal with this Developersdex?  I find it
slightly annoying that they often email me when I get something answered on
a tech newsgroup.

> Don't just participate in USENET...get rewarded for it!

Crikey!  What have I been missing?  Personally, using Newsgroup s/w seems a
far more efficient way of accessing USENET.  I don't use DevX any more for
exactly that reason.

--
Mark Bertenshaw
Kingston upon Thames
UK
Setya Nugraha Djajadinata - 26 Jan 2004 03:44 GMT
Thanks for the reply,

Actually I only inherit the code, it was written by programmer before
me. But I'll try to fix it to conform your advice.

Best Regards,

Setya
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.