C# error CS1540

 Sep 12th, 2012 

 , , ,

There are many ways in which one could violate the rules of Object Oriented Programming in C#. Fortunately, the C# compiler is smart enough to prevent many of them, like the one in the example below. Can you  figure out why the compiler doesn’t allow the call in the last method?

abstract class BaseClass
    protected abstract void AMethod(BaseClass arg);
class Derived : BaseClass
    protected override void AMethod(BaseClass arg)

Read the rest of this post »

Using UAC with C# – Part 3

 Jan 6th, 2010 

 , ,

After a long period since I wrote part 2 of this article I decided to add some extra information. There is one thing that was missed by the previous two articles: the design of UAC enabled applications.

If you use Windows Vista/7 then you know that buttons and links which elevate privileges are preceded by a shield icon. This is the way Microsoft decided to inform the user about the effect of clicking that control.

The first idea that might pop up is the reinvention of the wheel (or shield). In other words you could draw the shield on a button. This is OK except that:

  1. Is not easy
  2. Will require you to recompile the interface if Microsoft decides to change the icon
  3. You need the icon in many sizes 16×16, 24×24, 32×32, etc. (extract it from MS’ DLLs)
  4. Will create a lot of overhead with layout (position icon relative to text size/position)

The second method is easier, safer and recommended by MS. All you need to do is send a specific message (BCM_SETSHIELD) to the button if the user has limited privileges and pressing that button will trigger the UAC window. Actually there is a second, tricky, thing that must be done: the style of the button must be “System” (in C# “System.Windows.FlatStyle.System”). Without this you will not be able to see the shield.

The code provided in part 1 of this article will be modified in order to display the shield on the two buttons. Moreover, the shield will be displayed only when the user runs under an account with limited privileges or non-elevated administrator.

In order to display the shield one needs to send the BCM_SETSHIELD (=0x0000160C) message to the button. This can be done by using the SendMessage function from user32.dll. This article will not cover what is and how to use SendMessage, if you need more information about it follow the previous link.

To set the shield of the “Elevate this application” button one needs to send the message in the following way:

SendMessage(btnElevate.Handle, BCM_SETSHIELD, 0, 1);

The first parameter is the handle of the button, the second one is the message, the third one is not used and must be ‘0’ and the last argument must be non-zero in order to draw the shield, zero otherwise.

If you try this it will not work :) Remember the ‘tricky’ thing told before? This is the full code to display the shield for btnElevate:

btnElevate.FlatStyle = FlatStyle.System;
SendMessage(btnElevate.Handle, BCM_SETSHIELD, 0, 1);

There is only one thing that must be done in order to work properly. Remove the shield if the user has elevated privileges. I don’t know if this is against MS’ recommendation but in my opinion one must not be shown information that cannot be used in that context; in our case don’t show the elevate shield if there is nothing to elevate.

Part 1 described how to check if a user has full rights. Now we are just using that boolean variable:

if (!hasAdministrativeRight)

Where SetUACShields will send the message to all buttons that require the shield drawn.

The full updated code from Part 1: Download IconUAC Code 3 (10.13 KB)

First CodeProject article – WPFDesigner

 Aug 30th, 2008 

 , , ,

Today I’ve posted my first article on CodeProject.

The article describes how to create a custom control in which you add elements and you can move/resize them.

The article can be found here.

Programming Puns

 Jul 19th, 2008 

 , ,

Writing code, comments, variable names, etc., in the manner that they create a pun:

byte me;
long john_silver;
char broiled;
string vest;
float valve;
double jeopardy;
struct by_lightning { … };
Object strongly;
class warfare { … };
String cheese;
Exception taken;
Graphics ex; // XXX
long walk(short pier) { } // from the MrBunny Java book
int elligence;
bool me_over;
short circus;
int eger; // …completing what the designers of the language left out
char acter;
bool ean;
void* bowels;
class Brick implements Throwable { }
class Marathon implements Runnable { }
class Novel implements Serializable { }
class Human implements Cloneable { }
Fun(key) %% funky!
/* Following was seen in the required (at least until 3.8 or so) constructor for a JUnit TestCase.
* Many words or parts of words can be substituted for man as the variable name for hours of amusement.
* The shortest that makes sense is just “b”.
public MyTestCase(String man) {
throw (fit | up | something);
while (e_coyote) {

How expensive are cross-domain operations?

 Apr 28th, 2008 

 , , , , ,

I was curios how expensive is a cross domain operation so I have made a test.

The test procedure is simple. Perform a number of cross-domain and non cross-domain operations (get the value of NextNumber() from same domain and from another domain) and measure the time elapsed. For each value I have runned the application 3 times, recorded the time (in milliseconds) and created the mean of this three.

For this I’ve created a Console Application containing two classes, the main class and “NumberClass” which has a method that returns the next long. The code for this two is below:


//MarshalByRefObject is used because this object will cross domain boundary
class NumberClass:MarshalByRefObject
    public ulong number = 0;
    public ulong NextNumber()
        return number++;

The Main method from the main class:

Read the rest of this post »

EventInfo.AddEventHandler – bug or not?

 Apr 16th, 2008 

 , , , , ,

Reflection is the process by which a computer program can observe and modify its own structure and behavior. The programming paradigm driven by reflection is called reflective programming. [Wikipedia]

Working a few days ago on a plugin architecture on .NET Framework I’ve found something very interesting. The method System.Reflection.EventInfo.AddEventHandler does late-binding. You’ll probably say “so what?”. Let’s see the problem with an example:

Using Visual Studio 2005/2008, create two C# projects (a windows forms application project and a class library one). Rename the class from the class library project to “TestClass.cs”.

Read the rest of this post »