Open dll for COM Interop

I thought I knew how to do this, but obviously not, I would appreciate help! I cannot register my DLL, so I can create an instance in VBS or elsewhere.

I wrote the following sample class, checked โ€œBuild COM Visible,โ€ check โ€œRegister for COM Interop,โ€ and then built it. When I try to create an instance from VBS, I get the error "ActiveX component cannot create object".

This is the class code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Smurf { public class Pants { public string Explode(bool Loud) { string result; if (Loud) result = "BANG"; else result = "pop"; return result; } } } 

... and this is VBS:

 Dim a Set a = CreateObject("Smurf.Pants") msgbox("ok") 

What else do I need to do?

Thanks:)

[edit]

I forgot to mention, after the first failure, I tried REGSVR32 and REGASM - without help!

[/ edit]

Note that when I try to execute REGSVR32, I get this message:

The module "C: ... \ Smurf.dll" was loaded, but the entry point DllRegisterServer was not found. Verify that "C: ... \ Smurf.dll" is a valid DLL or OCX file, and then try again.

How useful is this?

This is the latest version of the code:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace Smurf { [Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")] public interface IPants { [DispId(1)] string Explode(bool Loud); } [Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface IPantsEvents { string Explode(bool Loud); } [ComVisible(true)] [Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"), ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(IPantsEvents))] public class Pants : IPants { public Pants() { } [ComVisible(true)] [ComRegisterFunction()] public static void DllRegisterServer(string key) { } [ComVisible(true)] [ComUnregisterFunction()] public static void DllUnregisterServer(string key) { } [ComVisible(true)] public string Explode(bool Loud) { string result; if (Loud) result = "BANG"; else result = "pop"; return result; } } } 
+8
c # visual-studio-2010 com com-interop
source share
1 answer

There may be several different things. First, you will want to use the regasm tool with the / codebase / tlb switch from an elevated command prompt (assuming Windows Vista, 7, or Windows Server 2008). Something like:

 regasm "Path to Smurf.dll" /codebase /tlb 

Once you have registered the dll using regasm, you can invoke it using VBS, VBA or VB6.

I was able to use early binding and late binding from VBA to call the Explode method. However, when I tried from VBScript, I got "ActiveX cannot create an object error as you did."

I work on a 64-bit version of Windows 7, and I remembered that this could cause problems when compiling 32-bit DLLs and running them on 64-bit operating systems. On a whim, I ran a command prompt and entered:

 C:\Windows\SysWow64\CScript.exe "Path to VBScript" 

The result was that the script worked correctly and displayed "Pop" on the screen.

Here is some simplified C # code that I used, as well as the contents of a VBScript file.

 namespace Smurf { [Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")] public interface IPants { string Explode(bool Loud); } [Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface IPantsEvents { string Explode(bool Loud); } [ComVisible(true)] [Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938")] [ClassInterface(ClassInterfaceType.None)] [ComSourceInterfaces(typeof(IPantsEvents))] public class Pants : IPants { [ComVisible(true)] public string Explode(bool Loud) { string result; if (Loud) result = "BANG"; else result = "pop"; return result; } } } 

VBScript:

 Dim x Set x = CreateObject("Smurf.Pants") MsgBox (x.Explode(False)) Set x = Nothing 
+10
source share

All Articles