使用VS.NET 2005创建COM互操作? [英] Creating a COM interop using VS.NET 2005?

查看:52
本文介绍了使用VS.NET 2005创建COM互操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#.NET 2005创建和使用COM对象。


程序集设置为Register for COM interop但是当我试图在Word 2003上从VB调用它时,我收到了这个错误:


运行时错误''-2147024894(80070002) '':

找不到文件或程序集名称COMTest3或其中一个依赖项。


COM的代码:


使用System;

使用System.Collections.Generic;

使用System.Text;

使用System.Runtime .InteropServices;


名称空间COMTest3

{

[Guid(" EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")) ]

公共接口ComNameITInterface

{

[DispId(1)]

string GetMyName();

}


[Guid(" 7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),

InterfaceType(ComInterfaceType.InterfaceIsIDispatc h) )]

公共界面ComNameITEvents

{

}


[Guid(" 0D53A3E8-) E51A-49C7-944E-E72A2064F938),

ClassInterface(Cl assInterfaceType.None),

ComSourceInterfaces(typeof(ComNameITEvents))]


公共类NameIT:ComNameITInterface

{

公共字符串GetMyName()

{

返回" ABCDEF" ;;

}

}

}

用于调用COM的VB代码:


私有子文档_新()

Dim x As New COMTest3.NameIT

MsgBox x.GetMyName()

设置x = Nothing

End Sub


感谢您的帮助,

Asaf

I am trying to create and use a COM object with C#.NET 2005.

The assembly is set to "Register for COM interop" but when I am trying to
call it from VB on Word 2003 I am getting this error:

Run-time error ''-2147024894 (80070002)'':
File or assembly name COMTest3, or one of its dependencies, was not found.

The code for the COM:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace COMTest3
{
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
public interface ComNameITInterface
{
[DispId(1)]
string GetMyName();
}

[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h)]
public interface ComNameITEvents
{
}

[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ComNameITEvents))]

public class NameIT : ComNameITInterface
{
public string GetMyName()
{
return "ABCDEF";
}
}
}
The code for the VB to call the COM:

Private Sub Document_New()
Dim x As New COMTest3.NameIT
MsgBox x.GetMyName()
Set x = Nothing
End Sub

Thanks for any help,
Asaf

推荐答案

您好AG70,


感谢您的更新!

正如我在上一篇文章中所说,默认情况下ComVisible = false,所以即使是
如果你检查了注册COM互操作,COM客户端将不会看到界面。

请尝试下面的代码来构建你的.NET类库并暴露给

com。

注意:我已经设置好了每个类中的ComVisible属性为true,并且我需要公开
接口。


使用System;

使用System.Collections .Generic;

使用System.Text;

使用System.Runtime.InteropServices;

名称空间COMTest3

{

[Guid(" EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F"),

InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]

公共接口ComNameITInterface

{

[DispId(1)]

string GetMyName();

}

[Guid(" 7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),

InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]

公共接口ComNameITEvents

{

}

[Guid(" 0D53A3E8-E51A-49C7-944E-E72A2064F938" ),

ClassInterface(ClassInterfaceType.None),

ComSourceInterfaces(typeof(ComNameITEvents)),ComVi sible(true)]

pub lic class NameIT:ComNameITInterface

{

public string GetMyName()

{

return" ABCDEF" ;;

}

}


}


祝你好运,


Peter Huang

Microsoft在线合作伙伴支持


安全! - www.microsoft.com/security

此帖子原样是按原样提供的。没有保证,也没有授予任何权利。

Hi AG70,

Thanks for your update!
As I said in your previous post, the ComVisible = false by default, so even
if you has check the "Register for COM interop", the COM client will not
see the interface.
Please try the code below to build your .NET class library and expose to
com.
NOTE: I have set the ComVisible attribute to true in each class and
interface I need to expose.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace COMTest3
{
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]
public interface ComNameITInterface
{
[DispId(1)]
string GetMyName();
}
[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]
public interface ComNameITEvents
{
}
[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ComNameITEvents)),ComVi sible(true)]
public class NameIT : ComNameITInterface
{
public string GetMyName()
{
return "ABCDEF";
}
}

}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


