-------------------------------------------------------------------------------
mlSession = WinHttpOpen(scUserAgent, _
WINHTTP_ACCESS_TYPE_NO_PROXY, _
vbNullString, _
vbNullString, _
0)
If mlSession = 0 Then
' ERROR Can't open Session
Err.Raise -1, "", "Error code: " & CStr(Err.LastDllError)
Else
mlConnection = WinHttpConnect(mlSession, _
"localhost", _
80, _
0)
If mlConnection = 0 Then
' ERROR Can't open Connection
Err.Raise -1, "", "Error code: " & CStr(Err.LastDllError)
End If
End If
-------------------------------------------------------------------------------
The WinHttpOpen function successfully returns a session handle but the
WinHttpConnect function fails, returning 0 (=mlConnection)
The error code is 12005, which is ERROR_WINHTTP_INVALID_URL.
Could anyone tell me what could be causing this problem?
For your reference, the API functions are declared as following
Public Declare Function WinHttpOpen Lib "winhttp5.dll" _
(ByVal pwszUserAgent As String, _
ByVal dwAccessType As Long, _
ByVal pwszProxyName As String, _
ByVal pwszProxyBypass As String, _
ByVal dwFlags As Long) As Long
Public Declare Function WinHttpConnect Lib "winhttp5.dll" _
(ByVal hSession As Long, _
ByVal pswzServerName As String, _
ByVal nServerPort As Integer, _
ByVal dwReserved As Long) As Long
Thanks.
The cause of the "Invalid URL" error is due to an interoperability issue
between Visual Basic and Unicode Win32 APIs. Fortunately, there is a
workaround.
In Visual Basic, variables of the String type are stored internally in the
Unicode format. However, when VB passes a String variable as a parameter to
a Win32 API function, it converts the Unicode string to ANSI. It does this
in order to support the ANSI-based Windows 9x platforms. VB assumes that a
Win32 API provides both ANSI and Unicode versions, and assumes that it is
calling the ANSI version. Many Win32 APIs do provide dual versions; for
example, WinINet implements Unicode and ANSI versions of InternetConnect:
InternetConnectW and InternetConnectA, respectively.
However, WinHTTP's Win32 API is Unicode only, since it runs only on Windows
NT/2000/XP-based platforms, which are Unicode-native. Therefore, WinHTTP
expects to receive a Unicode string. The "Invalid URL" error occurs because
the ANSI ServerName string from VB appears to WinHttpConnect as a malformed
Unicode string.
There is a way to "trick" Visual Basic into passing the string as Unicode,
using the undocumented StrPtr function. Note that StrPtr is available only
in Visual Basic 6 (and 5); it is not supported in VB.NET. (This trick is not
needed with VB.NET, as it supports passing strings in Unicode format to a
Win32 API.)
A String parameter to a WinHTTP Win32 function should be declared as type
Long instead. So your WinHttpConnect declaration should look like this:
Public Declare Function WinHttpConnect Lib "winhttp5.dll" _
(ByVal hSession As Long, _
ByVal pswzServerName As Long, _
ByVal nServerPort As Integer, _
ByVal dwReserved As Long) As Long
And when you call the Win32 API, use the StrPtr to pass the string as
Unicode:
mlConnection = WinHttpConnect(mlSession, _
StrPtr("localhost"), _
80, _
0)
Here are some links to additional information:
http://www.mvps.org/vb/index2.html?tips/varptr.htm
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnovba00/html/LightningStrings.asp
Hope that helps.
Regards,
Stephen Sulzer
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
"Phillip Suh" <phill...@qcom.com.au> wrote in message
news:855c9aaf.03040...@posting.google.com...
"Stephen Sulzer [MSFT]" <ssu...@online.microsoft.com> wrote in message news:<#ZFxE5z#CHA....@TK2MSFTNGP11.phx.gbl>...