pinvoke.net

Wed, October 4, 2006, 09:42 AM under dotNET
In my Vista sessions I focus on accessing native APIs form managed code. A few times now people ask me if there is an API browser of some sort... you know, like the API Text Viewer from the VB6 times... I found this replacement on the web but haven’t personally tried it. Frankly, I don’t know of a Microsoft one so if there is a replacement hiding somewhere and you know about it please share a pointer!

So the alternative I offer is... the alternative I have always offered! The title gives it away: pinvoke.net
If you read my older entry then you’ll know where to go to get the interop book.

There is also a Visual Studio 2005 add-in for the pinvoke wiki site, but I haven’t personally tried it. Get it from gotdotnet.

I plan to update the wiki with Vista API definitions at some point (the power API I mentioned previously is already there). I’ve added this to my TODO list (item # 88345602), but if you feel you’d like to take on the task, please go for it (Vista dllimports all over my blog entries since May this year)!

ApplicationRecoverCallback

Wed, October 4, 2006, 04:36 AM under Windows | Vista
At a talk last week, while demoing the Vista application recovery mechanism, a delegate (no pun intended!) asked me if he could access UI elements (e.g. textbox contents) from the recovery function (i.e. in my sample, the RecoveryIt method).

The stock answer is that in your recovery method you should not be spending too much time doing anything and should make no assumptions for any application state as you have just crashed and hence who knows what is valid and what not. Try and do something that is fairly autonomous.

Having said that, from trial and error, I can tell you that you can write to files (as the sample shows), you can create threads, access private variables (and hence navigate object structures) and even the form's handle is valid.

Note that the method runs on a worker thread (that is donated by the OS in the recovery scenario); it is easily provable by sticking the following line of code in the recovery method:
MessageBox.Show(Thread.CurrentThread.IsBackground.ToString()); //true

So, based on the previous sentence, it is obvious that you cannot touch UI elements from that method (we know that we have to be on the main thread to do that). Even if you try to use Invoke/BeginInvoke, you still won't get very far. The point here is that you should treat the UI as frozen or in other words don't rely on the availability of a message pump...

Also, here is an extract of the bullet points from the slide that accompanies my demo:
+ Recovery routine called after data collection
--+ Application’s code attempts to recover user work
----+ Flush to disk, repair on next run of application
----+ Repair data in memory, save to disk

Hope this helps.

Mobile cross-platform development

Tue, October 3, 2006, 03:00 AM under MobileAndEmbedded
A topic I've done a lot of work on in the past and just one of the areas covered in Nick Randolph's podcast so go listen here (funny American and Aussie accents aside :-p).

Inspired by the podcast, I gathered some of the relevant blog posts I've written on the subject in chronological order (click on the numbers): 1, 2, 3, 4, 5, 6, 7, 8, 9

Favourite quote by Nick near the end (47:51):
"...you need the equivalent of the vb6 to vb.net converter/wizard... you'd run it on a VB6 app and it would come back with 18 million lines of code that had to be changed and your application only had 500K lines of code! "
LOL... been there, done that, got the t-shirt :D

Microsoft Developer Show #8

Vista: WerRegisterFile

Mon, October 2, 2006, 05:52 AM under Windows | Vista
Did you know that on Vista you can add your own files to the reports of WER?

Here are some steps for achieving that.

1. Create a brand new VS2005 WinForms project on Vista and name it "WerRegisterFile". Add a button to the form and in its event handler stick the following:
    private void button1_Click(object sender, EventArgs e)
{
// simulate crash
Environment.FailFast("bye bye");
}
Build and run the project from the file system to observe WER in action
a) First windows checks if there is a solution to the problem (screenshot).
b) Then it asks the end user to confirm that they'd like to send the report (screenshot)
c1) it then (asynchronously) proceeds with sending the data (e.g. Version.txt, minidump.mdmp) to winqual (screenshot). Note: this can be instantaneous and capturing that screenshot required blistering mouse movement

c2) it then informs the user to close the program (screenshot). Note that if the app registers for restart, then you won't see this dialog and instead the application will re-launch itself :-)

Let's enhance the WER process by adding our own file to send to winqual:

2. In the form paste the following pinvoke declarations:
    internal const int WerRegFileTypeOther = 2;
internal const int WER_FILE_ANONYMOUS_DATA = 2;

[System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = CharSet.Auto)]
internal static extern int WerRegisterFile(string pwzFile, int regFileType, int dwFlags);
3. Anywhere in your project (e.g. form_load, form ctor, button_click etc) before the application crashes make the following call:
WerRegisterFile("danielmoth.txt", WerRegFileTypeOther, WER_FILE_ANONYMOUS_DATA);
4. Go to the build directory of your solution (e.g. bin\release) and create a file named "danielmoth.txt"

Now, when you rebuild and then run the exe form the file system, you'll observe the same screenshots /behaviour as above. So how can we inspect that our own file was sent (other than visiting winqual and checking there)? Well, with 'Problem Reports and Solutions' of course!

Find the entry for "WerRegisterFile" (check the date/time to verify that it was just generated). Click on it and you are presented with the following dialog (I've highlighted with the red arrow the evidence that our file was sent):


If you click on the "View a temporary copy of these files" then you get to view a copy of exactly what was sent.

That's all folks :)

Some quick notes:
i) Remember that if you don't have an internet connection, these reports are queued and you can still view them following the steps I outlined before

