Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remote machine CPU usage

Status
Not open for further replies.

NFI

Programmer
Jun 7, 2000
278
GB
What-ho,<br><br>&nbsp;&nbsp;I'm *still* hammering away at this one...still no luck :(<br><br>&nbsp;&nbsp;My idea is that I can write a little test app that goes off to a remote machine, running Windows NT4.0 and retrieve its CPU load, the same as task mangler does for your local machine.&nbsp;&nbsp;I'm trying to do it through API calls (the ADVAPI32.DLL API), using the RegCloseKey, RegConnectRegistry, RegOpenKeyEx and RegQueryValueEx functions to connect to the remote machines' HKEY_DYN_DATA key.&nbsp;&nbsp;Admitedly, under WinNT, I should be connecting to the HKEY_PERFORMANCE_DATA key, but my NT machines don't have this key and my code doesn't work either way.<br><br>&nbsp;&nbsp;If anyone can help me here, I'll buy them an e-pint :)<br><br>&nbsp;Cheers very much,<br><br>Paul<br><br><br>Here's my code; good luck out there...<br><br>'(General)<br>Private Declare Function RegCloseKey Lib &quot;advapi32.dll&quot; (ByVal hKey As Long) As Long<br>Private Declare Function RegConnectRegistry Lib &quot;advapi32.dll&quot; Alias &quot;RegConnectRegistryA&quot; (ByVal lpMachineName As String, ByVal hKey As Long, phkResult As Long) As Long<br>Private Declare Function RegOpenKeyEx Lib &quot;advapi32.dll&quot; Alias &quot;RegOpenKeyExA&quot; (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long<br>Private Declare Function RegQueryValueEx Lib &quot;advapi32.dll&quot; Alias &quot;RegQueryValueExA&quot; (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long<br><br>'Defined Keys<br>Private Const HKEY_CLASSES_ROOT = &H80000000<br>Private Const HKEY_CURRENT_USER = &H80000001<br>Private Const HKEY_LOCAL_MACHINE = &H80000002<br>Private Const HKEY_USERS = &H80000003<br>Private Const HKEY_PERFORMANCE_DATA = &H80000004<br>Private Const HKEY_DYN_DATA = &H80000006<br>Private Const HKEY_CURRENT_CONFIG = &H80000005<br><br>'Constant for reading registry key<br>Private Const KEY_QUERY_VALUE = &H1<br><br><br>Private Sub Command1_Click()<br>Dim strServerName As String<br>Dim strSubKey As String<br>Dim strValueName As String<br>Dim lhRemoteRegistry As Long<br>Dim lhRemoteRegistryKey As Long<br>Dim sKeyValueBuffer As String<br>Dim lsKeyValueBufferSize As Long<br><br>'&nbsp;&nbsp;&nbsp;&nbsp;strSubKey = &quot;PerfStats\\StopStat&quot; & Chr(0)<br>&nbsp;&nbsp;&nbsp;&nbsp;strSubKey = &quot;PerfStats\\StartStat&quot; & Chr(0)<br>'&nbsp;&nbsp;&nbsp;&nbsp;strSubKey = vbNullString & Chr(0)<br>'&nbsp;&nbsp;&nbsp;&nbsp;strValueName = &quot;&quot; & Chr(0)<br>&nbsp;&nbsp;&nbsp;&nbsp;strValueName = &quot;KERNEL\\CPUUsage&quot; & Chr(0)<br>&nbsp;&nbsp;&nbsp;&nbsp;strServerName = &quot;\\wts-gr-ws004&quot; & Chr(0)<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;lReturnCode = RegConnectRegistry(strServerName, HKEY_PERFORMANCE_DATA, lhRemoteRegistry)<br>'&nbsp;&nbsp;&nbsp;&nbsp;lReturnCode = RegConnectRegistry(strServerName, HKEY_LOCAL_MACHINE, lhRemoteRegistry)<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Label1.Caption = lReturnCode<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Label6.Caption = lhRemoteRegistry<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>'&nbsp;&nbsp;&nbsp;&nbsp;lReturnCode = RegOpenKeyEx(HKEY_PERFORMANCE_DATA, strSubKey, 0&, KEY_QUERY_VALUE, lhRemoteRegistryKey)<br>&nbsp;&nbsp;&nbsp;&nbsp;lReturnCode = RegOpenKeyEx(lhRemoteRegistry, strSubKey, 0&, KEY_QUERY_VALUE, lhRemoteRegistryKey)<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Label2.Caption = lReturnCode<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;sKeyValueBuffer$ = Space$(10)<br>&nbsp;&nbsp;&nbsp;&nbsp;lsKeyValueBufferSize& = Len(sKeyValueBuffer)<br>&nbsp;&nbsp;&nbsp;&nbsp;lReturnCode = RegQueryValueEx(lhRemoteRegistryKey, strValueName, 0&, 0&, ByVal sKeyValueBuffer, lsKeyValueBufferSize)<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Label3.Caption = lReturnCode<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Text1.Text = sKeyValueBuffer<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;'Close keys<br>&nbsp;&nbsp;&nbsp;&nbsp;lReturnCode = RegCloseKey(lhRemoteRegistryKey)<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Label4.Caption = lReturnCode<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;lReturnCode = RegCloseKey(lhRemoteRegistry)<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Label5.Caption = lReturnCode<br><br>End Sub<br><br>Private Sub Form_Load()<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Label1.Caption = vbNullString<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Label2.Caption = vbNullString<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Label3.Caption = vbNullString<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Label4.Caption = vbNullString<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Label5.Caption = vbNullString<br>&nbsp;&nbsp;&nbsp;&nbsp;Me.Label6.Caption = vbNullString<br>&nbsp;&nbsp;&nbsp;&nbsp;<br><br>End Sub<br><br><br>The form is a standard form with a textbox (text1), command button (Command1) and six labels (label1 to 6) on it, in an arrangement of your choice...
 
If you are talking about on the Client's machine remotely from your server, you have to understand that ASP is executed on the server not on the client. <p>Karl<br><a href=mailto:kb244@kb244.com>kb244@kb244.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
 
OK, now I'm confused...ASP is to do with IIS and all that web lark, how does it fit in here?&nbsp;&nbsp;I'm just trying to connect to the registry of one machine from a different machine and retrieve information from it, I think you might have got the wrong end of the proverbial stick here...this is just a simple .exe, there's no web stuff going on here at all, the two machines are in NT domains and are comunicating (using TCP/IP I'd imagine) across a LAN, not via the internet.<br><br>Cheers for wrapping your brain around my problem, though :)<br><br><br>Paul
 
