Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - Общий форум

Страница: 1 |

 

  Вопрос: VB6 -> VB.NET (EnumServiceStatus) Добавлено: 17.04.05 21:33  

Автор вопроса:  Yura | ICQ: 34372249 
Надо перевести проект VB6 под .NET Ругается на определение функции. тип "any" гады убрали (хотя более жесткая типизация).


1. Create a new Standard EXE project in Visual Basic, Project1. Form1 is created by default.
2. Place two command buttons and two list boxes on Form1 and place the following code in Form1's code window:
3.       Private Sub Command1_Click()
4.          'Getting only the active services
5.             GetServiceInfo SERVICE_ACTIVE
6.       End Sub
7.
8.       Private Sub Command2_Click()
9.          'Getting all services registered
10.             GetServiceInfo SERVICE_ACTIVE Or SERVICE_INACTIVE
11.       End Sub
12.
13.       Private Sub Form_Load()
14.          Command1.Caption = "Active Services"
15.          Command2.Caption = "All Registered Services"
16.       End Sub
17.

18. Add a standard module (Module1) to Project1 and place the following code in Module1.
19.       '****************** MODULE1 CODE **********************
20.
21.       'UDTs
22.       '*******************************************************
23.       Type SERVICE_STATUS
24.          dwServiceType As Long
25.          dwCurrentState As Long
26.          dwControlsAccepted As Long
27.          dwWin32ExitCode As Long
28.          dwServiceSpecificExitCode As Long
29.          dwCheckPoint As Long
30.          dwWaitHint As Long
31.       End Type
32.
33.       Type ENUM_SERVICE_STATUS
34.          lpServiceName As Long
35.          lpDisplayName As Long
36.          ServiceStatus As SERVICE_STATUS
37.       End Type
38.       '*******************************************************
39.
40.       'Constants
41.       '*******************************************************
42.       Public Const ERROR_MORE_DATA = 234
43.       Public Const SERVICE_ACTIVE = &H1
44.       Public Const SERVICE_INACTIVE = &H2
45.       Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4
46.       Public Const SERVICE_WIN32_OWN_PROCESS As Long = &H10
47.       Public Const SERVICE_WIN32_SHARE_PROCESS As Long = &H20
48.       Public Const SERVICE_WIN32 As Long = SERVICE_WIN32_OWN_PROCESS _
49.                   + SERVICE_WIN32_SHARE_PROCESS
50.       '*******************************************************
51.
52.       'API declarations
53.       '*******************************************************
54.       Public Declare Function OpenSCManager Lib "advapi32.dll" _
55.             Alias "OpenSCManagerA" _
56.          (ByVal lpMachineName As String, ByVal lpDatabaseName As String, _
57.                ByVal dwDesiredAccess As Long) As Long
58.
59.       Public Declare Function EnumServicesStatus Lib "advapi32.dll" _
60.             Alias "EnumServicesStatusA" (ByVal hSCManager As Long, _
61.             ByVal dwServiceType As Long, ByVal dwServiceState As Long, _
62.             lpServices As Any, ByVal cbBufSize As Long, _
63.             pcbBytesNeeded As Long, lpServicesReturned As Long, _
64.             lpResumeHandle As Long) As Long
65.
66.       Public Declare Function CloseServiceHandle Lib "advapi32.dll" _
67.             (ByVal hSCObject As Long) As Long
68.
69.       Public Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" _
70.             (szDest As String, szcSource As Long) As Long
71.       '*******************************************************
72.
73.       'Where the real work is done.
74.       '*******************************************************
75.       Public Sub GetServiceInfo(lngServiceType As Long)
76.          Dim hSCM As Long
77.          Dim lpEnumServiceStatus() As ENUM_SERVICE_STATUS
78.          Dim lngServiceStatusInfoBuffer As Long
79.          Dim strServiceName As String * 250
80.          Dim lngBytesNeeded As Long
81.          Dim lngServicesReturned As Long
82.          Dim hNextUnreadEntry As Long
83.          Dim lngStructsNeeded As Long
84.          Dim lngResult As Long
85.          Dim i As Long
86.
87.          'Open connection to Service Control Manager.
88.          hSCM = OpenSCManager(vbNullString, vbNullString, _
89.                               SC_MANAGER_ENUMERATE_SERVICE)
90.
91.          If hSCM = 0 Then
92.             MsgBox "OpenSCManager failed. LastDllError = " _
93.             & CStr(Err.LastDllError)
94.             Exit Sub
95.          End If
96.
97.          'Get buffer size (bytes) without passing a buffer
98.          'and make sure starts at 0th entry.
99.          hNextUnreadEntry = 0
100.          lngResult = EnumServicesStatus(hSCM, _
101.                                         SERVICE_WIN32, _
102.                                         lngServiceType, _
103.                                         ByVal &H0, _
104.                                         &H0, _
105.                                         lngBytesNeeded, _
106.                                         lngServicesReturned, _
107.                                         hNextUnreadEntry)
108.
109.          'We should receive MORE_DATA error.
110.          If Not Err.LastDllError = ERROR_MORE_DATA Then
111.             MsgBox "LastDLLError = " & CStr(Err.LastDllError)
112.             Exit Sub
113.          End If
114.
115.          'Calculate the number of structures needed.
116.          lngStructsNeeded = _
117.          lngBytesNeeded / Len(lpEnumServiceStatus(0)) + 1
118.
119.          'Redimension the array according to our calculation.
120.          ReDim lpEnumServiceStatus(lngStructsNeeded - 1)
121.
122.          'Get buffer size in bytes.
123.          lngServiceStatusInfoBuffer = _
124.             lngStructsNeeded * Len(lpEnumServiceStatus(0))
125.
126.          'Get services information starting entry 0.
127.          hNextUnreadEntry = 0
128.          lngResult = EnumServicesStatus(hSCM, _
129.                                        SERVICE_WIN32, _
130.                                        lngServiceType, _
131.                                        lpEnumServiceStatus(0), _
132.                                        lngServiceStatusInfoBuffer, _
133.                                        lngBytesNeeded, _
134.                                        lngServicesReturned, _
135.                                        hNextUnreadEntry)
136.
137.          If lngResult = 0 Then
138.             MsgBox "EnumServicesStatus failed. LastDllError = " _
139.                & CStr(Err.LastDllError)
140.             Exit Sub
141.          End If
142.
143.
144.          'Get the strings and display them.
145.          With Form1
146.             .List1.Clear
147.             .List1.AddItem "Display Names"
148.             .List2.Clear
149.             .List2.AddItem "Service Names"
150.             For i = 0 To lngServicesReturned - 1
151.                lngResult = lstrcpy(ByVal strServiceName, _
152.                   ByVal lpEnumServiceStatus(i).lpDisplayName)
153.                .List1.AddItem strServiceName
154.                lngResult = lstrcpy(ByVal strServiceName, _
155.                   ByVal lpEnumServiceStatus(i).lpServiceName)
156.                .List2.AddItem strServiceName
157.             Next
158.          End With
159.
160.          'Clean up.
161.          CloseServiceHandle (hSCM)
162.
163.       End Sub
164.       '******************* END OF MODULE1 **************************

Ответить

  Ответы Всего ответов: 1  

Номер ответа: 1
Автор ответа:
 Павел



Администратор

ICQ: 326066673 

Вопросов: 368
Ответов: 5968
 Web-сайт: www.vbnet.ru
 Профиль | | #1
Добавлено: 18.04.05 10:27
Тебе, видимо, придется все декларации заново переписывать, потому что
во-первых, в VB .NET вместо Long надо использовать тип Integer (а
вместо Integer - Short), во-вторых, строки фиксированной длины не так
объявляются, в-третьих, могут быть другие проблемы с объявлением
строк.

Так что либо качай AllApi APIViewer (или APIGuide) и смотри там
декларации функций на VB .NET, либо читай MSDN и пиши декларации
соответственно ему.

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам