July 13, 2010

Testing COM components

Here's some code you can use to try to load your Type through COM.
You can use the GetPEKind method to get the platform that the assembly was compiled for.

(Recently ran into a problem where I couldn't be sure the assembly I'd registered was registered correctly, my query was particular to win64, there are some changes in the registry entries for 64bit windows when running 32 bit applications.)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Reflection;

namespace ComTest

class MyGetTypeFromCLSIDSample
public static void Main()
Guid myGuid1 = new Guid("3A21CE6B-8571-4955-9780-BAE1EE3215C0");//my Types GUID, regasm was ran on this assembly.

// Get the type associated with the CLSID
// and specify whether to throw an exception if an error occurs
// while loading the type.

//if this shows The type of the GUID is System.__ComObject.
//The current module is mscorlib.dll.
//assembly kind ILOnly, PE32Plus , platform AMD64
//then your assembly isn't registered.

Type myType1 = Type.GetTypeFromCLSID(myGuid1, true);
Console.WriteLine("The GUID associated with myType1 is {0}.", myType1.GUID);
Console.WriteLine("The type of the GUID is {0}.", myType1.ToString());

// Show the current module.
Module m = myType1.Module;
Console.WriteLine("The current module is {0}.", m.Name);

PortableExecutableKinds peKind;
ImageFileMachine machine;
m.GetPEKind(out peKind, out machine);

Console.WriteLine("assembly kind {0} , platform {1}", peKind.ToString(), machine);//assembly kind ILOnly , platform I386


No comments: