Scott Hanselman

Well I Got The Big Idea That Id Convert My Tiny Abstract OS In C

October 8, '02 Comments [0] Posted in Web Services
Sponsored By

Well, I got the big idea that I'd convert my Tiny Abstract OS in C# to other languages (VB.NET, J#, etc)  and platforms (.NET CF, Mono).  I figured it'd be a good exercise to practice VB.NET since it's moderately complex and moderately OOP.  I've never been one who can bang out little samples - I really need something cohesive and meaty if I'm going to really dig into it.  Plus, it'd be cool to have other versions of the TinyOS around in other languages. It really exercises the basics of the .NET Framework, and it's been downloaded 4955 times from http://www.gotdotnet.com, which is very exciting and I get mail about it all the time.

So, I figured I'd start with VB.NET before I fire up VMWare and my Mono VM.  I sat down and started using the C# to VB.NET converter VS.NET Add-In.  I converted the who project and received 102 Todos, which is about more than I expected.  I had no idea what was really going on...until I saw things like this in my new VB Project:

If instr.Param2 <> System.UInt32.MaxValue Then 'ToDo: Unsigned Integers not supported

and:

Public Sub New(id As System.UInt32, memorySize As System.UInt32)
'ToDo: Unsigned Integers not supported
'ToDo: Unsigned Integers not supported
pid = id
registers(8) =
CType(pid, System.UInt32) 'ToDo: Unsigned Integers not supported
processMemorySize = memorySize
End Sub 'New

Turns out, from a CLS point of view, I really shot myself in the foot on this one.  I wrote this Tiny OS using exactly what I needed - when the system called for UInt32, I used one.  Apparently (I knew this, but never really grokked it) the CLS does not define an unsigned integer, and the UInt32 type is marked as not CLS compliant. So, what type should I use?  It was a silly thing really, to build something low-level bits-and-bytes with a managed language, but since I was at a low level, it seemed silly to only use the signed half of an Integer.

Well, I'm going to re-think this little exercise, perhaps I'll first try to get it running as a C# app in Mono and go from there.


Let me add one particuar point of note.  I'm interested in this valiant attempt by the C# to VB.NET Converter and appreciate any insights.  When trying to convert this particularly icky (hacky...now that I look at it, I could have done it easier with adding the bytes and shifting bits as I added...ah, how time refactors for you...) bit of C# code:

public unsafe static uint BytesToUInt(byte[] BytesIn)
{
     fixed(byte* otherbytes = BytesIn)
     {
    
uint newUint = 0;
    
uint* ut = (uint*)&otherbytes[0];
     newUint = *ut;
    
return newUint;
     }
}

The C# to VB.NET converter came up with this:

Private Declare Sub CopySystem.UInt32 Lib "Kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As System.UInt32, ByRef Source As System.UInt32, ByteCount As Integer)

Public Shared Function BytesToUInt(BytesIn() As Byte) As System.UInt32 'ToDo: Unsigned Integers not supported
    
Dim otherbytes As Machine.BytePtr = New Machine.BytePtr(BytesIn)
    
Try
          Dim newUint As System.UInt32 = 0 'ToDo: Unsigned Integers not supported
         
Dim ut As Machine.TypePtr = New Machine.TypePtr(Machine.TypePtr.Cast(Machine.NewPointer(otherbytes(0)), GetType(System.UInt32)), typeof(System.UInt32)) 'ToDo: Unsigned Integers not supported
         
CopySystem.UInt32(newUint, ut.Address, Runtime.InteropServices.Marshal.SizeOf(GetType(System.UInt32)))
         
Return newUint
    
Finally
         
otherbytes.Dispose()
    
End Try
End Function 'BytesToUInt

Hmm...

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.