Managed World

Techno-babble from yet another babbler RSS 2.0
# Friday, January 11, 2008

Well, at least I found it funny. A coworker brought this to my attention today. I thought it was pretty clever and enjoyed it. Then again, I've known for a while how great Windows Server 2008 is :). Here's an excerpt:

Once I was almost famous. For years, my friends and I were on the front lines: we were the Windows Server 2003 servers that powered Microsoft.com, one of the hottest Web sites in the world. Then, early last summer, everything changed. Quietly, without warning, the new kids took over. Windows Server 2008. Yes, I know, the product’s not even done yet. These were Beta 3 servers, for Pete’s sake. Long way from prime time. But there they were, humming away. No problems. All of ‘em on Windows Server 2008. Except me. The last Windows Server 2003 left at Microsoft.com.

Enjoy!

 #       Comments [0]

Hello everyone, and welcome back after our winter break hiatus. We have a lot of cool features to cover in the coming weeks that will especially show off the power of Windows Server 2008 as an application server. So, this week we will kick off the new year by taking a short little peek at the new management API coming in IIS7.

What is new in IIS7 Administration? IIS7 provides a comprehensive managed-code API that allows complete manipulation of the XML configuration files and convenience access to server objects. IIS7 includes Microsoft.Web.Administration, which is a new management API for the web server that enables editing configuration through complete manipulation of the XML configuration files and also provides convenience objects to manage the server, its properties and state. The configuration editing aspect of the API provides programmatic access to read and write configuration properties in the IIS configuration file hierarchy and specific configuration files. The object management aspect of this API provides a series of top-level administration objects for direct management of the server (i.e. sites, application pools, worker processes, etc.).

The management classes reside in the Microsoft.Web.Administration namespace. The classes provide a weakly-typed interface to access configuration sections and convenience objects with properties and methods representing attributes of the configuration (like the path of a virtual directory) or actions to take on the object (like recycling an application pool).

Resources

 #       Comments [0]

As part of the "Developer Meet Server" show on Channel 9, I have started a new series of videos on the topic of application compatibility for Windows Server 2008. I will be releasing one video interview per week all the way until launch, so make sure to keep an eye open!

in this first video, we discuss a bunch of topics in this video that are very pertinent to the topic of Application Compatibility on Windows Server 2008, from Session 0 Isolation to IIS 7 to Mandatory Integrity Control, among many others (I will give any of you five geek points if you can work in "Session 0 Isolation" or "Mandatory Integrity Control" into your next dinner party conversation!).

So, make sure to go check it out.

 #       Comments [0]
# Thursday, November 22, 2007

In my previous post, I talked about why I personally love C++/CLI as a solution for managed and native interop. With the launch of Visual Studio 2008, and the include of the new marshalling library, there is on part of the equation missing, I believe. On the C# side of the interop equation, you have http://www.pinvoke.net. When I'm wanting to interop with a given Windows API, there is a very (very) good chance that the wrappers have already been posted onto pinvoke.net.

While the marshal_as<> construct of the marshalling library in VC++ 2008 is quite extensible through the use of templates, there is no community site for me to go to in order to retrieve an existing piece of the interop puzzle. That is, until now.

With the help of Kate Gregory, there is now a new website for those C++/CLI users out there: http://www.marshal-as.net. If that link doesn't work for you, give it a day or two as this is a new website and DNS is still propagating for it.

