Tuesday, May 24, 2005

... or is it Superman?

The kind folks, who administer MSDN webcasts, sent me as one of the year 2005 presenters a shirt and a ... cape. Yes, a red cape.

5/24/2005 11:42:31 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0]  | 
 Wednesday, April 20, 2005

I've been asked for assistance with the following issue today. A developer has a CAB that he wants to install on the device without using CeAppMgr, from his own setup application. In addition he wanted to force the installation to the storage card if present.

This task involves several steps:

  1. Copy CAB to the device
  2. Detect storage card presence
  3. Launch wceload.exe to install the cab

1. Copying CAB to the device.

I would use cecopy (from Windows Mobile Developer Power Toys) or RAPI (CeCreateFile, CeWriteFile). If working with managed code, I suggest OpenNETCF.Communications library, or RapiDeploy tool

2. Detecting storage card presence.

While on the device side the preferred method is to use FindFirstFlashCard/FindNextFlashCard, these functions do not have Rapi equivalent. From the desktop side use CeFindAllFiles/FAF_FOLDERS_ONLY (or CeFindFirstFile/CeFindNextFile) to search the root directory and enumerate all files for having FILE_ATTRIBUTE_DIRECTORY and FILE_ATTRIBUTE_TEMPORARY combination of attributes (0x110).

3. Launch wceload.exe to install the cab

This is the interesting part. Here is a list of command-line switches that wceload.exe supports:

  • /delete - if value = 0 do not delete cab after install
  • /noui - perform a silent operation. Do not ask if it is ok to overwrite the exiting files
  • /nouninstall - do not create a .uninstall file. The applciation entry will not appear in Remove Programs list
  • /askdest - will force wceload to display a dialog that allows user to select installaltion location and some other things
  • /noaskdest - install specified applications to specified locations (see below)

 


Dialog displayed by wceload when /askdest is specified

The /noaskdest switch is the most inetersting of them all. When you specify it, wceload ignores the rest of the command line. Instead it checks the following registry location - [HKEY_LOCAL_MACHINE\SOFTWARE\Apps\Microsoft Application Installer\Install]

The key contents are key/value pairs:
[CAB file path] = [CAB destination directory]
e.g. \Storage Card\MyApp.CAB = \Storage Card\Program Files\My App

wceload will try to install the cab specified in the value name to the location specified in the value value. Below is the sample registry content:

To reiterate: in order to install a cab to the memory card, one needs to:

  1. Copy CAB to the device (e.g. to the memory card)
  2. Create a registry value on the device specifying the cab location and cab destination
  3. Launch wceload.exe with /noaskdest parameter
4/20/2005 5:52:05 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [5]  | 
 Wednesday, March 09, 2005

Preface:
===========================
The President: C'mon, let me nuke that bastard.
Commander Gilmour: Are you suggesting that we blow up the moon?
The President: Would you miss it?
[looks around the table]
The President: Would you miss it?
============================ [Austin Powers, The Spy, Who Shagged Me] =========

The programming world has its panties in uproar. In just over two weeks Microsoft ends the support cycle of the VB6 and VBA products. A group of MVPs has initiated a petition asking Microsoft to stop this insanity. Not only they want for MS to rescind the end-of-support decision, but they actually demand continued development and, unbelievably, integration of VB6 into Visual Studio product alongside with .NET languages. I probably would not be able to state my opinion on this better than Geoff Appleby did, so I'll just say nothing. Except of maybe this.

What support? The VB6 support has been lately done by the MVPs - the people who have drafted and  signed the aforementioned petition. They are absolutely free to continue to do so. The future security patches will necessarily cover the VB components. But no new development (see preface).

I am not a VB-hater (does such category even exist?). But I know from my own newsgroup support experience the amount of harm a loose, poorly structured language like VB brings. I wrote my share of VB6 code and the company I work for uses it extensively, but I moved on and the company is moving on. Hey, I love my car, but I realize one day I will have to buy a new car.

Get over it. And learn some new tricks along the way. World does not stand in place. It moves along.

3/9/2005 11:14:57 PM (Pacific Standard Time, UTC-08:00)  #    Comments [26]  | 
 Thursday, February 24, 2005

The Toolbar control provided as a part of Compact Framework v1.0 does not support tooltips (they are shown when you tap and hold toolbar button). Here is how to add this missing functionality.

