I hate dredging this up as its so old, but thought i'd clear this up. After
spending the past 3 days trying everything possible to get my C# code to work
in COM, i almost gave up until seeing this post.
To put it bluntly, the documentation is wrong.
The code i was using in 1.1 DID NOT WORK in 2.0. Simple test case below:
---------- BEGIN ITVTest.cs ----------
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace comitvtest
{
[Guid("3A8E1A9D-206E-48cb-BD4F-0FE6D3155CDF")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class ITVTest : IITVTest
{
public ITVTest()
{
throw new System.NotImplementedException();
}
public int GetCurrentHour()
{
return System.DateTime.Now.Hour;
}
}
}
---------- END ITVTest.cs ----------
---------- BEGIN IITVTest.cs ----------
using System;
using System.Runtime.InteropServices;
namespace comitvtest
{
[Guid("770A5441-1B1D-4847-91B5-B84EDDC1B647")]
interface IITVTest
{
int GetCurrentHour();
}
}
---------- END IITVTest.cs ----------
--- Project Setup ---
Goto Project -> Properties
Click the "Build" tab.
Select the Register for COM Interop option
Click the "Signing" tab.
Select "Sign the Assembly", from the file name select "<new>" give it a name
i used KeyPairing.snk with NO password
Build project
start -> run: cmd
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
H:\>c:
C:\>cd projects\com.itv.test
C:\Projects\com.itv.test\bin\Release>tlbexp com.itv.test.dll
Microsoft (R) .NET Framework Assembly to Type Library Converter 2.0.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.
Assembly exported to 'C:\Projects\com.itv.test\bin\Release\com.itv.test.tlb'
C:\Projects\com.itv.test\bin\Release>regasm com.itv.test.dll
/tlb:com.itv.test.t
lb
Microsoft (R) .NET Framework Assembly Registration Utility 2.0.50727.42
Copyright (C) Microsoft Corporation 1998-2004. All rights reserved.
Assembly exported to
'C:\Projects\com.itv.test\bin\Release\com.itv.test.tlb', an
d the type library was registered successfully
--- Visual Basic 6 Test ---
Create new Standard EXE project
Goto to Project menu, select References
Find the com assembly in references list (com_itv_test), select, and hit OK
View the object browser
Types are visible here. (can also do the same with with Visual FoxPro's
Class Explorer and open the TLB file.)
--- The Proof ---
Now try it without the ComVisible attribute.
- Mark Harris
C:\Projects\com.itv.test>cd bin/release
Willy Denoyette said:
Sorry, but this is not a complete sample (missing BaseCommand), so there is
lttle I can do with it, but trust me [ComVisible(true)] IS the default.
I'm also not entirely clear on the purpose is of this class for a COM
client, true, this object creatable from COM clients, but none of it's
methods are callable.
Willy.
Vish said:
Hi,
Ok. I am not sure why but it did fix the issue. Obviously y'all know more
on
this subject than i do. But i am posting my code below if y'all are still
interested.
[ClassInterface(ClassInterfaceType.None)]
[Guid("8D0240AD-1BBE-4831-8214-39DD46A4A797")]
[ComVisible(true)]
public sealed class TestTool : BaseCommand
{
private IApplication m_app;
public override void OnCreate(object hook)
{
if (hook != null)
{
if (hook is IMxApplication)
{
m_app = (IApplication)hook;
}
}
}
public override void OnClick()
{
IMxDocument mxDoc = (IMxDocument)m_app.Document;
IActiveView activeView = mxDoc.ActiveView;
activeView.Extent = activeView.FullExtent;
activeView.Refresh();
}
public TestTool()
{
base.m_category = "Vish's Test";
base.m_caption = "Full Extent C#";
base.m_message = "Zooms the display to its full extent";
base.m_toolTip = "Full Extent C#";
base.m_name = "Developer Samples_FullExtent C#";
string[] res = GetType().Assembly.GetManifestResourceNames();
if (res.GetLength(0) > 0)
{
base.m_bitmap = new
System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream(res[0]));
}
}
#region Component Category Registration
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(String regKey)
{
MxCommands.Register(regKey);
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(String regKey)
{
MxCommands.Unregister(regKey);
}
#endregion
}
Willy Denoyette said:
Yeah I know it's always been default because it's bitten me in the
rear-end a few times ;-). I honestly didn't think that they would have
made a breaking change like that. So the question that still remains,
did
setting [ComVisible(true)] really solve the problem or was it something
else?
Jason Newell
Pretty sure it was something else (my guess: missing public on the
class).
Willy.