在Windows服务中引用dll

时间:2015-06-18 19:39:57

标签: vb.net service opc

我使用interop.opcautomation.dll写了一个OPC客户端。我在vb 2010 express中将其写为windows窗体。一切都目前运作良好。我想把它变成一个Windows服务。我遇到的问题是,当我运行服务时它不会连接到opc服务器。我得到以下异常“对象引用未设置为对象的实例.- OPC连接错误”我在我尝试连接的代码的末尾得到异常。下面是代码。感谢您的任何意见,我们对此表示赞赏。

Imports System.ServiceProcess
Imports System.Threading
Imports System.Text
Imports System.Windows.Forms
Imports System
Imports System.IO
Imports System.IO.File
Imports System.Net
Imports OPCAutomation


Public Class Service1
    Inherits System.ServiceProcess.ServiceBase

    Dim WithEvents OPCServer As OPCAutomation.OPCServer
    Dim WithEvents ConnectedGroup As OPCAutomation.OPCGroup

    Dim Batch As String
    Dim Group As String
    Dim Press As String
    Dim Line As String
    Dim sWidth As String
    Dim sHeight As String
    Dim sBifold As String
    Dim sCore As String
    Dim StartUp As Integer
    Dim iHandle As Integer
    Dim sError As String

    Dim ItemIds(60) As String
    Dim ServerHandles As Array
    Dim ClientHandles(60) As Integer
    Dim ServerErrors As Array

    Dim SnapServerURL As String = "snapserver"

    Private trd As Thread
    Private Stopping As Boolean = False
    Private StoppedEvent As New ManualResetEvent(False)

    Protected Overrides Sub OnStart(ByVal args() As String)

        EventLog.WriteEntry("PressControlService in OnStart.")

        trd = New Thread(AddressOf ServiceMain)
        trd.IsBackground = True
        trd.Start()

    End Sub

    Protected Overrides Sub OnStop()

        EventLog.WriteEntry("PressControlService in OnStop.")
        Me.Stopping = True
        Me.StoppedEvent.WaitOne()

    End Sub

    Private Sub ServiceMain()

        Dim strOPCServerName As String
        Dim strOPCServerNode As String
        Dim strOPCGroupName As String
        Dim i As Integer

        StartUp = 0
        strOPCServerName = "Kepware.KEPServerEX.V5"
        strOPCServerNode = ""
        strOPCGroupName = "MyGroup"
        Try
            Dim OPCServer As New OPCAutomation.OPCServer
        Catch ex As Exception
            EventLog.WriteEntry(ex.Message & " - New OPC Server")
        End Try

        Try
            OPCServer.Connect(strOPCServerName, strOPCServerNode)
        Catch ex As Exception
            EventLog.WriteEntry(ex.Message & "- OPC Connect Error")
        End Try
    End Sub
End Class

0 个答案:

没有答案