First of all, let's see how the tooltips are added. The proper way to do it is to create a tooltip control and pass its handle to the TB_SETTOOLTIPS message as wParam. This sounds pretty painful if we were to do this by means of CF. Fortunately there is an easier way listed in the documentation as legacy but supported all the way through CE 5.0. You can send TB_SETTOOLTIPS passing an array of tooltip strings as lParam and string count as wParam and the toolbar control will create a tooltip control for you. Finally one has to remember to modify toolbar style to include TBS_TOOLTIP and do all of the above before adding the buttons to the toolbar.

Armed with this knowledge we start with defining a few P/Invoke functions:


[DllImport("coredll")]

extern static IntPtr GetCapture();

[DllImport("coredll")]

extern static IntPtr LocalAlloc(int flags, int size);

[DllImport("coredll")]

extern static IntPtr LocalFree(IntPtr p);

[DllImport("aygshell")]

extern static IntPtr SHFindMenuBar(IntPtr hwnd);

[DllImport("coredll")]

extern static int SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);

[DllImport("coredll")]

extern static int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

const int TB_SETTOOLTIPS = (WM_USER + 81);

const int TB_SETSTYLE = (WM_USER + 56);

const int TB_GETSTYLE = (WM_USER + 57);

const int TBSTYLE_TOOLTIPS = 0x0100;

const int WM_USER = 0x0400;

Now, that we are almost ready to set tooltips, there is one final step left. The control expects to receive an array of string pointers. The Compact Framework marshaller is not capable of creating one. Instead we write a special piece of code that would marshal an array of strings into a block of unmanaged memory.

To add tootlips we create a string array, convert it into unmanaged array and then use it as the LPARAM when sending a TB_SETTOOLTIPS message. Keep in mind that the tooltip control will expect this memory to be preserved throught the application lifetime. Release it when the form is closed (or the tooltips are changed).

private IntPtr m_pLabels;

private string[] m_labels = new string[]
{ "Button1", "Button2", "Another button" };

SendMessage(hWndToolbar, TB_SETSTYLE, 0, SendMessage(hWndToolbar, TB_GETSTYLE, 0, 0) | TBSTYLE_TOOLTIPS);

m_pLabels = AllocateStringArray(m_labels);

SendMessage(hWndToolbar, TB_SETTOOLTIPS, m_labels.Length, m_pLabels);

The end result looks like this:

The sample code can be found here.

2/24/2005 7:14:38 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0]  | 
 Wednesday, February 09, 2005

There is a common technique of obtaining the hWnd of various controls in Compact Framework applications to use with native Win32 APIs. It involves setting a Capture property to true and then using GetCapture() to get the handle:

txtUser.Capture = true;
IntPtr hWndUser = GetCapture();
txtUser.Capture = false;

Suprisingly this does not work on Smartphone. It gets you a handle alright, but the handle seems to be wrong. It is wrong indeed. The reason is that on the Smartphone the native Edit control (wrapped by the TextBox) is hosted inside another child window. This has to do with the Smartphone navigation. When you set TextBox.Capture to true, the outer control gets Captrure and as a result, it's the outer control, of which you get the handle. Since we know that the outer control has just one child, we can see our way from this quandary.

txtUser.Capture = true;
IntPtr hWndUser = GetCapture();
hWndUser = GetWindow(hWndUser, GW_CHILD);
txtUser.Capture = false;

//GW_CHILD = 5;

The required PInvoke definitions are parts of Win32Window class in OpenNETCF SDF

Note: This is applicable to CF v1. I have a reason to believe that in v2 things are done differently

2/9/2005 12:24:05 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0]  | 

A question popped up. Let's say we have an application \Program Files\MyApp\MyApp.exe that references a class library \Program Files\MyApp\Framework\MyLib.dll. How can we avoid a TypeLoadException in this scenario?

To answer this let's take a look at how the type resolution works in CF. When an application code attempts to load type T, it first checks if the assembly that contains the type (the one referenced in the Type's full name) is already loaded in the current AppDomain. Obviously, if the appdomain already has the assembly, there is no reason to perform a costly file operation lookihng it up and loading it again. This suggests an easy way to “help” the loader to resolve a type. All you need to do is preload the assembly before the code attempts to use the type from that assembly. In our scenario the following code need to be made:

System.Reflection.Assembly.LoadFrom(@”\Program Files\MyApp\Framework\MyLib.dll”);

This will ensure that the types that belong to this class library are successfully resolved.

