Tuesday, March 13, 2007

Dell story - part II

Today sales manager from the distributor store called me to notice, that I can come there and get my upgrade to Windows Vista, arrived from Dell. The only thing he mentioned in our conversation is fee of $45. I asked him what I'm paying for, and he told me that is's for shipping and handling. The same sentence, I found in Dell Vista Upgrade FAQ web site, but if I'll come to take it, it's seemed him, that I should not pay anything. 

A minute later, he called me back, and told, that I should pay for it anyway. After my request to explain he told me, that those $45 actually they'll pay to Microsoft and don't earn a penny for it. So he do not really know what I should pay for. 

The upgrade price for Windows Vista Home Basic is around $100, complete package costs around $200 (including shipping and handling), so the question is what's for I'm going to pay a half of regular licence price, when my system is eligible and the licence for Windows XP Pro SP2 already paid? I'll check this next Sunday.

Monday, March 12, 2007

Text length measurement? It's really easy with WPF

Don't you remember how it was to measure the length of your text in pixels? Do you really remember methods named GetLineFromCharIndex, Bounds and MeasureText?

Let's do it in WPF:

TextBox myText = new TextBox();
Rect textRext = myText.GetRectFromCharacterIndex(myText.Text.Length);

That's all, folks. Now you have the boundaries of your text in pixels. You can even measure the size of text up to your cursor position by using CaretIndex property of TextBox. Don't it really easy?

 

<Window x:Class="TextMeasurement.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:m="clr-namespace:TextMeasurement"
        Name="myWindow" 
    Title="TextMeasurement" Height="300" Width="300"
    >
  <StackPanel>
    <StackPanel Orientation="Horizontal">
      <TextBlock>The text length is:  </TextBlock>
      <TextBlock Name="mySize" Text="{Binding ElementName=myWindow, Path=TextSize}"/>
    </StackPanel>
    <TextBox Name="myText" TextChanged="onChanged"/>
  </StackPanel>
</Window>

 


public partial class Window1 : System.Windows.Window

{

    public static readonly DependencyPropertyKey TextSizePropertyKey = DependencyProperty.RegisterReadOnly("TextSize",

        typeof(double), typeof(Window1),

        new UIPropertyMetadata((double)0));

    public static DependencyProperty TextSizeProperty = TextSizePropertyKey.DependencyProperty;

    public double TextSize

    {

        get { return (double)GetValue(TextSizeProperty); }

    }

 

 

    public Window1()

    {

        InitializeComponent();

 

    }

 

    void onChanged(object sender, TextChangedEventArgs e)

    {

        Rect textRext = myText.GetRectFromCharacterIndex(myText.Text.Length);

 

        SetValue(TextSizePropertyKey, textRext.Right);

    }

 

}

 

Have a nice day with WPF :)

Sunday, March 11, 2007

How to use IE authentication programmatically

So, there are two methods deals with credential dialog of IE

CredUIPromptForCredentials – for pop it up and CredUIConfirmCredentials for persist those settings in password manager (optional). So, in order to call it explicitly, you should implement them via P/Invoke like this

[DllImport("credui.dll", EntryPoint = "CredUIConfirmCredentialsW", CharSet = CharSet.Unicode)]

       private static extern CredUIReturnCodes CredUIConfirmCredentials(string targetName, [MarshalAs(UnmanagedType.Bool)] bool confirm);

[DllImport("credui.dll", EntryPoint = "CredUIPromptForCredentials", CharSet = CharSet.Ansi)]

       private static extern CredUIReturnCodes CredUIPromptForCredentials(ref CREDUI_INFO creditUR,

                                                                                                                       string targetName,

                                                                                                                       IntPtr reserved1,

                                                                                                                       int iError,

                                                                                                                       StringBuilder userName,

                                                                                                                       int maxUserName,

                                                                                                                       StringBuilder password,

                                                                                                                       int maxPassword,

                                                                                                                       [MarshalAs(UnmanagedType.Bool)] ref bool pfSave,

                                                                                                                       CREDUI_FLAGS flags);

Please pay attention, that you’ll need CREDI_INFO structure and CREDI_FLAG and CredUIReturnCodes enums, that those function using

public struct CREDUI_INFO

{

   public int cbSize;

   public IntPtr hwndParent;

   public string pszMessageText;

   public string pszCaptionText;

   public IntPtr hbmBanner;

}

[Flags]

enum CREDUI_FLAGS

{

   INCORRECT_PASSWORD = 0x1,

   DO_NOT_PERSIST = 0x2,

   REQUEST_ADMINISTRATOR = 0x4,

   EXCLUDE_CERTIFICATES = 0x8,

   REQUIRE_CERTIFICATE = 0x10,

   SHOW_SAVE_CHECK_BOX = 0x40,

   ALWAYS_SHOW_UI = 0x80,

   REQUIRE_SMARTCARD = 0x100,

   PASSWORD_ONLY_OK = 0x200,

   VALIDATE_USERNAME = 0x400,

   COMPLETE_USERNAME = 0x800,

   PERSIST = 0x1000,

   SERVER_CREDENTIAL = 0x4000,

   EXPECT_CONFIRMATION = 0x20000,

   GENERIC_CREDENTIALS = 0x40000,

   USERNAME_TARGET_CREDENTIALS = 0x80000,

   KEEP_USERNAME = 0x100000,

}

public enum CredUIReturnCodes

{

   NO_ERROR = 0,

   ERROR_CANCELLED = 1223,

   ERROR_NO_SUCH_LOGON_SESSION = 1312,

   ERROR_NOT_FOUND = 1168,

   ERROR_INVALID_ACCOUNT_NAME = 1315,

   ERROR_INSUFFICIENT_BUFFER = 122,

   ERROR_INVALID_PARAMETER = 87,

   ERROR_INVALID_FLAGS = 1004,

}

Now, when you have those calls, you may want to make some wrappers (like those)

const int MAX_USER_NAME = 100;

const int MAX_PASSWORD = 100;

const int MAX_DOMAIN = 100;

static CredUIReturnCodes PromptForCredentials(ref CREDUI_INFO creditUI, string targetName, int netError, ref string userName, ref string password, ref bool save, CREDUI_FLAGS flags)

{

                         StringBuilder user = new StringBuilder(MAX_USER_NAME);

                         StringBuilder pwd = new StringBuilder(MAX_PASSWORD);

                         creditUI.cbSize = Marshal.SizeOf(creditUI);

                         CredUIReturnCodes result = CredUIPromptForCredentials(ref creditUI, targetName,IntPtr.Zero, netError,user, MAX_USER_NAME, pwd, MAX_PASSWORD, ref save, flags);

                                                  userName = user.ToString();

                                                  password = pwd.ToString();

                         return result;

}

CredUIReturnCodes AskCredit(ref string username, ref string password)

{

                         string host = url.Host;

                         CREDUI_INFO info = new CREDUI_INFO();

                         info.pszCaptionText = host;

                         info.pszMessageText = "The server "+host+" requires a username and password. \r\n\r\n\r\nWarning: This server is requesting that your username and password be sent in an insecure manner (basic                                                                            authentication without a secure connection).";

                         CREDUI_FLAGS flags = CREDUI_FLAGS.GENERIC_CREDENTIALS |

                                                  CREDUI_FLAGS.SHOW_SAVE_CHECK_BOX |

                                                  CREDUI_FLAGS.ALWAYS_SHOW_UI |

                                                  CREDUI_FLAGS.EXPECT_CONFIRMATION;

                         bool savePwd = false;

                         CredUIReturnCodes result = PromptForCredentials(ref info, host, 0, ref username,

                         ref password, ref savePwd, flags);

return result;

}

After it, you should call AskCredit to get the credit window pop up and get secured username and password from them. Something like this

CredUIReturnCodes code = AskCredit(ref user, ref pwd);

                         if (code == CredUIReturnCodes.NO_ERROR)

                         {

//Do something useful

                         }

The next part is to provide those credentials to your HTTPRequest. See the sample doing that

//create some string to get response into

string str = string.Empty;

HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;

HttpWebResponse res = null;

//while you don’t OK keep trying

while (res == null || res.StatusCode != HttpStatusCode.OK)

