views:

69

answers:

2

I need to create a class that can be accessible through the classic asp's Server.CreateObject method, and that exposes 3 properties (int Width, int Height, bool Loaded) and 3 methods (void Load(string locatoin), void Resize(int width, int height), void Save(string location)). All my attempts so far has been unsuccessful.

Any help would be appreciated.

A: 

Building a .NET COM Object can be somewhat difficult.

As you haven't told us what you've tried so far, the best I can do is point you to a good step-by-step guide:

Build and Deploy a .NET COM Assembly

Justin Niessner
I've tried following that tutorial and ended up with the code at http://alxandr.pastebin.com/f46f0d649. Problem is that it dosn't work. I use a OLE/COM Object Browser and when double-hitting the class "FdbImg.Image" I get the error "Not implemented".Any thoughts about why?
Alxandr
A: 

Building the object is very easy - registering it and managing the COM dependency can be quite tricky.

Your .NET project should be a class library, and your class can be a straightfoward C# / .NET CLR object:

namespace MyCompany.MyProject.Com {
  public class MyObject {
      public int Width { get; set; }
      public int Height { get; set; }
      public void Load(string location) { /* implementation here */ }
      public void Resize(int width, int height) { /* implementation here */ }
  }
}

Right-click your project, select Properties, Application, click Assembly Information... and ensure that "Make assembly COM-Visible" is selected at the bottom of the Assembly Information dialog.

Build your project - you should end up with MyCompany.MyProject.Com.dll in your \bin\debug\ folder.

Build a simple ASP webpage that looks like this:

<% option explicit %>
<%
dim myObject
set myObject = Server.CreateObject("MyCompany.MyProject.Com.MyObject")
myObject.Width = 20
myObject.Height = 40
%>
<html>
<head>COM Interop Demo</head>
<body>
<p>Width + Height = <%= myObject.Width + myObject.Height %></p>
</body>
</html>

Bring up that page on http://localhost/ and verify that you get "Server.CreateObject failed" the first time you try and run it.

Now register your DLL as a COM object using regasm.exe, installed with the .NET framework:

C:\>C:\Windows\Microsoft.NET\Framework\v2.0.50727\regasm.exe /tlb MyCompany.MyProject.Com.dll     /codebase

Microsoft (R) .NET Framework Assembly Registration Utility 2.0.50727.4927
Copyright (C) Microsoft Corporation 1998-2004.  All rights reserved.

Types registered successfully
Assembly exported to 'D:\WebDlls\MyCompany.MyProject.Com.tlb', and the type library w
as registered successfully

Now refresh your web page, and you should see Width + Height = 60 in your output.

These instructions assume you're not running anything in 64-bit; if you are, it gets more complex. (You either need to run everything as 64-bit - compile a 64-bit project and use the 64-bit version of regasm.exe to register it for 64-bit COM, accessed by IIS running a 64-bit scripting host) - or manually force everything to 32-bit.

Dylan Beattie
The problem is probably that my OS is x64 then. Isn't there any way I can make it work globally? I mean, there's got to be programs or libraries out there that needs that functionality?
Alxandr
What do you mean "globally" - do you want to create a pure 64-bit COM object, or a x86/x64 dual-target COM object, or just configure your 64-bit system to run and host 32-bit COM DLLs ?
Dylan Beattie
I'd like to create a x86/x64 dual-target COM object that will work on any machine containing the dependencies. The COM-object is to be used by a web-server and I can't force anyone to use it in x64 or x86, it need to work for both.
Alxandr