it would fit , if he's refering to the ActiveX control he's using on ASP. and the fact he's trying to use API through it. <p>Karl<br><a href=mailto:kb244@kb244.com>kb244@kb244.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
 
Hello again,<br><br>&nbsp;&nbsp;OK, things are getting a bit confused here - it's still me - same person - I'm not using ASP in any shape or form, I'm not using any ActiveX controls either, other than the standard controls on my test form - this is purely an API thing, with calls performed through a straight, non-web based VB application. The code I've given really is all the code there is to this, there's no hidden WCI's or anything like that going on here.<br><br>Paul
 
Karl,<br><br>Connecting to a remote PC's registry does not have a *thing* to do with ASP.<br><br>Paul,<br><br>I've struggled with this more than once - have never managed to connect to a remote registry. There's a user called alt255 who might have a thought or two on the matter, I'll email him this evening if he hasn't already seen this...... <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Great, thanks Mike.&nbsp;&nbsp;I think my code's connecting to the remote registry but I can't really tell and I certainly can't extract the info I want out of it...<br><br>Cheerio then,<br><br><br>Paul
 
NFI, did you get the code you needed to access the registry remotely? If not, I have done this and would be willing to share it with you. :)
 
G'day Wabbit,

nope - I tried and tried with this one, but I never got it working properly. I'm *sure* I was connecting to the remote registry, but no matter how I tried it, I couldn't get any information to be passed out.

If you've got any part of this working, then I should be extremely grateful for any help you can give...

Thanks for getting back to me on this one,


All the best,

Paul
 
Hiya,

How does perfmon work? Can I call it's functions as I would an API or other DLL? I really need the raw code so that I can stick it into another application. To tell you the truth, this is all rather academic now, as nearly a year's gone by and the stuff I was working on back then has long since been either finished or forgotten.

I'd be interested in your ideas, though.


I look forward to hearing from you,

Paul
 
I have been able to successfully extract info from the registry with these modifications to your function. You should be able to replace with your key/value names you wish to connect to.
We have a network of over 2000 computers here, so it seems to take about 45 minutes to run against each computer in the domain. I think most this delay has to do with the time-out on Win95 boxes using adsi and the regconnectregistry calls.

Function GetRemoteUser(ByVal computer) As String

Dim strServerName As String
Dim strSubKey As String
Dim strValueName As String
Dim lhRemoteRegistry As Long
Dim lhRemoteRegistryKey As Long
Dim lsKeyValueBufferSize As Long
Dim lReturnCode As Long
Dim strKeyValue As String
Dim sKeyValueBuffer As String

strKeyValue = &quot;&quot;
sKeyValueBuffer = &quot;&quot;
lsKeyValueBufferSize = 0
lhRemoteRegistry = 0
lhRemoteRegistryKey = 0

strServerName = &quot;\\&quot; & computer & Chr(0)
strSubKey = &quot;SYSTEM\CurrentControlSet\Services\NWCWorkstation\Parameters&quot; & Chr(0)
strValueName = &quot;CurrentUser&quot; & Chr(0)

lReturnCode = RegConnectRegistry(strServerName, HKEY_LOCAL_MACHINE, lhRemoteRegistry)
If lReturnCode = 0 Then
lReturnCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strSubKey, 0&, KEY_ALL_ACCESS, lhRemoteRegistryKey)
lReturnCode = RegOpenKeyEx(lhRemoteRegistry, strSubKey, 0&, KEY_ALL_ACCESS, lhRemoteRegistryKey)

'lhRemoteRegistryKey = 920
'lhRemoteRegistryKey = 908
'lhremoteregistrykey = 1076 = computer not found???

'If lhRemoteRegistryKey = 908 Then
'Debug.Print computer
'End If
'If lhRemoteRegistryKey = 920 Then
'Debug.Print computer
'End If
'If lhRemoteRegistryKey = 1076 Then
'Debug.Print computer
'End If
lReturnCode = RegQueryValueEx(lhRemoteRegistryKey, strValueName, 0, REG_SZ, 0, lsKeyValueBufferSize)
If lReturnCode = 2 Then 'Key/Value Not Found &quot;User Not Logged in&quot;
Else
If lReturnCode = ERROR_MORE_DATA Then
sKeyValueBuffer = Space$(lsKeyValueBufferSize)
lReturnCode = RegQueryValueEx(lhRemoteRegistryKey, strValueName, 0, REG_SZ, ByVal sKeyValueBuffer, lsKeyValueBufferSize)
strKeyValue = Left(sKeyValueBuffer, Len(sKeyValueBuffer) - 1)
End If

lReturnCode = RegCloseKey(lhRemoteRegistryKey)
lReturnCode = RegCloseKey(lhRemoteRegistry)
End If
Else
strKeyValue = &quot;Win9x&quot;
End If
GetRemoteUser = strKeyValue
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top