1

替代标题:打开控制时自动运行 THINC 应用程序(仅在 OSP NC 系统启动后运行)

我正在为 OSP-P300 控件(运行 WinXP)编写一个应用程序,并希望它在控件启动/打开时自动启动。我曾尝试在启动文件夹中使用快捷方式,但这会导致问题。

当应用程序在 NC 软件完成启动之前运行时,我的 THINC API 函数出现错误。(API 尚不可用)

我知道“Okuma THINC 启动服务”程序,并让它在我的控制下运行。手动配置时,此方法会解决问题并在适当的时间加载我的应用程序。

我的问题是:有没有办法在安装过程中以编程方式将我的应用程序添加到启动服务?

是的,用户仍然可以手动执行此操作,但安装期间默认选中的复选框选项会简单得多。

是否可以像添加一些注册表项一样简单?

4

2 回答 2

2

使用 API 磁盘上的启动服务。
这是我用来注册/注销的类。
我从这里得到的 CReg 类:RegistryKeyAccess.vb

导入 Microsoft.Win32

Public Class ThincStartupReg

    Public Overloads Shared Sub Register(ApplicationPath As String, ApplicationName As String, AppType As enumAppType, wait As Boolean, LaunchType As enumLaunch)
        Try
            Dim ObjReg As New CReg
            Dim regCreated As Boolean

            If ObjReg.ReadValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\StartupService\", "State") Then

                'Startup Service is installed

                If Not ObjReg.ReadValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName, "Enabled") Then
                    'No entry for this program
                    regCreated = ObjReg.CreateSubKey(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName)

                Else
                    regCreated = True
                End If

                If regCreated Then
                    ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                     "Type", "Process")
                    ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                      "Name", ApplicationName)
                    ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                      "Enabled", "True")
                    ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                      "Wait", If(wait, "True", "False"))
                    ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                      "Type", If(AppType = enumAppType.Process, "Process", "Service"))
                    ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                      "Launch", If(LaunchType = enumLaunch.LaunchOnce, "Once", "Monitor"))
                    ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                     "File", ApplicationPath)
                End If


            End If

        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Public Overloads Shared Sub Register(ThisAssembly As System.Reflection.Assembly, ByVal AppType As enumAppType, ByVal Wait As Boolean, ByVal LaunchType As enumLaunch)
        Dim AppName = ThisAssembly.FullName.Split(",")(0)
        Dim AppPath = ThisAssembly.Location
        Register(AppPath, AppName, AppType, Wait, LaunchType)
    End Sub

    Public Shared Sub UnRegister()
        Try
            Dim ObjReg As New CReg
            Dim AppName = System.Reflection.Assembly.GetExecutingAssembly().FullName.Split(",")(0)

            If ObjReg.ReadValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\StartupService\", "State") Then
                If ObjReg.ReadValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & AppName, "Enabled") Then
                    ObjReg.DeleteSubKey(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & AppName)
                End If

            End If

        Catch ex As Exception
            Throw ex
        End Try

    End Sub


End Class

如果您不确定安装服务是否会安装,您可以将快捷方式添加到启动文件夹,然后循环直到 OSP 进程开始。

Public Shared Function Wait(Timeout As TimeSpan) As Integer
        If File.Exists("C:\OSP-P\OSPMNGCD.CNC") Then
            Dim startTime = Now
            Dim myProcess As Process() = Process.GetProcessesByName("PNC-P200")
            While myProcess.Length = 0
                If Now.Subtract(startTime) >= Timeout Then Return -1
                myProcess = Process.GetProcessesByName("PNC-P200")
                Thread.Sleep(1000)
            End While
            'OSP Started
            Return 1
        End If
        'Simulation mode (not on a machine)
        Return 2
    End Function
于 2013-10-31T23:20:18.843 回答
1

Instructions for pragmatically adding a Startup Item for the THINC Startup Service (TSS) to handle:

Startup of an application via the TSS is controlled by the Registry, in the following Key:

"HKEY_LOCAL_MACHINE\Software\OAC\Startup"

To "register" your application to be handled by the TSS, create a sub-key, under Startup, which names your app:

"HKEY_LOCAL_MACHINE\Software\OAC\Startup\ScottsApp"

ScottsApp should then be assigned the following values:

    NAME
    FILE
    TYPE
    ENABLED
    LAUNCH
    WAIT
    DELAY
    ARGUMENT

NAME = The "display name" for your application if it is a Process, or the Service Name if it is a Windows Service

FILE = The full path, including the executable name, to your application's executable assembly. This is internally disregarded if the TYPE value is "SERVICE", as the TSS would then use the NAME value to start the specified Windows Service

TYPE = "SERVICE" or "PROCESS"

ENABLED = TRUE or FALSE (Boolean). Allows the Startup Item to be enabled or disabled without being fully removed from the TSS's list

LAUNCH = "ONCE" or "MONITOR" If "ONCE", then the application is launched one time; if "MONITOR", then the application is kept alive - If a user closes it, it will be re-launched by the TSS

WAIT = TRUE or FALSE (Boolean) If FALSE, then the application will be launched as soon as the TSS is launched at startup. If TRUE, then the application will be launched only after the TSS has detected that the Okuma NC control software has been fully booted, and the THINC API is usable.

DELAY = Integer value; Represents the number of milliseconds that the TSS should wait, after the WAIT condition has been met, before launching your application. For instance, if you want to wait an additional 10 seconds, set this value to 10000

ARGUMENT = Any command line argument necessary for your application

于 2013-10-31T17:05:17.283 回答