Tetapi jangan kuatir setelah Anda membaca artikel ini, Anda akan dengan mudah membuat service di windows.
Sebenarnya ini adalah artikel lama tapi kayaxnya masih jarang dibahas diblog-blog pemrograman lokal atau mungkin mereka sudah merasa cukup puas dengan membuat aplikasi yang bisa running pada saat windows diaktifkan.
Salah satu keuntungan aplikasi dibuat service adalah bisa running tanpa harus login ke windows dan tentunya tidak semua aplikasi harus dibuat service cukup aplikasi-aplikasi tertentu saja, contoh sample aplikasi yang ada disini.
Contoh-contoh source code yang mengklaim bisa membuat service dengan vb6 biasanya menggunakan fungsi-fungsi API yang buanyakkk dan seperti biasa kalo salah-salah bermain API bisa terbakar loh
Di artikel kali ini kita akan mencoba cara yang mudah/instan yaitu dengan menggunakan komponen NT Service Control Module.
Untuk sample program yang akan kita bahas nanti akan membutuhkan 2 parameter pada saat dijalankan yaitu :
- /i -> untuk menginstall/mendaftarkan aplikasi sebagai service
- /u -> untuk menguninstall/menghapus aplikasi dari service
- Command -> Variant
- Command$ -> String
Public Sub Main() Dim param As String param = Command$ Select Case param Case "/i" 'TODO : install sevice Case "/u" 'TODO : uninstall service Case Else 'parameter tidak dikenal End Select End SubKomponen NT Service Control Module mempunya beberapa method, properties dan event yang memudahkan kita untuk membuat service :
Oke kita mulai saja langkah-langkah pembuatan service di vb dengan menggunakan komponen NT Service Control Module :
1. Buat project baru dan tambahkan komponen NT Service Control Module (komponen ini bisa Anda download pada bagian akhir artikel)
2. Masuk ke bagian editor code form kemudian tambahkan prosedur berikut :
Private Sub initNTService() On Error GoTo ServiceError stopService = False With NTService1 .DisplayName = "Coding4ever NT Service" .ServiceName = "coding4everNTService" lblAnimasi.Caption = .DisplayName & " Loading" 'Install the service If Command$ = "/i" Then 'enable interaction with desktop .Interactive = True .StartMode = svcStartAutomatic 'Install the program as an NT service If .Install Then 'Save the TimerInterval Parameter in the Registry .SaveSetting "Parameters", "TimerInterval", "45" MsgBox .DisplayName & ": installed successfully" Else MsgBox .DisplayName & ": failed to install" End If End 'Remove the Service Registry Keys and uninstall the service ElseIf Command$ = "/u" Then If .Uninstall Then MsgBox .DisplayName & ": uninstalled successfully" Else MsgBox .DisplayName & ": failed to uninstall" End If End 'Invalid parameter ElseIf Command$ <> "" Then MsgBox "Invalid Parameter" End End If 'Retrive the stored value for the timer interval tmrAnimasi.Interval = CInt(.GetSetting("Parameters", "TimerInterval", "45")) 'enable Pause/Continue. Must be set before StartService 'is called or in design mode .ControlsAccepted = svcCtrlPauseContinue 'connect service to Windows NT services controller .StartService End With Exit Sub ServiceError: Call NTService1.LogEvent(svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description) End Sub
3. Langkah selanjutnya kita tinggal membuat kode untuk menghandle event-event berikut :
- Continue
- Control
- Pause
- Start
- Stop
Private Sub NTService1_Continue(Success As Boolean) 'Handle the continue service event On Error GoTo ServiceError tmrAnimasi.Enabled = True lblAnimasi.Caption = NTService1.DisplayName & " Running" Success = True NTService1.LogEvent svcEventInformation, svcMessageInfo, "Service continued" Exit Sub ServiceError: NTService1.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description End Sub Private Sub NTService1_Control(ByVal mEvent As Long) 'Take control of the service events On Error GoTo ServiceError lblAnimasi.Caption = NTService1.DisplayName & " Control signal " & CStr([mEvent]) Exit Sub ServiceError: NTService1.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description End Sub Private Sub NTService1_Pause(Success As Boolean) 'Pause Event Request On Error GoTo ServiceError tmrAnimasi.Enabled = False lblAnimasi.Caption = NTService1.DisplayName & " Paused" NTService1.LogEvent svcEventError, svcMessageError, "Service paused" Success = True Exit Sub ServiceError: NTService1.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description End Sub Private Sub NTService1_Start(Success As Boolean) 'Start Event Request On Error GoTo ServiceError lblAnimasi.Caption = NTService1.DisplayName & " Running" Success = True Exit Sub ServiceError: NTService1.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description End Sub Private Sub NTService1_Stop() 'Stop and terminate the Service On Error GoTo ServiceError lblAnimasi.Caption = NTService1.DisplayName & " Stopped" stopService = True Unload Me ServiceError: NTService1.LogEvent svcMessageError, svcEventError, "[" & Err.Number & "] " & Err.Description End Sub
4. Terakhir adalah kode untuk menghandle jika di program service kita ditutup (event Form_Unload)
Private Sub Form_Unload(Cancel As Integer) 'Unload the Service If Not stopService Then If MsgBox("Are you sure you want to unload the service?..." & vbCrLf & "the service will be stopped", vbQuestion + vbYesNo, "Stop Service") = vbYes Then NTService1.stopService lblAnimasi.Caption = NTService1.DisplayName & " Stopping" Cancel = True Else Cancel = True End If End If End SubUntuk menguji program yang kita buat berhasil atau tidak lakukan langkah-langkah berikut :
1. Compile project kita, misal hasil compile -> coding4everNTService.exe
2. Buat 2 buah file dengan ekstensi .bat atau .cmd, ke 2 file ini bertugas untuk menginstall/menguninstall aplikasi kita sebagai service
Isi file Install.bat
1. coding4everNTService /i
Isi file Uninstall.bat
2. coding4everNTService /uJalankan file Install.bat kemudian klik Start -> Run, ketik : services.msc
Setelah itu cek nama service yang kita buat misal Coding4ever NT Service
Jika tampil berarti kita sudah sukses membuat service dan klik link Start the service yang ada dipojok kiri atas untuk menjalankan service, jika berhasil status service kita akan menjadi Started
Oke mungkin itu saja yang bisa saya bahas pada artikel kali ini, sample program + komponen NT Service Control Module bisa Anda download disini.
Selamat mencoba
0 comments:
Posting Komentar