DCSIMG
March 2007 - Posts - Just code - Tamir Khason

March 2007 - Posts

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/vs2005-sp1-update-for-vista-is-rather-pity/]


I was rather disappointed by the new patch for Visual Studio 2005 SP1 for Windows Vista. WTF, the most significant problems such as re-rendering, JIT and elevated account requirement were addressed, but not solved. So what the patch is really for?

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/single-instance-application-screen-saver-and-power-management-disabling-and-battery-meter/]


The first client's request was to make my WPF application single-instance based. Really simple, remember from WinForms? Named pipes to itself, mutexes, VB approach. First method is rather complicated, and I do not want to put all this code into my app.xaml.cs file. The second one is rather good, but it has security issues, so the only reasonable way to do it is using VB's WindowsFormsApplicationBase approach.

How? Please, before asking me, take a brief look into Windows SDK. One of samples there speaking about it. Anyhow, all you should do is to create Manager, derived from WindowsFormsApplicationBase (only this class has IsSingleInstance property). All other stuff is really straight forward.

Delete your app.xaml (with cs). The create new cs class and put start point there (as well as you do it in 2.0 and 1.1 application

 

public class AppManager
{
     [STAThread]
     public static void Main(string[] args)
     {
           ApplicationManager manager = new ApplicationManager();
           manager.Run(args);
      }
}

Then create new class, derrived from WindowsFormsApplicationBase (don't forget to add reference to Microsoft.VisualBasic) inside the file (or in other file if you wish and override OnStartup

public class ApplicationManager : WindowsFormsApplicationBase
{
   ManagedApplication app;

   public ApplicationManager()
   {
      this.IsSingleInstance = true;
   }

   protected override bool OnStartup    (Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
   {
     app = new ManagedApplication();
     app.Run();
     return false;
   }

protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
  {
     base.OnStartupNextInstance(eventArgs);
     app.Activate();
  }
}

Next create application derived class to wake or run your application

public class ManagedApplication : Application
{
   protected override void OnStartup(System.Windows.StartupEventArgs e)
{
   base.OnStartup(e);

Window1 wnd = new Window1();
wnd.Show();
}

internal void Activate()
{
if (!this.MainWindow.IsVisible)
{
this.MainWindow.Show();
}

if (this.MainWindow.WindowState == WindowState.Minimized)
{
this.MainWindow.WindowState = WindowState.Normal;
}

this.MainWindow.Activate();
this.MainWindow.Focus();
}
}

That's all, folks. Now your application will run only once. Each other instance will wake, restore and activate your program

The next challenge is to prevent screen saver and power management from appear, while my application is running. To do it, we'll have to go deeper into WinProc and make some woodoo on system messages (don't you did it recently)

In you main window subscribe to SourceInitialized to get your application handler (the only handler in WPF application)

public Window1()
{
InitializeComponent();
SourceInitialized += new EventHandler(Window1_SourceInitialized);
}

Then, get handle and hook on it's messages

void Window1_SourceInitialized(object sender, EventArgs e)
{
((HwndSource)PresentationSource.FromVisual(this)).AddHook(myHook);
}

Messages you need is WM_SYSCOMMAND at the beginning, and then SC_SCREENSAVE and SC_MONITORPOWER. Simple, isn't it? The only thing you should remember, that in WPF we can use only 4 low order bits from wParam (read MSDN) so, our hook will looks like this

const int WM_SYSCOMMAND = 0x112;
const int SC_SCREENSAVE = 0xF140;
const int SC_MONITORPOWER = 0xF170;

//we need only 4 low order bits of the wParam
const int lowOrderMask = 0xFFF0;
IntPtr myHook(IntPtr hwnd, int mgs, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (mgs == WM_SYSCOMMAND &&
((((long)wParam & lowOrderMask) == SC_SCREENSAVE) ||
((long)wParam & lowOrderMask) == SC_MONITORPOWER))
{
handled = true;
}
return IntPtr.Zero;
}

Remember, that you are working with laptop and you really need power. So, let's make battery monitor for fun.

In order to get battery status we'll make PInvoke for GetSystemPowerStatus from kernel. I will not explain how to do it (you can see it in the sample attached), but I want to use WPF power to show it. We'll do DependencyObject with a couple of read only dependency properties.

public class Battery : DependencyObject

So, we have one. Let's put timer and get the battery status every second

public Battery()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
}

Now we need read only dependency property. For this we'll have to create DependencyPropertyKey first. Only one, who can access those keys will able to change the value of the property. Everything else is really simple and looks exactly like standard DP.

public static readonly DependencyPropertyKey BatteryStatusPropertyKey = DependencyProperty.RegisterReadOnly
("LineStatus", typeof(ACLineStatus), typeof(Battery),
new UIPropertyMetadata(ACLineStatus.Unknown));

public static DependencyProperty BatteryStatusProperty = BatteryStatusPropertyKey.DependencyProperty;

public ACLineStatus LineStatus
{
get { return (ACLineStatus)GetValue(BatteryStatusProperty); }
}

So, as you can see, we made readonly DP of ACLineStatus tyle and we'll fill it this way SetValue(BatteryStatusPropertyKey, (ACLineStatus)m_status.ACLineStatus); later. We'll can access the key, so we can set values, all others will be able only get.

Next we'll do it for all other properties and make simple graphic indicator for this. This one:

<Border BorderThickness="2" BorderBrush="Black" Height="100">
<Border.Background>
<VisualBrush Stretch="None" AlignmentX="Left">
<VisualBrush.Visual>
<Rectangle Fill="Red" Height="100">
<Rectangle.Width>
<MultiBinding Converter="{StaticResource pcConv}">
<Binding Source="{StaticResource battery}"
Path="BatteryLifePercent"/>
<Binding ElementName="myWindow" Path="Width"/>
</MultiBinding>
</Rectangle.Width>
</Rectangle>
</VisualBrush.Visual>
</VisualBrush>
</Border.Background>
</Border>

Now the rectangle visual as background brush will fill the relevant space according the battery life. We'll have to scale it for window width change, so, we'll bind to Width of out window and BatteryLifePercent readonly DP. The converter do following

class PercCalcConverter:IMultiValueConverter
{
#region IMultiValueConverter Members

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double pr = (double)values[0];
double max = (double)values[1];

double val = pr * max / 100;
return val;
}

That's all, for today, folks. See the attachment with full application source (as usual) with a little surprise inside it. Going sleep now...

Source code for this article

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/wtf-live-messenger-size/]


