InternetCheckConnection in VB6 returning false - vb6

I'm trying to come up with a VB6 test app that calls InternetCheckConnection.
In my test app, InternetCheckConnection always returns false regardless of the URL I use. I copied and pasted this code from a larger spaghetti-code app, but in the spaghetti-code, InternetCheckConnection seems to work fine, returns true.
Is there some other function I have to call first in order for InternetCheckConnection to work?

I was able to get InternetCheckConnection to work correctly by including the full address for a website, including "http://". For example, using "www.google.com" returned False, but "http://www.google.com" returned true.

Try using the InternetGetConnectedState function.
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpSFlags As Long, ByVal dwReserved As Long) As Long
Dim blnInternetConnected as Boolean
Dim Flags as long
blnInternetConnected = InternetGetConnectedState(Flags, 0&)

Perhaps, in the "spaghetti-code" app, InternetCheckConnection is relying on the state of some global variable you are not aware of. Is that possible?

Related

OCIBreak analog in managed ODP.NET

I'm trying to migrate to managed ODP.NET, but I'm having problems with the OCIBreak.
Friend Shared Sub Break(dbConn As OracleConnection)
Dim ContextHandle = DirectCast(dbConn.GetType.GetProperty("ServiceContextHandle", ref.BindingFlags.Instance Or ref.BindingFlags.NonPublic).GetValue(dbConn, Nothing), Runtime.InteropServices.SafeHandle)
Dim ErrorHandle = DirectCast(dbConn.GetType.GetProperty("ErrorHandle", ref.BindingFlags.Instance Or ref.BindingFlags.NonPublic).GetValue(dbConn, Nothing), Runtime.InteropServices.SafeHandle)
OCIBreak(ContextHandle, ErrorHandle)
End Sub
<Runtime.InteropServices.DllImport("oci.dll", CallingConvention:=Runtime.InteropServices.CallingConvention.Cdecl)>
Private Shared Function OCIBreak(Context As Runtime.InteropServices.SafeHandle, ErrorHandle As Runtime.InteropServices.SafeHandle) As Integer
End Function
What I found out, you can partially emulate it using following code.
Private Sub Cancel(conn As OracleConnection)
Using cmd = New OracleCommand("", conn)
cmd.Cancel()
End Using
End Sub
I expect this to work, because OracleCommand.Cancel cancels currently running command, which is not necessarily this one (docs).
And it actually does work this way - but only for SELECT-queries, it doesn't cancel PL/SQL-blocks or stored procedures. It is still possible to cancel those, but only via the corresponding OracleCommand-instance.
Is it possible to achieve somehow? Maybe there is a way to get that currently running command from oracle connection object?

How to access Speech Recognition via Win32 API?

I'm trying to update a very old VB6 program to provide Speech Recognition.
I'm thinking the best way to do this is by accessing the Win32 API for Speech Recognition.
Looked for this on MS documentation. Looks like previously this would be done via the SAPI 5.3 per this question which has been deprecated and replaced by MS Agent, which itself is deprecated.
http://msdn.microsoft.com/en-us/library/ms720589(VS.85).aspx
The above link is old (because I used it 10 years ago), but is valid. You basically create the appropriate objects, possibly create a grammar in their format, and then create events on the RC object, below:
The below method "works", but it wasn't very impressive. Also, it led us to have to create our own MsgBox function, since we needed to automate everything... And, since you can't just tell Windows to "click" the OK button of a standard message box (unless you want to muck around even more APIs), you have to control even more aspects of your software.
It did work, but I'm not sure anyone ever used it.
Public WithEvents RC As SpSharedRecoContext
Public myGrammar, b As ISpeechRecoGrammar
Private Sub Form_Load()
On Error GoTo EH
Set RC = New SpSharedRecoContext
Set myGrammar = RC.CreateGrammar
myGrammar.CmdLoadFromFile "sol.xml", SLODynamic
myGrammar.CmdSetRuleIdState 0, SGDSActive
EH:
If Err.Number Then ShowErrMsg
End Sub
Private Sub RC_FalseRecognition(ByVal StreamNumber As Long, ByVal StreamPosition As Variant, ByVal Result As SpeechLib.ISpeechRecoResult)
'Label1.Caption = "(no recognition)"
End Sub
Private Sub RC_Recognition(ByVal StreamNumber As Long, ByVal StreamPosition As Variant, ByVal RecognitionType As SpeechLib.SpeechRecognitionType, ByVal Result As SpeechLib.ISpeechRecoResult)
'Label1.Caption = Result.PhraseInfo.GetText
End Sub
Private Sub RC_StartStream(ByVal StreamNumber As Long, ByVal StreamPosition As Variant)
'Label2.Caption = Val(StreamNumber)
End Sub

vb6 using variables as DLL paths

