0

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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 ?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.