Chapter 3 – Practice 2 solution code
Practice 2 Write a Console application that processes your %Windir%\WindowsUpdate.log file and displays the time, date, and exit code for any rows that list an exit code.
1 /// Make copy since it is open and used by system
2 File.Copy(
3 @"c:\windows\WindowsUpdate.log",
4 @"c:\windows\WindowsUpdate.log_",true );
5 string sf = "";
6 using (
7 FileStream fs = new FileStream(
8 @"c:\windows\WindowsUpdate.log_",
9 FileMode.Open, FileAccess.Read)
10 )
11 {
12 StreamReader sr = new StreamReader(fs);
13 sf = sr.ReadToEnd();
14 }
15 Match m = Regex.Match(sf,
16 @"^(?<date>\d{4}-\d{2}-\d{2})" +
17 @"\s+" +
18 @"(?<time>\d{2}:\d{2}:\d{2}:\d+)" +
19 @".*?exit code\s=\s(?<code>[a-f0-9x]+).*$"
20 , RegexOptions.Multiline | RegexOptions.IgnoreCase);
21 while (m.Success)
22 {
23 Console.WriteLine(
24 "date: " + m.Groups["date"] +
25 "\ttime: " + m.Groups["time"] +
26 "\tcode: " + m.Groups["code"]);
27 m = m.NextMatch();
28 }
How to send email from your perl script?
Suppose you have some script working as a cron job and you want to be informed of its status from time to time. The obvious thing is to use email notifications but as always its not that easy. Especially if you have SSL / TLS protected email provider like gmail. If you have configured local email server then its straightforward solution to just use it. Other wise you will want to use some of the extensions packages. I found that using packages that allow to use email servers like gmail is not easy – it probably requires recompilation of your perl distibution – I’am not going to dive into details here so if you want a quick solution then:
1. Use email provider that is not requiring SSL connection
2. Use Mail::SendEasy
as for 2. below is a simple routine to send email :
use Mail::SendEasy; sub ReportEmail { my $body = shift; $status =1; $cnt=0; do { # in case of errors on the line... my $mail = new Mail::SendEasy( smtp => 'smtp.somwhere.com', port => 587, user => 'username' , pass => 'password' , ) ; $status = $mail->send( from => 'martin@somewhere.com' , from_title => 'script report' , reply => 'martin@somewhere.com' , error => 'martin@somewhere.com' , to => 'my_email@somewhere.com' , subject => "WebSite was hijacked!!!!" , msg => $body, msgid => "0101", ); if (!$status) { # log errors here # print LOG "SendMailError (".$cnt.") ".$mail->error ; $cnt++; } } while($status == 0 && $cnt != 10); }
TypeForwardedTo attribute in action
If you will try to test this attribute as it is described in MCTS Self-Paced Training Kit (70-536) in chapter 1 on pg. 48 then you might get into trouble. I don’t mean it is wrong, but it is missing some intricacies. One is that type being moved must be in the same namespace as the original one. Suppose you have initially:
1.
Application.cs
ClassLibrary1.cs
using System;
namespace ClassLibrary1
{
public class Class1
{
}
}
ClassLibrary1.cs makes into ClassLibrary1.dll and Application.cs into Application.exe. Application.exe references ClassLibrary1.dll and all is working well, until someone decides to clean up code and move ClassLibrary1.Class1 into ClassLibrary2.dll. With the help of TypeForwardedTo you dont have to recompile application.exe, but only
ClassLibrary1.dll and
ClassLibrary2.dll.
Now we have to do below changes:
2.
Application.cs – No! changes here
ClassLibrary1.cs – remember to reference in ClassLibrary1.dll a new library : ClassLibrary2.dll
using System;
using System.Runtime.CompilerServices;
using ClassLibrary2;
[assembly: TypeForwardedTo(typeof(ClassLibrary1.Class1))]
namespace ClassLibrary1
{
public class Class1Ex /// there is no more Class1 type here
{
}
}
ClassLibrary2.cs – New class, this is where Class1 type is being forwarded
using System;
namespace ClassLibrary1 // forwarded type, same as in ClassLibrary1.dll
{
public class Class1
{
}
}
namespace ClassLibrary2
{
public class SomeClass // some dummy class
{
}
}
If you are seeing exceptions running Application.exe then this might mean that you have not defined correctly in ClassLibrary2 a ClassLibrary1.Class1 type.
putty and Ctrl + s that blocks screen
When you press Ctrl + s under putty, then your linux session will freeze and screen will not update. In my case I mostly had to restart my session which was quite annoying even though I use screen. Quicker solution is to press Ctrl + q. This is what many websites/blogs will tell you.
I am writing it here just to remind me of it, since I constantly forget that trick.
Tough choice – XNA or Silverlight ?
According to all the news and second draft from Charles Petzold “Programming Windows Phone 7” book, initially You will not be able to mix those two. If it is a tough decision for You then it means that You need fast rendering which is provided with XNA. What if You have large number of dialogs with lots of controls for changing settings? This is what Silverlight was designed for. The only choice is to start with XNA, and maybe some time later recode all Your UI in Silverlight. This sounds like a nightmare. I know that XNA is mainly designed for games, but what about business applications?
Classes from unnamed (default) package is not visible from class in named package
Well, this is a well known fact for around 10 years now, but I got trapped into compilation error involving this “feature” recently. Here is more detailed explanation:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4361575
to make this story short – always remember to put your classes into packages, and never try to use class from unnamed package from within named package class, compiler will not give you meaningful error notice.
This is only valid for compilers from 1.4 and up. If you have some older class files which were in unnamed package then they are no longer usable – unless you have sources. Well – actually there is a method, use reflection API.
Hello world!
Hello everyone, just moved my WordPress blog from other server, here. I hope to stay here for a longer time.
Configure firefox to use proxy using ssh
If you own some remote server access over ssh, then you can easily tunell all your http request over it. Under linux ssh tool is available by default, under windows its best to install Cygwin with ssh. After that all you have to do is issue following command:
$ ssh -ND localhost:8080 $USER@$REMOTE_SERVER
where localhost:8080 means that ssh will bind to localhost on port 8080, $USER is the name of your user account on remote server $REMOTE_SERVER.
after that you need to configure firefox to. Under firefox : enter Tools->Options->Advanced->Network->Settings, choose manual configuration and fill “Host Socks” with localhost:8080.
and thats all
When intellisense stops working…
It happens that intellisense stops working and you no longer see methods for STL containers and other classes. What is most often advised in such situation is to delete .ncb file, since this is such a huge problem, developers from Microsoft thought of fixing this issue by providing macro:
http://blogs.msdn.com/vcblog/archive/2007/11/19/controlling-intellisense-through-macros.aspx
Compiling synergy on OpenSuse 11.1 x64 with gcc 4.3.2
Here are some hints on how to make synergy work on OpenSuse 11.1.
1. Fix bugs in code that originate from using gcc version 4.3.2, this were all because of missing header files, like cstdlib, memory, string.h. These are no longer being auto added by other general headers and must be added manually.
2. Later on I had to disable compiler errors on warnings. Otherwise I would have to make more code fixes, these is probably also caused by newer version of gcc than the one used by synergy developers.
3. Step 2 – required changes in configure.in, which resulted in errors with autoconf and automake. I was missing aclocal-1.6 and automake-1.6, but I did have newer versions aclocal-1.10 and automake-1.10, so the simple solution was to symlink new binary file names to older file names.
after that synergyc compiled properly, and did properly connect to my synergy server located on windows box.