I can't get this to work. It says string constant expected...
Dim const path As String = "\Windows\System32\user32"
Private Declare Function CallWindowProcW Lib path...
Any suggestions?
As was said, you can't do that. If you use a path to a library it must be hard-coded.
However, if you don't use a hard-coded path there's a defined order in which Windows will look for for the library. It will search the app directory, the current dir, the Windows and System dirs, and along the current Path. If you put your DLL in any of those places, and omit the hard-coded path in the reference, it will be found (what order they're searched in varies by Windows version and by where it's documented).
There's one other possibility. If a DLL is already loaded in memory, Windows will reuse the loaded copy. So you can omit the path if you first load the DLL yourself, and for that you can use a string variable. Check out the docs for LoadLibrary. You do have to keep a handle and free the library yourself using FreeLibrary.
Private Declare Function CallWindowProcW Lib "User32" ...
Test it
Declare Statement
Example
The following example declares an external reference to a Function procedure that returns the current user name. It then calls the external procedure GetUserNameA as part of the getUser procedure.
Declare Function getUserName Lib "advapi32.dll" Alias "GetUserNameA" (
ByVal lpBuffer As String, ByRef nSize As Integer) As Integer
Sub getUser()
Dim buffer As String = New String(CChar(" "), 25)
Dim retVal As Integer = getUserName(buffer, 25)
Dim userName As String = Strings.Left(buffer, InStr(buffer, Chr(0)) - 1)
MsgBox(userName)
End Sub

Stringify a method call in VBScript

I'm currently working every day with QuickTest Professional 11, which uses VBScript behind the scenes. Lately, I've started developing some of my own functions to handle common situations. I'm pretty new to VBscript, most of my programming experience is in C and Python.
I'm trying to implement Python's Try/Except in VBScript, mostly to wrap around actions like clicking links or selecting values from dropdown boxes. Here's what I have so far:
Class cls_ErrorHandler
Private bWasError
Private Sub Class_Initialize()
bWasError = False
End Sub
Private Sub IsErr
If Err.Number <> 0 Then
bWasError = True
Else
bWasError = False
End If
Err.Clear
End Sub
' If the command fails, set bWasError
Public Sub Try(strCommandToTry)
On Error Resume Next
Execute(strCommandToTry)
me.IsErr
On Error Goto 0
End Sub
Public Sub Except(strCommandInCaseOfError)
If bWasError Then
Execute(strCommandInCaseOfError)
End If
bWasError = False
End Sub
End Class
I'd like to be able to write things like this:
Set oErrorHandler = New cls_ErrorHandler
oErrorHandler.Try(Stringify(Browser("Browser Name").Page("Page Name").WebCheckBox("Checkbox Name").Set "ON"
oErrorHander.Except(Stringify(Browser("Browser Name").Page("Page Name").WebButton("Save").Click))
As far as I can tell, there really isn't any nice way to pass a function as an argument to another function in VBScript. The best way seems to be to pass a string containing the name of a function, and then feed that string into Execute() or Eval(). Objects in QuickTest Professional tend to have lots of quotation marks, so escaping them all by hand would make the code unreadable.
In C, I'd use something like this:
#define Stringify(obj) #obj
and it would be done... but nothing like that seems to exist in VBScript. Is there any way to implement this? Is there anything in VBScript that takes an object as its input and returns a string representation of that object's name? Would it be possible to write a DLL in C/C# that would provide this kind of functionality?
You can use GetRef to obtain a function/sub pointer, and call the function/sub using that pointer. See online help, it shows an example for an event handler, but you can refer to any global function or sub with GetRef, and use the variable holding the GetRef return value just like an alias for the sub/function.
Be aware, however, that there are cases you won't be able to cover with this:
You cannot use a GetRef function pointer if the current calling stack contains a
method call that is a function registered to a test object via
RegisterUserFunc.
You cannot call such a test object method from within a routine call
that was adressed via a GetRef function pointer.
Also consider using ExecuteGlobal instead of Execute so the code you pass can set global variables that the ExecuteGlobal caller can access afterwards.

If RegOpenKeyEx Does Not Return ERROR_SUCCESS Am I Guaranteed that the HKEY Was Not Opened?

Reading Microsoft's documentation on RegOpenKeyEx and RegCloseKey I am unsure of whether or not I need to call the close function if RegOpenKeyEx fails.
Please point me to a definitive source indicating if I need to always call RegCloseKey or if it only needs to be called when RegOpenKeyEx returns ERROR_SUCCESS.
References:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724897%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724837%28v=vs.85%29.aspx
RegOpenKeyEx will only return a valid key handle if ERROR_SUCCESS is the returned value. This is where the caller is responsible for closing the key, otherwise no closing required and key is not opened. This is the the assumed agreement regarding responsibility to close the opened handle, though not explicitly mentioned in RegOpenKeyEx function documentation.
This is also consistent across API samples. If you are unsure after checking sample code in the MSDN article, here is another one: http://msdn.microsoft.com/en-us/library/aa384182%28VS.85%29.aspx
I think if you look at the example listed under your reference links you can see that it does not call RegCloseKey if lResult does not return ERROR_SUCCESS
This is the link to it:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724235(v=vs.85).aspx

Resources