| Programming | Software Engineering | Web Design | Database | Operating Systems

Programming Virtual Server from a managed application (Part 1)

Cheers Ben
Keywords: Programming,Virtual Server
From: http://blogs.msdn.com/virtual_pc_guy/archive/2005/05/05/415135.aspx

One of the coolest things (IMHO) about Virtual Server is that we have a completely documented COM API that allows end users and other developers to create programs that automate and interact with Virtual Server.  Unfortunately doing this from a managed application (VB.Net or C#) is not the easiest thing to do.  The reason for this is that there are two main complications that you have to deal with before you can get going.

The first complication is handling COM security levels. Calling the Virtual Server COM interfaces requires that your application be running with a COM security level of ‘impersonation’ (or higher).  It is possible to do this from a managed application by using CoInitializeSecurity() - however it is only possible for an application to call CoInitializeSecurity() once – and the first call applies to the entire application.  The problem starts when you realize that many components will attempt to call CoInitializeSecurity() for you – with the wrong COM security level (Even using the VMRC ActiveX control in your application will cause this to happen).

Thankfully - this is fairly easy to deal with - if you know what you are doing.  First - there is sample code in the Virtual Server Programmers guide that shows you how to do this (be warned that this sample code has some typographical errors in it - but they are fairly easy to figure out).  You should be able to take this code and use it directly - but if you are programming in VB.Net (my preferred managed language - yes I know that this will divide my reading audience :) I recommend that you put this sample code in a separate class - rather than a separate module (for reasons that I will discuss next week).

Once you have done this - you can simply call this class to initialize COM security as part of a shared main subroutine - ensuring that you get in first.  There are two remaining problems that you should be aware of though:

  1. It is possible to have a managed application grow so complicated that it is hard to ensure that your call to CoInitializeSecurity will be the first one made.  If you get into this situation the only real solution is to create an unmanaged wrapper application that calls CoInitializeSecurity first and then launches your application.
  2. The Visual Studio 2005 beta actually uses a wrapper application to launch managed application for debugging that will stop calls to CoInitializeSecurity from working (it calls it first).  To stop this from happening you will need to go into your project settings and either enable unmanaged code debugging or disable the use of the 'vshost' process (depending on which beta release you are running).

Finally you should know that calling CoInitializeSecurity will mean that your program will require unmanaged code privileges.


Related Article
  • Automating the keyboard under Virtual Server
  • Programming Virtual Server from a managed application (Part 2)
  • The new mouse filter driver in Virtual Server 2005 Service Pack 1 beta
  • Configuring X.Org / XFree86 under Virtual PC / Virtual Server
  • Pre-creation of saved state files under Virtual Server 2005 SP1 beta

  • Comment
    Hillary Post At: 2007-1-5 19:09:35
    http://fastppc.info
    Britney Post At: 2007-1-6 3:23:17
    Hello, nice site look this:
    http://fastppc.info http://fastppc.info http://fastppc.info http://fastppc.info http://fastppc.info http://fastppc.info http://fastppc.info
    End ^) See you
    Hillary Post At: 2007-1-7 18:54:11
    http://fastppc.info
    Bill Post At: 2007-6-27 22:21:48
    It`s awesome!



    See you.
    Add Your Comment:
    Your Name:      
    Your Comment:
    Note: After you post comment,please refresh the browser to show you comment.
    Search In YeYan.CN:
     

    Home | Privacy Policy | Copyright Policy | Contact Us | Site Map
    Copyright © 2006 YeYan.CN, All Rights Reserved.