【问题标题】:Visual Basic Open URL with Default BrowserVisual Basic 使用默认浏览器打开 URL
【发布时间】:2011-05-30 15:59:47
【问题描述】:

编辑

对于 VB 6

结束编辑

嘿,这似乎应该是一个简单的解决方法,而且我不是特别喜欢 Visual Basic 语言,但是如何使用代码在默认 Web 浏览器中打开 URL?

编辑
为什么我不断收到此错误?

对 PInvoke 函数 'CrackleMail!WindowsApplication1.FormFinal::ShellExecute' 的调用具有 堆栈不平衡。这可能是因为托管的 PInvoke 签名与 非托管目标签名。检查 PInvoke 的调用约定和参数 签名匹配目标非托管签名。

【问题讨论】:

  • 我更新了我的帖子以更正函数签名
  • 看起来 op 使用了已接受答案的 vb6 部分。相应地更新标签。

标签: url vb6 browser


【解决方案1】:
Option Explicit

'Link the kernel method that allows a process to be open/spawn

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

Private Sub mnuAbrirNavegador_Click(Index As Integer)
    OpenUrl("http://www.microsoft.com")
End Sub

Private Sub OpenUrl(ByVal url As String)
    r = ShellExecute(0, "open", url, 0, 0, 1)
End Sub

【讨论】:

    【解决方案2】:

    很简单! 只需使用 Wscript createobject 方法

    CreateObject("Wscript.Shell").Run "www.example.com"
    

    【讨论】:

      【解决方案3】:

      VB.NET:

      System.Diagnostics.Process.Start("http://example.com")
      

      VB 6(不确定):

      Declare Function ShellExecuteA Lib "shell32.dll" ( _
          ByVal hWnd As IntPtr, _
          ByVal lpOperation As String, _
          ByVal lpFile As String, _
          ByVal lpParameters As String, _
          ByVal lpDirectory As String, _
          ByVal nShowCmd As Integer) As IntPtr
      
      ShellExecuteA(Me.Handle, "open", "http://example.com", "", "", 4)
      

      【讨论】:

      • 我认为您不必使用别名。
      • @Shimmy:我在 VB5/6 中所知道的一切我肯定都忘记了,所以请随时编辑我的答案或发布您自己的答案 :)
      • 其实我从来不知道VB6,但是在VB.NET,AFAIK中,当你使用Declare时,在这种情况下你不需要Alias
      • 感谢将 ShellExecute(Me.Handle... 改为 ShellExecuteA(Me.Handle
      • 上面的声明给出了一个编译错误“未定义用户定义的类型”。我已经在另一个可以正常工作的答案中发布了来自 MSDN 的代码
      【解决方案4】:

      接受的答案中的代码给我一个编译错误 我从 MSDN Use ShellExecute to launch the default Web browser 得到了下面的代码

      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
      
      Private Sub Command1_Click()
         Dim r As Long
         r = ShellExecute(0, "open", "http://www.microsoft.com", 0, 0, 1)
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-06
        • 1970-01-01
        • 2011-01-23
        • 2020-05-06
        • 1970-01-01
        • 2020-11-27
        • 1970-01-01
        相关资源
        最近更新 更多