I have already sent Kate a couple of samples that I had done for my own interop work with Visual C++ 2008 (have I mentioned yet how easy my life is now that I'm using C++/CLI in Visual C++ 2008?). You should pop over and check it out. If you have your own samples that you want to submit, feel free to use the contact form (the email icon) at the website and email your sample to the website.

Enjoy!

 #       Comments [6]

 

This debate came up recently on my trip to Tech Ed Developer in Barcelona. If I am having to write interop code, should I use C# and p/invoke, or should I use C++/CLI? I had some very smart people like Kate Gregory and Daniel Moth to chat with this about. Kate and Daniel are definitely good people to chat with because, honestly, they have completely different opinions on this topic. Kate is on the C++/CLI side of things, especially for serious interop tasks, while Daniel believes that you should keep it in C# no matter how complicated that C# gets. So, what side of the fence do I fall on?

This is a question I've been pondering for a while now. Since I first came in to this Windows Server 2008 Technical Evangelist, I've had to write a lot of interop code. And I personally believe in using the right tool for the job. And while p/invoke is great for smaller interop jobs between native code and managed code, it can "get out of hand" quickly. For instance, to p/invoke into the new Transactional NTFS APIs, I could look at having to create a bunch (several hundred lines of code or many more) of interop structures merely to call DeviceIoControl to get issue a single TxF command. Here's the thing: all those structures are all ready defined on the native side and could be used with ease (as in, a simple "#include" statement) on the C++/CLI side of things. No extra code necessary.

But it's not just the structure definitions that are the problem, it is also the programming paradigm used in the native code you are trying to interop with. When dealing with a simple Windows API function, this may not be a problem at all. Of course, it gets a little scary when dealing with COM interop, but at least there are still the tools built into C# to interop with COM. Where the train really starts to derail is when you start writing interop code for a native library that uses a different paradigm.

For instance, let's look at DeviceIoControl. It's not really one function call, it's a complete API in itself. I've had to get familiar with DeviceIoControl when doing my work with Transactional NTFS. DeviceIoControl uses a much more conversational paradigm than managed developers are perhaps used to. Here's a common "calling session" with DeviceIoControl (minus all the fun exception handling that should be there):

while (true)
{
    // Clean-up previously allocated memory

    // Allocate memory

    // Issue command
    if (!DeviceIoControl(...))
    {
        if (GetLastError() == ERROR_MORE_DATA)
        {
            // Determine how much more memory we need

            continue;
        }
    }
    else
    {
        // There was enough memory finally

        // Pull data out of buffer we issued to DeviceIoControl

        break;
    }
}

When writing managed code against this DeviceIoControl paradigm, you will write more code that is harder to maintain and debug than you will if you simply used C++/CLI to do the interop. With C++/CLI you can use the API natively exactly like it's meant to be used, and then "expose" the code to the managed world via C++/CLI. When I moved my Transaction NTFS wrapper from C# to C++/CLI, I was able to remove a large chunk of code, and it was much easier to read and understand. Using the right tool for the job will do that for you.

The DeviceIoControl paradigm is just one example though. Another "hang up" when writing interop code in C# is when you need to do some very detailed memory and pointer manipulation. Take this following piece of code that is used within the DeviceIoControl pattern above and is used to get the list of running transactions on a system (assuming the "list" has already been populated):

// Loop through the transaction entries and store them in a list
TXFS_LIST_TRANSACTIONS_ENTRY *txEntry;
txEntry = (TXFS_LIST_TRANSACTIONS_ENTRY *)((char *)txList + sizeof(TXFS_LIST_TRANSACTIONS));
for (DWORD i = 0; i < txList->NumberOfTransactions; i++)
{
    TRANSACTION_ENTRY *newEntry = AddTxFEntry(txEntry->TransactionId, txEntry->TransactionState);

    // Each transaction is attempted to be loaded with their files ASAP (if any).
    LoadLockedFiles(rmDirectory, newEntry);
    txEntry++;
}

This code is a PITA to write in C#. Not only do you have to write all the interop structures for TXFS_LIST_TRANSACTIONS_ENTRY, TXFS_LIST_TRANSACTIONS, and TRANSACTION_ENTRY, but you have to do all the pointer fun via the Marshal helper in C#. Sure, you could pop into unsafe code and deal with pointers directly, but I think if you are willing to do that, you should be more than willing to just use C++/CLI. Why would I go through all that, if I can just write the code and expose the results to managed code via C++/CLI? As soon as I switched the wrapper from C# to C++/CLI, my job got a LOT easier. And this is coming from a programmer that is relatively new to the whole C++ world. If I can write in C++/CLI for interop, I honestly believe any committed C# developer can as well.

Let's look at a small snippet from the Transactional NTFS Wrapper:

FileStream^ TransactedFile::Open(System::String ^path, FileMode mode, FileAccess access, FileShare share) {
                
    ...

    ////////////////////////////////////////////////////////////////////////
    // We need to marshal our values into native types since we are
    // going to call into a native Windows API based on CLR parameters
    marshal_context ctx;
    const wchar_t *marshalledPath = ctx.marshal_as<const wchar_t *>(path);

    HANDLE fileHandle = CreateFileTransacted(marshalledPath, 
        marshal_as<DWORD>(access), 
        marshal_as<DWORD>(share),
        NULL, 
        marshal_as<DWORD>(mode), 
        FILE_ATTRIBUTE_NORMAL, 
        NULL, 
        kernelTransactionHandle, 
        NULL, 
        NULL);
    
    ...
}

This is the actual segment of code that does the main interop piece for me. Is the above that intimidating? A couple of things to note to those C# developers out there. You can think of the "^" as denoting "by reference". Yes, it's a bit more complicated than that, but just think of it that way for now, that's all you need to know. And the "::" operator is your namespace operator. So instead of "System.String", I have "System::String". Once again, a very simple syntactic difference.

The marshal_context object is a new object introduced with Visual C++ 2008 as part of the new marshalling library. By using the marshal_context, the context will take care of memory allocation and de-allocation for the strings we are are interop'ing. Very simple. The CreateFileTransacted API expects the path to be a "const wchar_t *", so I simply marshal our passed System::String as "const wchar_t *" and we're good to go. Since marshal_as<> is template based, it's very easy to extend yourself. And that's what I've done with marshalling System::IO::FileMode/Access/Share as DWORD (which I will show in a future post).

Now, honestly, is that piece of code that scary? If you have questions about it or things that still scare you, please let me know and I will look at writing a future post on the topic.

Back to the debate at hand though: C# versus C++/CLI for interop. I can certainly understand where Daniel is coming from. C# developers can quickly become intimidated when seeing C++/CLI code. It isn't the C# code they know and love. But I'm not sure he gives C# developers enough credit. It's an important step for developers to realize that, syntactically, C++/CLI and C# are not that different. If you are familiar with reference types in C#, you are probably more familiar with pointers than you realize. There are some "gotchas" around topics like pinned pointers, but they are concepts you need to know in C# as well if writing interop code there, so they aren't that big of a deal in the end. I'm not saying to not use P/Invoke and C# though. I'm simply saying to use the right tool for the job. If it is a very simple Windows API you are writing interop code for, C# and P/Invoke is probably "good enough". For more complicated interop jobs though (as in with most of the new enhancements in Vista and Windows Server 2008), why not use C++/CLI since it will make your life easier one you get over the small learning curve?

Did I have this same opinion before Visual Studio 2008? Yes, and no. Yes, C++/CLI code was still built for interop, but I honestly believe that the introduction of the extensible marshal_as<> construct (combined with other technologies like STL CLR) make interop in C++/CLI even more powerful and easier to use than before. For instance, marshalling strings was not fun to do before Visual Studio 2008 (especially for a developer not extremely aware of the different ways to use strings in native code). However, out of the box, the marshal_as<> construct provides a lot of different ways to work with and to interop with strings. And in all of these ways, you don't have to worry about allocating or de-allocating memory at all (ah, the same joys I get when working in a garbage collected world). And in the case that you are interacting with C++ libraries that are possibly STL-based, STL CLR provides you with a library that you simply don't get in C# (interop'ing with C-based Windows APIs are one thing, interop'ing with proper C++ libraries are a completely different beast when using C# for the interop code).

If you are a C# developer that writes _any_ interop code, you owe it to yourself to learn C++/CLI. Trust me, it will make your life much easier, and it's not that hard to learn. And being scared of pointers is no excuse. If you are comfortable with reference types in C#, you are probably more familiar with the concept of pointers than you realize. Plus, with STL and Boost (even more so with the upcoming TR1 work), there really is no need to have a pointer anywhere in your modern C++ program (ah, the sweetness that is the shared_ptr).

So with the launch of Visual Studio 2008, I believe that the "right tool" for the "interop job" is C++/CLI for anything that is beyond trivial. Constructs and libraries like marshal_as<> and STL CLR only drive this point home more. You can feel free to continue to write thousands of lines of managed-code-copying-already-defined-in-header-files simply to p/invoke into an existing Windows API, but me? I will be using Visual C++ 2008 and enjoying every minute of it since I don't have to do any re-invention of the wheel.

Posted in
 #       Comments [1]
# Monday, November 05, 2007

Lately, I've been getting a lot more serious about cutting out some time to learn a functional or dynamic language. After watching several Channel 9 videos while traveling today (and listening to several podcasts), I've realized that I've started to grow not only "complacent", but a bit frustrated as well with static languages (currently VB and C#).

It came to mean when I was defining a class. I was working on the properties of that class, of course using encapsulation (read: public properties backed by private fields). Property, after property, after property. Yes, there are some shortcuts by using code snippets. But still, that's a HECK OF A LOT of typing merely for "infrastructure" code. It takes quite a while to get _through_ that infrastructure until I can actually start writing the good stuff.

Now, I realize I could cut those frustrations by just having public fields and the like, but that's not the point. Besides, I think that leads to ugly code that's harder to maintain and just has stronger coupling to dependencies than their needs to be.

This is where I think I would like working with a dynamic language (I remember doing some of this stuff in Python). Instead of going and defining a property in a class, I just use it elsewhere, without having to define it elsewhere. From then on out, it's available to everyone. This way, I could be writing "The Good Code" from the start. Not of this infrastructure stuff. Of course if I went this way, I would certainly have to be doing a lot more unit testing than I do today when I write code to catch those errors you get at compile time in a static language.

Perhaps I will look at writing some XNA code via IronPython, that might be fun.

Another type of language I want to learn are purely functional languages. I want to get into things like High-Order Functions (currently, I like to think I'm somewhat bright, but the idea of a Function that accepts two or more functions as _parameters_, and _returns_ a function on top of that makes my head want to explode).

Regardless, of all this, I feel that I have a lot to learn to become a better programmer. As a programmer, I was practically born-and-raised on C#. Granted, lately I've one back, learned, and program(med) in C and C++ (and I will so in the future as well when I start digging away at future concurrency technologies coming from Microsoft). I think to make the next step as a developer, I need to get away from statically-typed, imperative, object-oriented languages and diversify.

Have any of you (my readers (well, those readers that aren't my family and write code (if there are ANY of you out there :P))) experienced this feeling as well? Do you currently use a dynamic or functional language? What do you like about it over a language like C# or Java?

Posted in Programming
 #       Comments [0]

Contact

Email Me Send mail to the author(s)

Calendar

<January 2008>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

About this site

Jason Olson's thoughts on Programming, Games, Music and Life in General

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

© Copyright 2008
Jason Olson

Sign In
All Content © 2008, Jason Olson
Theme based on 'Business' created by Christoph De Baene (delarou)