{

                         try

                         {

//you are fine, do the rest

                         res = req.GetResponse() as HttpWebResponse;

                         using (Stream s = res.GetResponseStream())

                         {

                                                  using (StreamReader sr = new StreamReader(s))

                                                  {

                                                                           str = sr.ReadToEnd();

                                                                           sr.Close();

                                                  }

                         s.Close();

                         }

                         res.Close();

                         }

//You got an exception

                         catch (WebException e)

{

//If this exeption is not security one, you have nothing to do, the only thing is, maybe, check you network connection

                         if (e.Status == WebExceptionStatus.ProtocolError)

                                                  {

                                                                           res = e.Response as HttpWebResponse;

//Now check what the problem and if it’s security, threat it

                                                                           if (res.StatusCode == HttpStatusCode.Unauthorized)

                                                                           {

                                                                                                    string user = "";

                                                                                                    string pwd = "";

//pop you window and get information

                                                                                                    CredUIReturnCodes code = AskCredit(ref user, ref pwd);

                                                                                                    if (code == CredUIReturnCodes.NO_ERROR)

                                                                                                    {

//create new credential set and use it as required

                                                                                                                             CredentialCache creds = new CredentialCache();

                                                                                                                             creds.Add(url, "Basic", new NetworkCredential(user,pwd));

                                                                                                    //creds.Add(url, "Digest", new NetworkCredential(user, pwd));

                                                                                                    //creds.Add(url, "Negotiate", CredentialCache.DefaultNetworkCredentials);

                                                                                                                             req.Credentials = creds;

                                                                                                    }

                                                                           }

                                                  }

                         }

}

Well done. Now you have an application, that pops Internet Explorer authentication window and optionally saves your passwords into security password store within the browser

Israel? It's beside our propose... The Dell story

I got my new Dell Latitude D820 a couple of month ago (after the announcement about Dell's Express Upgrade to Windows Vista. Right after I got it, I checked the site with the computer service tag to be sure eligible for upgrade program. So, I was. Working fluently with my new Dell with Beta and RTM versions (first internals and then MSDN) I was really happy (the first thing I did, after purchase is to format disks with pre-installed Windows XP Pro SP2, which I paid for). So, some way after the official announcement about Windows Vista release, I called the retailer, I purchased the laptop to ask about upgrade. The first answer was: "We do not know anything about it, please contact local Dell office for additional information".

I called them and, sure, was redirected to distributor. I sent a message (using my google account) to Dell's local office and wait for about a month for them to reply, that I never got. So, after another call, I sent another mail (this time using my work address in Microsoft) with the same question. Hurrah!, I got an answer from Shlomy Quarter from Dell as following (hebrew):

 

תמיר שלום,

מדיניות השדרוג אינה חלה על ישראל בשלב זה, מכיוון שהמידע לא הגיע אליך במעמד הרכישה חברת אומניטק תשדרג לך תוך כדי חיוב בדמי המשלוח בלבד כפי שמתוארים באתר.

אנא פנה אל מאיר או עמית בחברת אומניטק בנושא.

Best Regards,

                   Shlomy

Wow, why you, Shlomo, did not sent me the same email to @gmail.com account? The regular client, paid for new Dell computer is not important enough? The client should come from big company to get any response for his after-purchase query?

A couple of hours after this email, I checked another time my Dell service tag for eligibility of the upgrade. Sure it was not, but I have a screenshot a month ago, where the same service tag was eligible. Strange, don't you think? Another computer, purchased earlier in US is still eligible system for this upgrade, I tell you more, I got a disk with an upgrade to my Israel postal address about two weeks after global lunch of Windows Vista.

So, according Dell, all those who purchased new Dell computer here, is Israel are really different customers, then all those who purchased in USA? Don't this seemed a bit strange, or, maybe Dell is really different here? Don't them? All of us are beside there propose, or not?

P.S. I'm really do not need this upgrade, due? I have both internals and MSDN versions, but the Principe is matter... 

Thursday, March 08, 2007

UAC strikes again - flash fix for IE

Until recently, we all saw flash content from Vista in IE, however about a week ago we do not. What's the problem? The new version of Macromedia Flash Player, that do not leaves well with Vista's User Account Control (UAC). So how to fix it?

Great thank to Frank, that investigated the problem and found the solution. You should authorize flash installer to run with elevated rights. How to do it?

Go to "Your Windows Directory\System32\Macromed\Flash", right click FlashUtil9b.exe and select "Run as administrator". The program will install new flash player with elevated security settings.

How you can see any flash content. So, what do you think, UAC still good or bad for Jews?

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?

Tuesday, March 06, 2007

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