.NET Framework Bookmark and Share   
 index > 64-Bit .NET Framework Development > Problems Exposing .NET class to COM (Excel addin)
 

Problems Exposing .NET class to COM (Excel addin)

Hi,

I am really hoping someone here helps me out. Have been struggling with this probelm since last 2 days now.

What I am trying to do isexpose a very simple .NET (C#) class to Excel as an addin via COM. And as I understand this is a very standard process.

Below is the code that I am using and it all works fine on my LOCAL computer. i.eafter all the registration etc. I am able to open up my excel and type in the C# function name i.e. =getname() and am able to see the expected value. The real issue is when I try to copy this dll and register it on someone else's computer it would not work.

I have ensured the registry looks the same both on my and others computer after registering thruthe regasm tool. Also on other's computer I am able to go into excel Tools/Add-ins and see the .NET/COM class that I just registered but its just when I try to fire one of its methods in the spreadsheet I dont see anything but the standard "#NAME?"

So this is the following I am doing:

1. Successfully compile the class lib C# project
2. Generate the key pair by running sn.exe -k key.snk and modify the AssemblyInfo.cs appropriately and it all works fine
3. Once compiled I run the regasm command to generate the .tlb file and register to the registry: C:\>regasmTestExcelAddin.dll /tlb:TestExcelAddin.tlb and it all works fine
4. Add it to the GAC: gacutil /i TestExcelAddin.dll and that also works fine (since its strongly named and all)

After that I go to Excel click on Tools/Add-ins/Automation and select my registered addin class TestExcelAddin.Test and OK and call the function in the spreadsheet and works fine.

Now following the same procedure on someone else's computer does not work (they do have the same .net version and same excel version). On others computers I am successfully able to run all the above commands but its just when I type in the function in excel it shows me "#NAME?" instead of the value.

So where am I going wrong. Please help. Below is the code for reference:

namespace

TestExcelAddin

{

[

Guid("A641893E-BECE-4681-B136-D486F2B751E8")]

[

InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]

public interface _Test

{

[

DispId(1)]

int GetDay();

}

[

Guid("87C20D72-9582-4887-88EC-7CF9B84EB610")]

[

ClassInterface(ClassInterfaceType.None)]

[

ProgId("Tester.Numbers")]

public class Test : _Test

{

public Test(){}

public int GetDay()

{

return (DateTime.Today.Day); }

[

ComRegisterFunctionAttribute]

public static void RegisterFunction(Type type)

{

Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type, "Programmable"));

RegistryKey key = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type, "InprocServer32"), true);

key.SetValue(

"", Environment.SystemDirectory + @"\mscoree.dll", RegistryValueKind.String);

}

[

ComUnregisterFunctionAttribute]

public static void UnregisterFunction(Type type)

{

Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type, "Programmable"), false); }

private static string GetSubKeyName(Type type, string subKeyName)

{

StringBuilder s = new System.Text.StringBuilder();

s.Append(

@"CLSID\{");

s.Append(type.GUID.ToString().ToUpper());

s.Append(

@"}\");

s.Append(subKeyName);

return s.ToString();

}

}

keyurshahit
How is the other machine different? Does it have a 64-bit operating system? Are you using the correct version of Regasm.exe in that case? Did you make sure to turn off UAC before you ran Regasm.exe? Your [ComRegisterFunction] is not complete. Comment it out to see what registry entries are made by Regasm.exe. Note that your [ComUnregisterFunction] doesn't correctly clean up the registry.

Hans Passant.
nobugz

Thanks for your quick response!

All the machines have the exact same configuration. Also all are 32 bit computers.

I tried to comment out the COMRegister/Unregister code and redeployed on my local machine and works fine as before BUT not on others machine. On others machines while trying to add this class as an add-in it complains that file mscoree.dll not found and hence dosent work. That registration code is for that same reason. So I believe we do need that registration code. What else could I be missing?

keyurshahit

You can use google to search for other answers

Custom Search

More Threads

• The specified module could not be found. (Exception from HRESULT: 0x8007007E)
• Refcounting issue in COM-.NET interop with beta2 framework
• Found edge to retarget after airlock
• 64 bit PE File Format(Native and .Net)
• Unable to run J# Web Site on 64-bit Server
• Porting from 32-bit to 64-bit
• Can't see Debug output from native-coce sub-component
• Creating 64-bit interop dlls from an ActiveX component
• Visual C++ .net Won't install on a 64bit platform
• GcServer Enabled=True uses TWICE the ram