你好彼得,


非常感谢你的帮助。

我已经再次发布这个话题了,因为出于某些原因我没有看到你用Outlook Express新闻阅读器回复你的b $ b回复而且我还没有收到通知

回复我的电子邮件。


我会检查您的解决方案,并会通过新闻组更新。


再次感谢您的帮助,

Asaf

"" Peter Huang" [MSFT] QUOT;写道:
Hello Peter,

Thanks you very much for your help.
I have posted this topic again because from some reason I haven''t seen your
reply with Outlook express news reader and I haven''t received a notification
for your reply to my email.

I will check your solution and will update you by news groups.

Thanks again for your help,
Asaf
""Peter Huang" [MSFT]" wrote:
您好AG70,

感谢您的更新!
正如我在上一篇文章中所说,默认情况下ComVisible = false,所以即使
如果您检查了注册COM互操作,COM客户端也不会看到界面。
请尝试下面的代码来构建您的.NET类库并公开
com。
注意:我已经在每个类和我需要公开的界面中将ComVisible属性设置为true。

使用System;
使用System.Collections.Generic;
使用System.Text;
使用System.Runtime.InteropServices;

命名空间COMTest3
{Guid(" EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]
公共接口ComNameITInterface
{
[DispId(1) ]
字符串GetMyName();
}

[Guid(" 7BD20046-DF8C-44A6-8F6B-687FAA26FA71& ;),

InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]
公共接口ComNameITEvents


[Guid(" 0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ComNameITEvents)),ComVi sible(true)]
公共类NameIT:ComNameITInterface
{
公共字符串GetMyName()
{
返回" ABCDEF" ;;
}
}

}

致以最诚挚的问候,

Peter Huang
微软在线合作伙伴支持

获得安全保障! - www.microsoft.com/security
此帖子提供按原样没有保证,也没有赋予任何权利。
Hi AG70,

Thanks for your update!
As I said in your previous post, the ComVisible = false by default, so even
if you has check the "Register for COM interop", the COM client will not
see the interface.
Please try the code below to build your .NET class library and expose to
com.
NOTE: I have set the ComVisible attribute to true in each class and
interface I need to expose.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace COMTest3
{
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]
public interface ComNameITInterface
{
[DispId(1)]
string GetMyName();
}
[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]
public interface ComNameITEvents
{
}
[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ComNameITEvents)),ComVi sible(true)]
public class NameIT : ComNameITInterface
{
public string GetMyName()
{
return "ABCDEF";
}
}

}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.



Hello Peter,


我创建了一个新的COM Interop对象,带有C#.NET 2005,带有

ComVisible(true)但是很容易出错:


--------- ------------------

Microsoft Visual Basic

------------- --------------

运行时错误''-2147024894(80070002)'':

文件或程序集名称COMTest5,或其中一个依赖项,找不到。


我的C#代码是:


使用系统;

使用System.Collections.Generic;

使用System.Text;

使用System.Runtime.InteropServices;


命名空间COMTest5

{

[Guid(" E6750599-3B77-4606-970E-E05D590DD0AC"),

InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible (true)]

公共接口ComNameITInterface

{

[DispId(1)]

string GetMyName();

}


[Guid(" BA9896AE-0386-4d3c-BCBF-2B0F7594495A"),

InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]

公共接口ComNameITEvents

{

}

[Guid(" 9DB718A0-629E-4e27-91AD-D47FF5160464"),

ClassInterface(ClassInterfaceType.None),

ComSourceInterfaces(typeof(ComNameITEvents) )),ComVisible(true)]


公共类NameIT:ComNameITInterface

{

public string GetMyName()

{

返回" ABCDEF" ;;

}

}

}


