Archive for the ‘C#’ Category

VirtualKD 2.8 with VirtualBox 4.2

Monday, March 11th, 2013

If you try to install VirtualKD 2.8 on VirtualBox 4.2.x, you get an error similar to this:

Unable to cast COM object of type ‘VirtualBox.VirtualBoxClass’ to interface type ‘VirtualBox.IVirtualBox’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{…}’ failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

It turns out it’s a pretty easy thing to fix. The Interop.VirtualBox.dll distributed with VirtualKD is built for the 4.1 VirtualBox interface, so you just have to rebuild it for your version of VirtualBox. Create a C# project, and paste the following code into the Program.cs file to build a new Interop.VirtualBox.dll for 4.2.x.

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;

namespace ConvertTypeLibToAssembly
{
    public class App
    {
        private enum RegKind
        {
            RegKind_Default = 0,
            RegKind_Register = 1,
            RegKind_None = 2
        }
    
        [ DllImport( "oleaut32.dll", CharSet = CharSet.Unicode, PreserveSig = false )]
        private static extern void LoadTypeLibEx( String strTypeLibName, RegKind regKind, 
            [ MarshalAs( UnmanagedType.Interface )] out Object typeLib );
        
        public static void Main()
        {
            Object typeLib;
            LoadTypeLibEx( @"C:\Program Files\Oracle\VirtualBox\VBoxC.dll", RegKind.RegKind_None, out typeLib ); 
            
            if( typeLib == null )
            {
                Console.WriteLine( "LoadTypeLibEx failed." );
                return;
            }
                
            TypeLibConverter converter = new TypeLibConverter();
            ConversionEventHandler eventHandler = new ConversionEventHandler();
            //AssemblyBuilder asm = converter.ConvertTypeLibToAssembly( typeLib, "Interop.Virtualbox.dll", 0, eventHandler, null, null, null, null );
            AssemblyBuilder asm = converter.ConvertTypeLibToAssembly(typeLib, "Interop.VirtualBox.dll", TypeLibImporterFlags.SafeArrayAsSystemArray, eventHandler, null, null, "VirtualBox", null); //using assembly name "VirtualBox" and SafeArrayAsSystemArray to be compatible to VisualStudio-Generated Interop-Assembly

            asm.Save("Interop.Virtualbox.dll");
        }
    }

    public class ConversionEventHandler : ITypeLibImporterNotifySink
    {
        public void ReportEvent( ImporterEventKind eventKind, int eventCode, string eventMsg )
        {
            // handle warning event here...
        }
        
        public Assembly ResolveRef( object typeLib )
        {
            // resolve reference here and return a correct assembly...
            return null; 
        }    
    }
}

Some of this code was grabbed from MSDN, and a single line was grabbed from VirtualBoxService. As the latter is GPL, this code may be GPL. I don’t claim any rights to it.

C# Oscilloscope and Analog Meter Controls

Thursday, April 3rd, 2008

I updated my C# analog meter control awhile ago (it had some bugs), but never got around to posting it online. Well, here are those updates, PLUS I integrated a C# Oscilloscope control as well! I’d put up some screenshots, but I don’t currently have a compiler installed for C# (been redoing my machine) so I can’t take any. But, it works pretty nicely except for a few non-serious bugs….

This is an ongoing set of postings documenting some of the software I developed for my senior project this fall.

Go download it.

Analog Meter (Ammeter/Voltmeter/etc) Control for C# and .NET

Friday, January 18th, 2008

UPDATE: See my newest update, which includes a C# Oscilloscope and Analog Meter control!

This is another open source C# control that was developed for my Senior Design project. Its pretty obvious what it does from the following screenshot.

meter_ss.PNG

This is definitely a useful control for some projects, and is released under an open source license. This control implements a simple to use Analog Meter control, similar to old school analog meters used for all sorts of things. Obviously theres a lot more things that could be added to this, but this does what I need it to do at the moment. And, it looks quite nice if I do say so myself. 😉 Its very adjustable as well, with integrated designer support.

Usage

Add the control to your form, and adjust the Value parameter of the control to move the needle. Pretty easy to use. There are a bunch of other options, such as adjusting the frequency of the tick marks, but they should be mostly self evident as to what they do.

Enjoy! As always, let me know if you either use this or find a bug.

Download: Link

Visual Studio styled Dockable Windows Component Library for .NET

Wednesday, January 2nd, 2008

Well, isn’t THAT a mouthful of a title.

This is another open source project created as a result of my Senior Design project. See, we had this nice GUI, but it seemed like it needed to be a lot more flexible than it was, since using it was really quite annoying. So, I got the idea to copy the interface from Visual Studio… since, in my opinion, the way the dockable windows work in VS is quite intuitive and useful. Some of you may cry out that there are other programs that do the same behavior… well, I copied it from VS.

So, thats what this component library implements. See a screenshot:

panes.png

As you can see, its capable of fairly complex layouts, with each element completely resizable and movable and such.  It is not quite as polished as the interface for Visual Studio, but its still quite intuitive for the user to use, and a pretty decent approximation for most projects. More importantly, its open source!

A sample executable and source code/project files are included in the download package.

Please note that you can NOT drag new panes into the form designer, as implementing the appropriate designer support seemed to be more trouble than its worth. Refer to the PaneTest project for detailed implementation, and the source code for detailed documentation.

If you find any bugs or have any comments or questions, feel free to drop me an email!

Download: Link

PSoC USB SuiteUSB .NET (CyUSB) Asynchronous Wrapper

Saturday, December 8th, 2007

This is the first of a few pieces of software I’ve written for my Senior Design project here at WMU. We created a device using a Cypress PSoC, and I wrote the user interface for the project in C#. I will be releasing a number of components from this project as open source.

If you’ve used the Cypress SuiteUSB .NET library, you’ll notice that they say asynchronously sending and receiving data to and from the device is the difficult way to access the device. Of course, one thing that contributes to the difficulty of using their interface is that its nothing like any other .NET asynchronous operations. If you have used .NET asynchronous sockets or file writing… well, you probably realize that the way Cypress decided to implement their library is definitely NOT .NET oriented, and in general is rather annoying for .NET programmers to use.

Well, it annoyed the heck out of me too, so I wrote a nice wrapper for it. Now, asynchronous sending and receiving is trivial. 🙂

This will allow you to wrap the EndPoints exposed by any CyUSBDevice, so that you can call .NET-style Begin/End asynchronous calls that use IO completion ports to work nicely and quickly. I do not have any benchmarks for it at this time, but I’d bet that it works faster than Cypress’s library.. of course, I could be wrong.

Does NOT support Isochronous transfers. Probably does not support ControlEndPoints either.

A set of demo code is provided.

Usage

It’s pretty trivial to use this class. Create an instance of it, then use it like any other BeginXXX/EndXXX pair in .NET. For example:

CyUSBAsyncWrapper usbComm;

// create the wrapper
private void Init(CyUSBDevice usbDevice){
	usbComm = new CyUSBAsyncWrapper(usbDevice.BulkOutEndPt);
}

// call this to begin transferring the data
private void SendData(byte [] data) {
	usbComm.BeginTransfer(data, data.Length, new AsyncCallback(EndTransfer), data);
}

// called on another thread when the transfer is complete
void EndTransfer(IAsyncResult result) {
	usbComm.EndTransfer(result);
}

Licensed under a BSD-style license. Let me know if you find any bugs.

Download: Link

Visual Studio 2005 not putting User Controls into Toolbox

Saturday, October 6th, 2007

I’ve been developing in C# for my Senior Design project, and I have a lot of user controls that I’ve been using in the project, and while most of them are instantiated at runtime, a few needed to be embedded into the forms. And of course, I couldn’t actually do that since I couldn’t drag any of the controls into the Toolbox, even with a successful build. After playing with it for awhile, I found the solution.

It was as easy as this:

Tools -> Options ->  Windows Form Designer  -> General -> AutoToolboxPopulate = true

For some reason, it was not set on my desktop but was set on my laptop. So odd… hope that helps ya if you happen to have that problem!

C# IPC: Access Denied in Windows Vista

Thursday, May 31st, 2007

Microsoft introduced a new form of Remoting in .NET 2.0 under System.Runtime.Remoting.Channels.Ipc, which used named pipes for remoting instead of HTTP or TCP connections. The named pipes are supposed to be much faster to use (which seems to be true) and work extremely well on a local machine. And, they work flawlessly by default in Windows XP. Of course, the remoting setup is practically the same as other remoting methods (refer to a remoting tutorial for more information on how remoting works).

However, in Windows Vista, they changed around the way named pipes work, and they haven’t clearly documented it in their documentation. Windows Vista has a stricter ACL created by default for named pipes. Heres the code that lets your service communicate with your code running in userspace:


IpcServerChannel channel;

IDictionary properties = new Hashtable();
properties.Add("authorizedGroup", "Users");
properties.Add("portName", "NCChannel");
properties.Add("rejectRemoteRequests", true);

// create the channel and start it up
channel = new IpcServerChannel(properties,null);

Pretty simple, eh? They should probably mention it a little bit more explicitly in their docs..