Software Builder
Software Builder

Reputation: 203

How to get the Browser UserAgent String in Visual Basic 6?

I am trying to get the UserAgent of the default browser using the ObtainUserAgentString API in Visual Basic 6. I found the documentation on the MSDN and tried to convert it to Visual Basic 6 but it did not work.

C++ (MSDN)

HRESULT ObtainUserAgentString(
  _In_  DWORD  dwOption = 0,
  _Out_ LPCSTR *pcszUAOut,
  _Out_ DWORD  *cbSize
);

Visual Basic 6 API

Private Declare Function ObtainUserAgentString Lib "Urlmon.dll" (ByVal dwOption As Long, ByRef pcszUAOut As String, ByRef cbSize As Long) As Long

Private Function BrowserUserAgent() As String
Dim httpUseragent As String
Dim szhttpUserAgent As Long
httpUseragent = Space(512)
szhttpUserAgent = Len(httpUseragent)
Call ObtainUserAgentString(0, httpUseragent, szhttpUserAgent)
BrowserUserAgent = httpUseragent
End Function

Private Sub Command1_Click()
MsgBox BrowserUserAgent
End Sub

Upvotes: 2

Views: 928

Answers (2)

Bob77
Bob77

Reputation: 13267

Aside from the fact this is a cruddy old ANSI entrypoint, everything you need appears to be documented.

Option Explicit

Private Const NOERROR As Long = 0
Private Const E_OUTOFMEMORY As Long = &H8007000E

Private Enum UAS_OPTIONSENUM
    [_UAS_EXACTLEGACY] = &H1000&
    UAS_DEFAULT = 0
    UAS_7 = 7 'Compatible mode.
    UAS_7_LEGACY = 7 Or [_UAS_EXACTLEGACY]
    UAS_8 = 8
    UAS_9 = 9
    UAS_10 = 10
    UAS_11 = 11
End Enum

Private Declare Function ObtainUserAgentString Lib "urlmon" ( _
    ByVal dwOption As Long, _
    ByVal pcszUAOut As Long, _
    ByRef cbSize As Long) As Long

Private Function BrowserUserAgent( _
    Optional ByVal Options As UAS_OPTIONSENUM = UAS_DEFAULT) As String
    Const MAX_BUFFER As Long = 2048
    Dim Size As Long
    Dim Buffer() As Byte
    Dim HRESULT As Long

    Do
        Size = Size + 128
        ReDim Buffer(Size - 1)
        HRESULT = ObtainUserAgentString(Options, VarPtr(Buffer(0)), Size)
    Loop While HRESULT = E_OUTOFMEMORY And Size < MAX_BUFFER
    If HRESULT = NOERROR Then
        BrowserUserAgent = StrConv(LeftB$(Buffer, Size - 1), vbUnicode)
    Else
        Err.Raise &H8004D000, _
                  , _
                  "ObtainUserAgentString error &H" & Hex$(HRESULT)
    End If
End Function

Private Sub Form_Load()
    AutoRedraw = True
    Print BrowserUserAgent()
    Print BrowserUserAgent(UAS_7)
    Print BrowserUserAgent(UAS_7_LEGACY)
    Print BrowserUserAgent(UAS_8)
    Print BrowserUserAgent(UAS_11)
End Sub

Screenshot

Upvotes: 4

user6017774
user6017774

Reputation:

HRESULT ObtainUserAgentString(
  _In_  DWORD  dwOption = 0,
  _Out_ LPCSTR *pcszUAOut,
  _Out_ DWORD  *cbSize
);

Param 2 is LongPointerCString. You always pass C strings ByVal which in reality passes the C string part of the B String ByRef. If it was a IN param you would have to end the string with a Chr(0) which is what real C strings have.

String arguments are a special case. Passing a string by value means you are passing the address of the first data byte in the string; passing a string by reference means you are passing the memory address where another address is stored; the second address actually refers to the first data byte of the string. How you determine which approach to use is explained in the topic "Passing Strings to a DLL Procedure" later in this chapter.

From Visual Basic Concepts in Help.

Upvotes: 2

Related Questions