我的VB代码是:


Private Sub Document_New()

Dim x As New COMTest5.NameIT

MsgBox x.GetMyName()

设置x =无任何

结束子

问候,

Asaf


" Asaf"写道:
Hello Peter,

I have created a new COM Interop object with C#.NET 2005 with
ComVisible(true) but stiil getting the error:

---------------------------
Microsoft Visual Basic
---------------------------
Run-time error ''-2147024894 (80070002)'':
File or assembly name COMTest5, or one of its dependencies, was not found.

My C# Code is:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace COMTest5
{
[Guid("E6750599-3B77-4606-970E-E05D590DD0AC"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h), ComVisible(true)]
public interface ComNameITInterface
{
[DispId(1)]
string GetMyName();
}

[Guid("BA9896AE-0386-4d3c-BCBF-2B0F7594495A"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h), ComVisible(true)]
public interface ComNameITEvents
{
}

[Guid("9DB718A0-629E-4e27-91AD-D47FF5160464"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ComNameITEvents)), ComVisible(true)]

public class NameIT : ComNameITInterface
{
public string GetMyName()
{
return "ABCDEF";
}
}
}

My VB Code is:

Private Sub Document_New()
Dim x As New COMTest5.NameIT
MsgBox x.GetMyName()
Set x = Nothing
End Sub
Regards,
Asaf

"Asaf" wrote:
你好彼得,

非常感谢你的帮助。
我再次发布这个话题,因为出于某种原因我没有'没看到你用Outlook Express新闻阅读器的回复,我没有收到你回复我的电子邮件的通知。

我会检查你的解决方案并会更新您是新闻组的。

再次感谢您的帮助,
Asaf

"" Peter Huang" [MSFT] QUOT;写道:
Hello Peter,

Thanks you very much for your help.
I have posted this topic again because from some reason I haven''t seen your
reply with Outlook express news reader and I haven''t received a notification
for your reply to my email.

I will check your solution and will update you by news groups.

Thanks again for your help,
Asaf
""Peter Huang" [MSFT]" wrote:
您好AG70,

感谢您的更新!
正如我在上一篇文章中所说,默认情况下ComVisible = false,所以即使
如果您检查了注册COM互操作,COM客户端也不会看到界面。
请尝试下面的代码来构建您的.NET类库并公开
com。
注意:我已经在每个类和我需要公开的界面中将ComVisible属性设置为true。

使用System;
使用System.Collections.Generic;
使用System.Text;
使用System.Runtime.InteropServices;

命名空间COMTest3
{Guid(" EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]
公共接口ComNameITInterface
{
[DispId(1) ]
字符串GetMyName();
}

[Guid(" 7BD20046-DF8C-44A6-8F6B-687FAA26FA71&q uot;),

InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]
公共接口ComNameITEvents



[Guid(" 0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ComNameITEvents)),ComVi sible(true)]
公共类NameIT:ComNameITInterface
{
公共字符串GetMyName()
{
返回" ABCDEF" ;;
}
}

}

致以最诚挚的问候,

Peter Huang
微软在线合作伙伴支持

获得安全保障! - www.microsoft.com/security
此帖子提供按原样没有保证,也没有授予任何权利。
Hi AG70,

Thanks for your update!
As I said in your previous post, the ComVisible = false by default, so even
if you has check the "Register for COM interop", the COM client will not
see the interface.
Please try the code below to build your .NET class library and expose to
com.
NOTE: I have set the ComVisible attribute to true in each class and
interface I need to expose.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace COMTest3
{
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]
public interface ComNameITInterface
{
[DispId(1)]
string GetMyName();
}
[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatc h),ComVisible(true)]
public interface ComNameITEvents
{
}
[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ComNameITEvents)),ComVi sible(true)]
public class NameIT : ComNameITInterface
{
public string GetMyName()
{
return "ABCDEF";
}
}

}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.



这篇关于使用VS.NET 2005创建COM互操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