Wimdows.NET

Wim's .NET blog

August 2004 - Posts

CharSet enumeration for P/Invoke and StructLayout

Hongmeig blogs about the difference between Marshal.SizeOf(typeof(char)) and sizeof(char).

The reason for this is that a char in unmanaged code (P/Invoke, marshalling) is 1 byte and like Hongmeig says in the .NET framework a char is Unicode, so 2 bytes.

What I'd like to point out though, is that we do have control over the character set when using P/Invoke calls and the physical layout of structs with regards to the character set. This can be very useful for string marshalling.

It can be achieved by using the System.Runtime.InteropServices.CharSet enumeration {Ansi,Auto,None,Unicode}, which can be applied on the DllImportAttribute and the StructLayoutAttribute.

For instance:

using System; 
using System.Runtime.InteropServices;
class Class1
{
static void Main(string[] args)
{
unsafe
{
Console.WriteLine("Marshal.SizeOf(typeof(TestAnsi) shows: " + Marshal.SizeOf(typeof(TestAnsi)));
Console.WriteLine("Marshal.SizeOf(typeof(TestUnicode) shows: " + Marshal.SizeOf(typeof(TestUnicode)));
}
}
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct TestAnsi
{
public char testbyte;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct TestUnicode
{
public char testbyte;
}

This results in the following output:

Marshal.SizeOf(typeof(TestAnsi) shows: 1
Marshal.SizeOf(typeof(TestUnicode) shows: 2

This CharSet enumeration can be applied in a similar fashion on the DllImportAttribute.

Posted: Aug 19 2004, 11:43 AM by Wim | with 1 comment(s)
Filed under:
Webhost4Life...until things go horribly wrong...

A good colleague and friend of mine has been hosting a busy website with WebHost4Life for well over a year now. Support is good, quick and usually pretty painless.

A couple of weeks ago - he receives this e-mail:

Dear member,

I am sorry to inform you that your server had just suffered from a sever power supply failure. Because of that the Raid Harddrives was fried as well as the Daily Backup HardDrive that's in the same machine fried as well.

We normally have a Mass Storge server that'll store all the backups remotely. However, the system when down in the middle of such transfering, so we cannot retrieve any data from our Mass Storage system either.

We are now sending all the Harddrives of this machine to the Labortary and to see if we are able to recover the data in it.

This is the 1st time in 5 years that we have such problem. We'll look into why the power failure fried all the Harddrives and prevent this from happening again.

In the meantime, if you have any backup of your website we can help you load it up into a brand new server and get your site backup and running right away. Please let us know asap.

We'll futher design a special backup plan just for you to avoid any future problems. We can backup your site daily and send it to another server located in another state to maximize the backup reliablity. We can move you to a different webserver, please confirm.

Basically, the long and the short of it is that he paid for backup (it's part of the hosting package) and it will cost 40 bucks to restore, but WebHost4Life were unable to do this for him.

After a week of 'to-ing' and 'fro-ing' he's now been offered free webhosting for LIFE! Hmm..wonder if that's how they came up with the name in the first place...:-)

As a result of this, it looks like they're not too keen to help him get his site back up. After all, he's not paying for it anymore.

So, basically, make sure you're not stranded like he did and query your hosts backup policy, to make sure they're not using a "Daily Backup HardDrive that's in the same machine"!!! It's alright saying they offer backup, but when it comes to the crunch, you want to make sure they are able to do a proper restore!

Posted: Aug 19 2004, 10:14 AM by Wim | with 75 comment(s)
Filed under: ,
If you like Celtic...

...folk kinda music, you might want to check out Loreena McKennit.

Listening to 'Skellig' at the moment. Absolutely brilliant stuff!

Posted: Aug 12 2004, 09:44 PM by Wim | with 1 comment(s)
Filed under:
Xml Serialization and the XmlArray and XmlArrayItem control attributes

We all know -or should know- that when using XML Serialization either over SOAP or manually, we can control the serialized output with so called control attributes.

Well known examples are the XmlElementAttribute and the XmlAttributeAttribute. I think it's worth mentioning the usefulness of the XmlArrayAttribute and the XmlArrayItemAttribute as well. XmlArrayAttribute to be used on the usual fixed arrays, since dynamic arrays can't be serialized and the XmlArrayItemAttribute for the element name of a single item in the array.

Here's a sample class:

using System.Xml.Serialization;
[Serializable]
public class Customer
{ // we need default ctor for Serialization!
public Customer()
{}
private string __fname;
private string __lname;
private Invoice[] __invoices;




[XmlElement("FirstName")]
public string FName
{
get { return this.__fname; }
set { this.__fname=value; }
}

[XmlElement("LastName")]
public string LName
{
get { return this.__lname; }
set { this.__lname=value; }
}

[XmlArray("Orders")]
[XmlArrayItem("Order")]
public Invoice[] Invoices
{
get { return this.__invoices; }
set { this.__invoices=value; }
}
}

Without taking into account that an Invoice is actually not the same as an Order :-), when we look at the serialized XML for a Customer object, it looks roughly like this:

<Customer>
 <FirstName>Wim</FirstName>
 <LastName>Hollebrandse</LastName>
 <Orders>
  <Order>
   <ID>12312</ID>
  </Order>
  <Order>
   <ID>12313</ID>
  </Order>
  <Order>
   <ID>12314</ID>
  </Order>
 </Orders>
</Customer>

Oops - just noticed I typed 'ID' with two capitals. Check out the discussion on Andrew Stevenson's blog.

Posted: Aug 12 2004, 09:10 PM by Wim | with 6 comment(s)
Filed under: ,
More Posts