• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Determining service name when running a VB6 program

Is there a way I can determine the service name a program is running as?  I want to run a VB6 program in three different services, and I want it to behave in three slightly different ways when it's running.
0
thomsone
Asked:
thomsone
  • 3
  • 2
1 Solution
 
kbireckiCommented:
I'm not sure if there is enough information here.  How are you running the applications as services?  Is it one VB6 app that runs differently based on something external to the app?  Are multiple instances possibly running simultaneously?  And when you say "Determine", what do you mean?  Are you trying to access the VB6 service from an external app?

Making a lot of assumptions, until you can clarify, I had a similar (maybe remotely similar) question where I got a couple of goo options.  one was immediately workable (AtiveX EXE), the other will take some more time (Using the Running Object Table - ROT).

My question was solved with this core solution plus the changes I found were needed to make it work in one of my follow-up comments.


This gave me a way to communicate with my VB6 app via an external VBScript routine.  This is how it is similar to your question, you could use this concept to talk to your VB6 service (?) and ask it for it's state.
0
 
thomsoneAuthor Commented:
Sorry for the late reply.  I want the VB program to be aware of the service name it was started with.  I was going to use srvany.exe to run it as a service, and I want the program to see what service name it's running under.
0
 
kbireckiCommented:
Are you passing a parameter to the vb6 app in the srvany command line?  Out just sering the name displayed in services?   Without a parameter, I don't know how you could associate the running app with the service name.   If you pass a parameter, though, that should do the trick.  Could you do it that way instead of relying on the service name?
0
 
thomsoneAuthor Commented:
I could do that.  How should I compile the app to get the parameter value?  I understand that there's a VB variable called "command" that I could use?  Thanks!
0
 
kbireckiCommented:
You just use the implicit reference "Command$" to get the command line argument, such as:

strParameter = Command$

Open in new window


This picks up the entire string after your app name as one big string.

If you want to pass multiple parameters, you have to parse them otu yourself.  You could use a separator, like commas.  So you could have a command line with multiple parameters like this:

YourVB6App.exe Param1, Param2, Param3

Open in new window


...and in your app you could separate them like this:

Dim arrParameters
Dim strParameters as string
Dim i as integer

strParameters =Command$
arrParameters = split(strParameters,",")

'Each array element will be a separate parameter, and can can be indeterminate how many parameters if you want.

For i = 0 to UBound(arrParameters) - LBound(arrParameters)
     MsgBox arrParameters(i)
Next i

Open in new window


In the example above, you would get three array elements, "Param1", "Param2", "Param3".

Taking it one step further, if you don't want to be concerned with parameter order or you want to be able to handle an optional number of parameters, you can pass parameters as name-value pairs, such as "Color=Blue", and then you Split() them a second time on the equal sign.  So a multiple parameter command line could look like this:

YourVB6App.exe Color=Blue, height=100, width=75

Open in new window


And you could parse them like this:

'************************************************************
'Declarations:
'************************************************************
Private Type ParameterNVPair
    pName As String
    pValue As String
End Type

'************************************************************
'Form Load, button, our routine code:
'************************************************************
    Dim arrParameters
    Dim arrNVPairs() As ParameterNVPair
    Dim strParameters As String
    Dim i As Integer
    
    strParameters = Command$
    arrParameters = Split(strParameters, ",")
    
    'Each array element will be a separate parameter, and can can be indeterminate how many parameters if you want.
    ReDim arrNVPairs(0)
    For i = 0 To UBound(arrParameters) - LBound(arrParameters)
        ReDim Preserve arrNVPairs(i + 1)
        If InStr(arrParameters(i), "=") > 0 Then
            arrNVPairs(i).pName = Split(arrParameters(i), "=")(0)
            arrNVPairs(i).pValue = Split(arrParameters(i), "=")(1)
        Else
            arrNVPairs(i).pName = arrParameters(i)
            arrNVPairs(i).pValue = "Nothing"
        End If
        MsgBox arrNVPairs(i).pName & " has been set to " & arrNVPairs(i).pValue
    Next i

Open in new window


And this example handles parameters that are not name-value pairs, such as "Enabled" in this example:
YourVB6App.exe Color=Blue, Enabled, height=100, width=75

Open in new window


The "Enabled" parameter here will be assigned to the Name portion of the array arrNVPairs(), but it will leave the Value portion empty.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now