ii) If you replace step 1 at the top with the alternative crash I described here (steps a & b), then in the last screenshot where it reads "Problem Signature 09" instead of "FatalError" it would say "System.DivideByZeroException"

iii) In addition to crashes and hangs, you can create and submit reports for other events. Surf the relevant APIs by starting here at WerReportCreate and then examine the APIs in the ‘Remarks’ section (and if you create a cool demo with those please drop me a line!).

Vista: Problem Reports and Solutions

Sun, October 1, 2006, 08:59 AM under Windows | Vista
I've talked about Windows Error Reporting in Vista before, but I don't think I explained how to examine locally the reports that have been sent (or that are in a queue to be sent, since with Vista that is what happens when there is no network connection).

From the Control Panel, navigate to 'System and Maintenance' and click the option that reads "Problem Reports and Solutions". You should have a screenshot similar to the following:


I encourage you to explore all of those options on the left, for example if you select "View problem history", then you are presented with a list that I am sure you'll find interesting. On my machine it looks as follows (you can drill into each report further as we'll see in my next blog entry):


Another relevant UI worth exploring is the "Reliability Monitor"
(...from the 'System and Maintenance' click on 'Performance Information and Tools' and then click on 'Advanced Tools' from the left).

VS “Orcas” September CTP

Sat, September 30, 2006, 03:58 PM under Orcas | VisualStudio
One of the areas I will be focusing on as we move forward is the next version of Visual Studio code-named Orcas. So, when I got word last week (while on the road) that the 1st CTP was out, I couldn’t wait to come back home and play with it.

Unfortunately, it doesn’t look like I will be doing any playing :-( Not because this CTP doesn’t include all the LINQ bits or because it doesn’t include all the C# 3.0 bits or even all the VB9 bits. No, the reason I won’t be playing is because it is released as a VPC image that is not supported on Windows Vista. I just don't have any machines (or partitions) with XP on them (and quite frankly don’t want to ever go back to XP). Shame, as I was looking forward to playing with unit testing on devices and the other improvements for the NETCF developer.

Oh well, looking forward to the next drop and let’s hope there will be support for Vista (or that I decide to use Virtual PC 2007 Beta on Vista RC to test a CTP)!

If you can though, check it out and be one of the first to start blogging about what you like and what not. Download the Orcas September CTP here.

P2PMessageQueue queries

Sat, September 30, 2006, 09:36 AM under MobileAndEmbedded
Over the last year I have had a few people contact me with questions relating to the Point to Point Message Queue class I wrote for MSDN. This blog entry will serve as a URL where I can point future questions regarding it.

1. It seems that under certain circumstances, on certain platforms, the queue does not close properly. A possible solution is to declare a dedicated named event that you create in the ctor of the queue. Set it in the Close method. In the Monitor method, change the WaitForSingleObject to WaitForMultipleObjects and include this new event.

2. It also seems that under certain other conditions (that amongst other things include multiple senders) the class spikes the cpu. This is due to the way the native API works and the solution is not to run the thread that is responsible for raising events. To do that, follow the advice of the article and create a new P2PMessageQueueSender class that inherits from the original one and simply overrides the StartEventThread with an empty method (Note that this solution should also work for the 1st issue described above)

3. If you are porting the code to NETCF v2.0 (when I wrote the article it wasn’t released yet), there are things you can do to the code other than simply recompiling it, such as making any worker threads to background (IsBackground=true) .

Of course, the easiest thing for you to do is none of the above. Simply get the SDF from OpenNETCF, which will include the above improvements (plus you can then get support from the team as opposed to the “as is” code of my old msdn article ;-)).

For other NETCF questions, please use the newsgroup and forum...

MVPs, MVPs, MVPs

Fri, September 29, 2006, 03:45 AM under Events
One of the four major drawbacks of accepting my job here at Microsoft was losing my MVP status.

So I am excited to be attending our UK MVP Open Day today and delighted to be involved with this event from the other side. Any new or old MVPs here in Reading (TVP) for the day make sure you say hi! (Come at the “Speaker Coaching” and “Developer Evangelist Roundtable” sessions, not to mention the drinking sessions after...)

For those MVPs that didn’t make it down for the day, if you are on the developer side of things, one of the announcements today was that you can think of me as an additional contact of the local office. My email is on the left :)

CommandLink for XP

Thu, September 28, 2006, 01:57 AM under dotNET
I've talked about the new button style in Vista before: CommandLink (and Vista uses this everywhere).

While looking on the web for something irrelevant, I came accross an old post by Geoff Appleby that offers a custom implementation of CommandLink for XP. Check it out!

Events yesterday and next Tuesday

Wed, September 27, 2006, 07:33 AM under Events
http://msevents.microsoft.com/CUI/EventDetail.aspx?culture=en-GB&eventid=1032308506

Yesterday we run the final rerun of the MSDN roadshow. In the short version of my Vista session I advertised my other upcoming session next Tuesday and someone from the audience pointed out that they could not register because it was full. I checked with the events team and we have now moved it to a larger room so if you tried and failed, please try again to register here (and ignore the "full" message, go on the waitlist and you'll be fine).

Note that I am tweaking (once again) the content I delivered in Edinburgh so it should be even better (as if that is possible :-p)