I found this ppiece of code on the powerbasic forums, can someone help me to convert to purebasic ? I'm a beginner and not understand very well pointers, i tried to convert to pb by myself but what i had is just errors...
Here's the code
Code: Select all
#COMPILE EXE
#INCLUDE "WIN32API.INC"
'----------------------------------------------------------------------------------------------------------------------------------------
' this code is based on Microsoft MSKB Q183478
'----------------------------------------------------------------------------------------------------------------------------------------
FUNCTION IsServiceRunning(lngServiceType AS LONG, BYVAL strText AS STRING, lstServiceName AS LONG, lstDisplayName AS LONG) AS LONG
LOCAL hSCManager AS DWORD
LOCAL x AS LONG
LOCAL dx AS ENUM_SERVICE_STATUS
LOCAL lpEnumServiceStatus() AS ENUM_SERVICE_STATUS
LOCAL lngBytesNeeded AS LONG
LOCAL lpServicesReturned AS DWORD
LOCAL hNextUnreadEntry AS DWORD
LOCAL lngStructsNeeded AS LONG
LOCAL lngServiceStatusInfoBuffer AS LONG
LOCAL strServiceName AS ASCIIZ * 250
LOCAL i AS LONG
'local lngServiceType as long (either %SERVICE_ACTIVE or
' %SERVICE_ACTIVE Or %SERVICE_INACTIVE
'strText is the name to search, it is always the ServiceName
hSCManager = OpenSCManager(BYVAL 0, BYVAL 0, %SC_MANAGER_ENUMERATE_SERVICE)
IF hSCManager THEN
hNextUnreadEntry = 0
x = EnumServicesStatus(hSCManager, %SERVICE_WIN32, lngServiceType, dx, &H0, _
lngBytesNeeded, lpServicesReturned, hNextUnreadEntry)
'We should receive %MORE_DATA error.
IF NOT GetLastError() = %ERROR_MORE_DATA THEN
EXIT FUNCTION
ELSE
'Calculate the number of structures needed
lngStructsNeeded = lngBytesNeeded / LEN(lpEnumServiceStatus(0)) + 1
'Redimension the array according to our calculation
REDIM lpEnumServiceStatus(lngStructsNeeded - 1)
'Get buffer size in bytes
lngServiceStatusInfoBuffer = lngStructsNeeded * LEN(lpEnumServiceStatus(0))
'Get services information starting entry 0
hNextUnreadEntry = 0
x = EnumServicesStatus(hSCManager, _
%SERVICE_WIN32, _
lngServiceType, _
lpEnumServiceStatus(0), _
lngServiceStatusInfoBuffer, _
lngBytesNeeded, _
lpServicesReturned, _
hNextUnreadEntry)
IF x = 0 THEN
'failed, call GetLastError why
ELSE
'clear the listboxes
IF lstServiceName > 0 THEN SendMessage lstServiceName, %LB_RESETCONTENT, 0, 0
IF lstDisplayName > 0 THEN SendMessage lstDisplayName, %LB_RESETCONTENT, 0, 0
FOR i = 0 TO lpServicesReturned - 1
x = lstrcpy(strServiceName, BYVAL lpEnumServiceStatus(i).lpDisplayName)
IF lstDisplayName > 0 THEN SendMessage lstDisplayName, %LB_ADDSTRING, 0, VARPTR(strServiceName)
x = lstrcpy(strServiceName, BYVAL lpEnumServiceStatus(i).lpServiceName)
IF lstServiceName > 0 THEN SendMessage lstServiceName, %LB_ADDSTRING, 0, VARPTR(strServiceName)
IF strText > "" THEN
IF LCASE$(strText) = LCASE$(strServiceName) THEN FUNCTION = %TRUE
END IF
NEXT
END IF
END IF
'Clean up.
CloseServiceHandle hSCManager
END IF
END FUNCTION
CALLBACK FUNCTION DlgProc()
LOCAL hndl1 AS LONG
LOCAL hndl2 AS LONG
LOCAL strSearch AS STRING
SELECT CASE CBMSG
CASE %WM_COMMAND
IF CBCTLMSG = %BN_CLICKED THEN
SELECT CASE CBCTL
CASE 103 'active services only
CONTROL HANDLE CBHNDL, 101 TO hndl1
CONTROL HANDLE CBHNDL, 102 TO hndl2
IsServiceRunning %SERVICE_ACTIVE, "", hndl1, hndl2
CASE 104 'all services
CONTROL HANDLE CBHNDL, 101 TO hndl1
CONTROL HANDLE CBHNDL, 102 TO hndl2
IsServiceRunning %SERVICE_ACTIVE OR %SERVICE_INACTIVE, "", hndl1, hndl2
CASE 105 'is running
strSearch = INPUTBOX$("Search for this service name")
IF ISTRUE IsServiceRunning (%SERVICE_ACTIVE, strSearch, 0, 0) THEN
MSGBOX "Yes"
ELSE
MSGBOX "No"
END IF
CASE 106 'is installed
strSearch = INPUTBOX$("Search for this service name")
IF ISTRUE IsServiceRunning (%SERVICE_ACTIVE OR %SERVICE_INACTIVE, strSearch, 0, 0) THEN
MSGBOX "Yes"
ELSE
MSGBOX "No"
END IF
END SELECT
END IF
END SELECT
END FUNCTION
FUNCTION PBMAIN()
LOCAL hDlg AS LONG
DIALOG NEW %NULL, "Enum Services", , , 300, 200, %WS_VISIBLE OR %WS_SYSMENU TO hDlg
CONTROL ADD LISTBOX, hDlg, 101, , 2, 2, 140, 160
CONTROL ADD LISTBOX, hDlg, 102, , 156, 2, 140, 160
CONTROL ADD BUTTON, hDlg, 103, "Active Services", 5, 162, 60, 14
CONTROL ADD BUTTON, hDlg, 104, "All Services", 233, 162, 60, 14
CONTROL ADD BUTTON, hDlg, 105, "Is Running?", 70, 162, 60, 14
CONTROL ADD BUTTON, hDlg, 106, "Is Installed?", 169, 162, 60, 14
DIALOG SHOW MODAL hDlg, CALL DlgProc
END FUNCTION
------------------
In other words, what would be the PB code to enumerate the service in the servicedatabase ?
Thanx by advance if someone can help me.
Starter.
*edited*
I've found something that might be better to translate to obtain the services list: (Vb code)
Code: Select all
Const ERROR_MORE_DATA = 234
Const SERVICE_ACTIVE = &H1
Const SERVICE_INACTIVE = &H2
Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Const SERVICE_WIN32_OWN_PROCESS As Long = &H10
Const SERVICE_WIN32_SHARE_PROCESS As Long = &H20
Const SERVICE_WIN32 As Long = SERVICE_WIN32_OWN_PROCESS + SERVICE_WIN32_SHARE_PROCESS
Private Type SERVICE_STATUS
dwServiceType As Long
dwCurrentState As Long
dwControlsAccepted As Long
dwWin32ExitCode As Long
dwServiceSpecificExitCode As Long
dwCheckPoint As Long
dwWaitHint As Long
End Type
Private Type ENUM_SERVICE_STATUS
lpServiceName As Long
lpDisplayName As Long
ServiceStatus As SERVICE_STATUS
End Type
Private Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Private Declare Function EnumServicesStatus Lib "advapi32.dll" Alias "EnumServicesStatusA" (ByVal hSCManager As Long, ByVal dwServiceType As Long, ByVal dwServiceState As Long, lpServices As Any, ByVal cbBufSize As Long, pcbBytesNeeded As Long, lpServicesReturned As Long, lpResumeHandle As Long) As Long
Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (szDest As String, szcSource As Long) As Long
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim hSCM As Long, lpEnumServiceStatus() As ENUM_SERVICE_STATUS, lngServiceStatusInfoBuffer As Long
Dim strServiceName As String * 250, lngBytesNeeded As Long, lngServicesReturned As Long
Dim hNextUnreadEntry As Long, lngStructsNeeded As Long, lngResult As Long, i As Long
'Open connection to Service Control Manager.
hSCM = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ENUMERATE_SERVICE)
If hSCM = 0 Then
MsgBox "OpenSCManager failed. LastDllError = " & CStr(Err.LastDllError)
Exit Sub
End If
'Get buffer size (bytes) without passing a buffer
'and make sure starts at 0th entry.
hNextUnreadEntry = 0
lngResult = EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_ACTIVE Or SERVICE_INACTIVE, ByVal &H0, &H0, lngBytesNeeded, lngServicesReturned, hNextUnreadEntry)
'We should receive MORE_DATA error.
If Not Err.LastDllError = ERROR_MORE_DATA Then
MsgBox "LastDLLError = " & CStr(Err.LastDllError)
Exit Sub
End If
'Calculate the number of structures needed.
lngStructsNeeded = lngBytesNeeded / Len(lpEnumServiceStatus(0)) + 1
'Redimension the array according to our calculation.
ReDim lpEnumServiceStatus(lngStructsNeeded - 1)
'Get buffer size in bytes.
lngServiceStatusInfoBuffer = lngStructsNeeded * Len(lpEnumServiceStatus(0))
'Get services information starting entry 0.
hNextUnreadEntry = 0
lngResult = EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_ACTIVE Or SERVICE_INACTIVE, lpEnumServiceStatus(0), lngServiceStatusInfoBuffer, lngBytesNeeded, lngServicesReturned, hNextUnreadEntry)
If lngResult = 0 Then
MsgBox "EnumServicesStatus failed. LastDllError = " & CStr(Err.LastDllError)
Exit Sub
End If
'Get the strings and display them.
Me.AutoRedraw = True
Me.Print "All registered services:" + vbCrLf
For i = 0 To lngServicesReturned - 1
lngResult = lstrcpy(ByVal strServiceName, ByVal lpEnumServiceStatus(i).lpServiceName)
Me.Print StripTerminator(strServiceName) + " - ";
lngResult = lstrcpy(ByVal strServiceName, ByVal lpEnumServiceStatus(i).lpDisplayName)
Me.Print StripTerminator(strServiceName)
Next i
'Clean up.
CloseServiceHandle (hSCM)
End Sub
Function StripTerminator(sInput As String) As String
Dim ZeroPos As Integer
ZeroPos = InStr(1, sInput, Chr$(0))
If ZeroPos > 0 Then
StripTerminator = Left$(sInput, ZeroPos - 1)
Else
StripTerminator = sInput
End If
End Function