The next question is - when to load the assembly. The easy answer is - to play it safe, load all such assemblies in the Main, before the Application.Run. This approach has a disadvantage - a noticeable performance hit because a number of modules are being loaded in the memory before the UI started painting. Besides some of them might be never used. Because of this I would advise staggered load. The trick is to make sure the appropriate assembly is loaded before code execution has entered a block that defines/instantiates a variable of a type defined in that assembly. For example if you have a function:

void DoSomething()
{
MyType myVar = new MyType();
}

where MyType is defined in a dynamically-loaded assembly, and your code never ever call this function, there is no reason at all to load the assembly (provided the no type from that assembly is ever used outside that function).

By cleverly structuring your code you can avoid performance hit even if you have a large amount of dynamically loaded assemblies

2/9/2005 12:15:44 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0]  | 
 Tuesday, January 18, 2005

Today in the Compact Framework public newsgroup I spotted a code snippet (seemed like a piece of a newsreader application) where a variable was called neueNachricht. Try doing something like that in English. I don't think so.

Code | Computers | Life
1/18/2005 11:00:24 AM (Pacific Standard Time, UTC-08:00)  #    Comments [0]  | 
 Wednesday, January 12, 2005

I was walking around CES floor and found a booth around which a long line of people was circling. At the head of the line a scruffy guy with curly hair and a short beard was signing photos. I asked someone who already got a picture, “is that supposed to be someone famous?”. The guy sputtered and said, “Of course. It’s Weird Al Yankovich

Then I went to the next booth and spotted a much shorter line with a cute chick that was signing posters, on which she modeled in some audio hardware ad. A few chinese guys were even photographing each other with her (in a “posessive” way). I decided to get a signed poster for coworker. When it was my turn, she asked who to sign it for. I asked if this was necessary. She was kind of taken aback and said that everyone wanted it signed. I agreed and gave the guy’s name. Then I asked her if that was her in the poster. She pouted and said, of course, why else would she sign them. I brought it back and it was an instant success. I became suspicious and asked “Is that supposed to be someone famous?”. The guy sputtered and said, of course, she was in those series - and then he couldn’t remember which ones. And then in a couple of hours it struck me - she was Lori Loughlin - “Becky”, “Jesse’s” wife in Full House - series that my daughter loves. Now my daughter can’t forgive me that it’s not she who got the poster.

1/12/2005 4:48:40 PM (Pacific Standard Time, UTC-08:00)  #    Comments [26]  | 
 Sunday, December 19, 2004

Q: When I append text to the end of the current content of a multiline listbox, it is always scrolled back to the first line. Even if I later use ScrollToCaret to return to the end of the text, the whole control briefly flickers. Is there a way to avoid it?

A: Indeed there seems to be an issue in Compact Framework where replacing selection in an edit control. Apparently someone wanted to be clever and even though there is a dedicated control message EM_REPLACESEL, he implemented set_SelectedText in the following way:

string SelectedText
{
   
get
   
{
       
if
( SelectionLength == 0 )
           
return
"";
       
return new string
(Text.ToCharArray(), SelectionStart, SelectionLength);
    }
   
set
   
{
       
this.Text = this.Text.Remove(SelectionStart, SelectionLength).Insert(SelectionStart, value
);
        SelectionStart =
this
.Text.Length;
        SelectionLength = 0;
    }
}

 

Why is it done this way, I don't know. There are perfectly good tools at the edit control message level to work with selection. What's important here is that set_SelectedText internally calls Control.set_Text which translates into WM_SETTEXT message. In case of an edit control it has a side effect of moving caret to the beginning and scrolling the text to the same place, thus creating an unpleasant flicker.

To remedy this problem we simply replace set_SelectedText with our own implementation:

Instead of
textBox.SelectedText = "A quick brown fox jumped over lazy dogs. ";
use
SendMessage(hWnd, EM_REPLACESEL, false, "A quick brown fox jumped over lazy dogs. ");

You will also need the following PInvoke definitions:

 

const uint EM_REPLACESEL = 0xc2;

[DllImport("coredll")]
extern static IntPtr GetCapture();
[DllImport("coredll")]
extern static int SendMessage(IntPtr hWnd, uint Msg, bool WParam, string LParam);

 

To get HWND of your textbox use something like this:

 

private IntPtr GetHWND(Control ctl)
{
    ctl.Capture =
true
;
    IntPtr hWnd = GetCapture();
    ctl.Capture =
false
;
   
return
hWnd;
}

 

12/19/2004 10:12:42 PM (Pacific Standard Time, UTC-08:00)  #    Comments [2]  |