Anyone, please tell me that it's only UI bug and Live messenger is not really 120MB!!!

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/windows-vista-100-working-keygen/]


Nice title, ah? :) Let's wait for search engines robots to index it. Why this title? 'Cos, according Ben, Windows Vista can be cracked by simple brute-force attack. Yes, I know, this ugly and dumb long, but it's possible. Let's take it up...

Windows Vista key, as well as near all Microsoft's product keys, is build according following pattern: xxxxx-xxxxx-xxxxx-xxxxx-xxxxx. Let's say, that I do not know what character should be and where, so I'm suppose, that we have deal with 25 alphanumeric characters. In English alphabet there is 26 case-insensitive characters and 10 numbers, so we have a deal with 3625 combinations, which about equals to 8 with 38 zeros. So, let's take really quick computer and do bruteforce over UI thread of Windows. In this case we can work with 10ms delay, so we can check 100 combinations a second. Now, in worse case, we'll find a key within 836 seconds, other words, about 90,000,000,000,000,000,000,000,000,000 hours, equals to 313,000,000,000,000,000,000,000,000 years. Pretty long isn't it? BUT there are some rules, discovered a couple of month ago, so, according them, we can minimize the number of possibilities to about 815. For some reason, I will not explain how, but in this case, even using UI validation and our brute-force attack, we can find the working key within about 66 years. BUT (another one) if we'll work without UI with a couple of hardcore computers, with a bit of luck and 6 million of China vendors, we'll find a golden key rather fast. So, should Microsoft be worry about this point? Maybe, within the next couple of years :)

Someone want to build such key generator?

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/good-icons-editor/]


Lack of VS icon editor? You are not alone. Recently I looked for freeware program to create and edit icons and I found one. Let me introduce IcoFX. Sufficient features and useful interface. I was disappointed, that the program has installer, but it can run from Disk-On-Key as well (either open msi and pick it out or install-copy exe-uninstall)

Here is the list of this soft features:

  • Vista support
  • Alpha channel support
  • Bach processing
  • Some effects (not really useful)
  • Support for icons upto 255X255X32bit
  • Import/Export (from assemblies as well with transparency support)
  • Blur or Sharp brush edges (this one is extremely important for icons creation)
  • Rotatings
  • File types conversion (with PNG support)

The really missing future - stroke, but anyhow, the program is great. Download and use it

More Posts « Previous page