<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: computer code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 00:53:49 GMT</pubDate>
    <description>DZone Snippets: computer code</description>
    <item>
      <title>Get uptime of remote computer using WMI</title>
      <link>http://snippets.dzone.com/posts/show/5472</link>
      <description>// description of your code here&lt;br /&gt;Get uptime of remote computer using WMI, I'm not sure the convert time function is correct, it gives different results than psinfo.exe&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;Private Function GetUptime() As String&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        Dim WMIScope As Management.ManagementScope&lt;br /&gt;        Dim WMIConnectionOptions As New Management.ConnectionOptions&lt;br /&gt;        Dim strRootDomain As String&lt;br /&gt;        Dim objRootLDAP As DirectoryEntry&lt;br /&gt;        Dim query As ManagementObjectSearcher&lt;br /&gt;        Dim queryCollection As ManagementObjectCollection&lt;br /&gt;        Dim oq As New System.Management.ObjectQuery&lt;br /&gt;        Dim mo As New ManagementObject&lt;br /&gt;        Dim perftimestamp As String = ""&lt;br /&gt;        Dim perttimefreq As String = ""&lt;br /&gt;        Dim counter As String = ""&lt;br /&gt;        Dim iUptimeInSec As Long = 0&lt;br /&gt;&lt;br /&gt;        With WMIConnectionOptions&lt;br /&gt;            .Impersonation = System.Management.ImpersonationLevel.Impersonate&lt;br /&gt;            .Authentication = System.Management.AuthenticationLevel.Packet&lt;br /&gt;&lt;br /&gt;        End With&lt;br /&gt;&lt;br /&gt;        WMIScope = New Management.ManagementScope("\\" &amp; _&lt;br /&gt;        Strcomputer &amp; "\root\cimv2", WMIConnectionOptions)&lt;br /&gt;&lt;br /&gt;        objRootLDAP = New DirectoryEntry("LDAP://RootDSE")&lt;br /&gt;        strRootDomain = objRootLDAP.Properties.Item("rootDomainNamingContext").Value.ToString&lt;br /&gt;&lt;br /&gt;        oq = New System.Management.ObjectQuery("select * from Win32_PerfRawData_PerfOS_System")&lt;br /&gt;        query = New ManagementObjectSearcher(WMIScope, oq)&lt;br /&gt;        queryCollection = query.Get()&lt;br /&gt;&lt;br /&gt;        For Each oReturn As ManagementObject In queryCollection&lt;br /&gt;            perftimestamp = oReturn("Timestamp_Object").ToString()&lt;br /&gt;            perttimefreq = oReturn("Frequency_Object").ToString()&lt;br /&gt;            counter = oReturn("SystemUpTime").ToString()&lt;br /&gt;        Next&lt;br /&gt;&lt;br /&gt;        ' Calculation in seconds:&lt;br /&gt;        iUptimeInSec = Convert.ToInt64((Double.Parse(perftimestamp) - Double.Parse(counter)) / Double.Parse(perttimefreq))&lt;br /&gt;&lt;br /&gt;        ' convert the seconds&lt;br /&gt;        Return ConvertTime(iUptimeInSec)&lt;br /&gt;&lt;br /&gt;    End Function&lt;br /&gt;&lt;br /&gt;    Private Function ConvertTime(ByVal seconds As Long) As String&lt;br /&gt;&lt;br /&gt;        Dim ConvSec As Long&lt;br /&gt;        Dim ConvMin As Long&lt;br /&gt;        Dim ConvHour As Long&lt;br /&gt;        Dim ConvDays As Long&lt;br /&gt;&lt;br /&gt;        Math.DivRem(seconds, 60, ConvSec)&lt;br /&gt;&lt;br /&gt;        Math.DivRem(seconds, 3600, ConvMin)&lt;br /&gt;        ConvMin = ConvMin / 60&lt;br /&gt;&lt;br /&gt;        Math.DivRem(seconds, (3600 * 24), ConvHour)&lt;br /&gt;        ConvHour = ConvHour / 3600&lt;br /&gt;&lt;br /&gt;        ConvDays = (seconds / (3600 * 24))&lt;br /&gt;&lt;br /&gt;        Return (ConvDays.ToString() + " days " + ConvHour.ToString() + " hours " + ConvMin.ToString() + " minutes " + ConvSec.ToString() + " seconds ")&lt;br /&gt;&lt;br /&gt;    End Function&lt;br /&gt;&lt;br /&gt;'or you could try this method which still doesn't match psinfo or systeminfo but it is closer&lt;br /&gt;&lt;br /&gt;Private Function GetUptime2() As String&lt;br /&gt;&lt;br /&gt;        Dim WMIScope As Management.ManagementScope&lt;br /&gt;        Dim WMIConnectionOptions As New Management.ConnectionOptions&lt;br /&gt;        Dim strRootDomain As String&lt;br /&gt;        Dim objRootLDAP As DirectoryEntry&lt;br /&gt;        Dim query As ManagementObjectSearcher&lt;br /&gt;        Dim queryCollection As ManagementObjectCollection&lt;br /&gt;        Dim oq As New System.Management.ObjectQuery&lt;br /&gt;        Dim mo As New ManagementObject&lt;br /&gt;        Dim perftimestamp As String = ""&lt;br /&gt;        Dim perttimefreq As String = ""&lt;br /&gt;        Dim counter As String = ""&lt;br /&gt;        Dim iUptimeInSec As Long = 0&lt;br /&gt;        Dim LastBootUpTime As DateTime&lt;br /&gt;        Dim dateNow As DateTime = Date.Now&lt;br /&gt;&lt;br /&gt;        With WMIConnectionOptions&lt;br /&gt;            .Impersonation = System.Management.ImpersonationLevel.Impersonate&lt;br /&gt;            .Authentication = System.Management.AuthenticationLevel.Packet&lt;br /&gt;&lt;br /&gt;        End With&lt;br /&gt;&lt;br /&gt;        WMIScope = New Management.ManagementScope("\\" &amp; _&lt;br /&gt;        Strcomputer &amp; "\root\cimv2", WMIConnectionOptions)&lt;br /&gt;&lt;br /&gt;        objRootLDAP = New DirectoryEntry("LDAP://RootDSE")&lt;br /&gt;        strRootDomain = objRootLDAP.Properties.Item("rootDomainNamingContext").Value.ToString&lt;br /&gt;&lt;br /&gt;        oq = New System.Management.ObjectQuery("Select * from Win32_OperatingSystem")&lt;br /&gt;&lt;br /&gt;        query = New ManagementObjectSearcher(WMIScope, oq)&lt;br /&gt;        queryCollection = query.Get()&lt;br /&gt;&lt;br /&gt;        For Each oReturn As ManagementObject In queryCollection&lt;br /&gt;&lt;br /&gt;            LastBootUpTime = System.Management.ManagementDateTimeConverter.ToDateTime(oReturn("LastBootUpTime").ToString)&lt;br /&gt;            &lt;br /&gt;        Next&lt;br /&gt;&lt;br /&gt;        Dim ts As TimeSpan = dateNow.Subtract(LastBootUpTime)&lt;br /&gt;        Console.WriteLine(ts.Days.ToString + " days " + ts.Hours.ToString + " hours " + ts.Minutes.ToString + " minutes " + ts.Seconds.ToString + " seconds ")&lt;br /&gt;&lt;br /&gt;    End Function&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 07 May 2008 15:20:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5472</guid>
      <author>mellerbeck (Michael Ellerbeck)</author>
    </item>
    <item>
      <title>Cantor's Sets with Processing</title>
      <link>http://snippets.dzone.com/posts/show/5102</link>
      <description>This simple Processing programme generates the &lt;a href="http://en.wikibooks.org/wiki/Fractals/Cantor's_Set"&gt;Cantor's Sets&lt;/a&gt; fractal.&lt;br /&gt;&lt;br /&gt;Images are generated randomly, but tend to look like &lt;a href="http://scvalex.deviantart.com/art/Cantor-s-Sets-76451637"&gt;this&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;int setHeight = 60;&lt;br /&gt;color setColour = color(184 + random(-40, 40),&lt;br /&gt;                        124 + random(-40, 40),&lt;br /&gt;                        124 + random(-40, 40));&lt;br /&gt;int wid = 600;&lt;br /&gt;&lt;br /&gt;void setup() {&lt;br /&gt;  size(wid, (int)((log(wid) / log(3) + 1) * (setHeight + 5)));&lt;br /&gt;  background(0);&lt;br /&gt;  smooth();&lt;br /&gt;  &lt;br /&gt;  cantorSet(10, 10, width - 20, setColour);&lt;br /&gt;  &lt;br /&gt;  save("cantorSet.png");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;float rnd(float x) {&lt;br /&gt;  return x + random(-20, 20);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void brushRect(int x, int y, int w, int h) {&lt;br /&gt;  for (int i = 0; i &lt; h; ++i) {&lt;br /&gt;    float r = random(3);&lt;br /&gt;    strokeWeight(r);&lt;br /&gt;    float downpull = random(h / 4);&lt;br /&gt;    float shudder = random(-2, 2);&lt;br /&gt;    line(x + shudder, y + i, x + w - shudder, y + i + downpull);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void cantorSet(int y, int offX, int wid, color col) {&lt;br /&gt;  if ((y &gt; height - 10) || (wid &lt; 1))&lt;br /&gt;    return;&lt;br /&gt;&lt;br /&gt;  stroke(col);&lt;br /&gt;  brushRect(offX, y, wid, setHeight);&lt;br /&gt;  //fill(col);&lt;br /&gt;  //rect(offX, y, wid, setHeight);&lt;br /&gt;  &lt;br /&gt;  color newCol = color(rnd(red(col)), rnd(green(col)),&lt;br /&gt;                      rnd(blue(col)));&lt;br /&gt;  cantorSet(y + setHeight + 5, offX, wid / 3, newCol);&lt;br /&gt;  cantorSet(y + setHeight + 5, offX + wid*2/3, wid / 3, newCol);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 04 Feb 2008 11:08:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5102</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Who pooped on my keyboard</title>
      <link>http://snippets.dzone.com/posts/show/4487</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// insert code here..&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;somebody pooped on my keyboard</description>
      <pubDate>Mon, 03 Sep 2007 22:18:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4487</guid>
      <author>poopman (Steven Joseph Adams )</author>
    </item>
  </channel>
</rss>
