创建COM对象 [英] Creating a COM object

查看:61
本文介绍了创建COM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




创建OCX COM组件的正确方法是什么。该组件已经注册了,但是无法创建实例。我正在使用引用

来创建互操作模块。

如果我使用javascript或vbscript它可以工作。


我会感谢任何帮助。


我做以下(C#):

尝试#1:


使用系统;

使用MTMicrImage; //对组件的引用


public class oMirImage:MTMicrImage.MicrImage //派生自界面

{

//创建在实现中使用它的基础实例

private MTMicrImage.MicrImageClass obase = new

MTMicrImage.MicrImageClass();


public oMicrImage()

{

}

#region方法

//全部方法去这里

#endregion


#region properties

//所有房产都在这里

#endregion

}



如果类继承自base clalss和inteface,则会发生同样的事情:

如下: />
..

..

公共类oMirImage:MTMicrImage.MTMicrImageClass,

MTMicrImage.MicrImage //从接口派生

尝试#2


使用系统;

使用MTMicrImage; //使用System.Reflections引用组件

;


public class oMirImage:MTMicrImage.MicrImage //派生自界面

{

private MTMicrImage.MicrImageClass obase; //他的作品不是新

被指定,但是具有空值。


public oMicrImage()

{

}


私有对象createInstance()

{


object obj = null;

尝试

{

obj =

Activator.CreateCOMObjectFrom" c:\\Interop .file.dll",

" InstanceMane");

}

catch(例外e)

{


}

返回obj;

}


#region方法

//所有方法都在这里

#endregion


#region properties

//全部物业到这里

#endregion

}


与上述相同的问题。


想要一些奇怪的东西!阅读以下内容:

如果我使用vb脚本或javascript它可以工作:

//使用javascript

var oMicrImage = WScript.CreateObject(" MTMicrImage.MicrImage")


Thjis有效,它可以正确创建对象。


感谢您的时间。


Carlos Lozano

解决方案

如果添加对COM对象的引用,将在
中创建一个互操作类
你的解决方案。然后,您可以访问COM对象的所有功能

,就像它是C#类一样。


-

Bob Powell [MVP]

Visual C#,System.Drawing


Ramuseco Limited .NET咨询
http://www.ramuseco.com

在Windows窗体提示中查找精彩的Windows窗体文章技巧
http://www.bobpowell.net/tipstricks.htm


用GDI + FAQ回答那些GDI +问题
http://www.bobpowell.net/faqmain.htm


所有新文章都提供C#和VB.NET中的代码。

订阅提供的RSS提要,绝不会错过任何新文章。


" Carlos Lozano" <钙********** @ discussions.microsoft.com>在留言中写道

news:47 ********************************** @ microsof t.com ...



创建OCX COM组件的正确方法是什么。该组件已经注册,但无法创建实例。我正在使用
参考创建的互操作模块。
如果我使用javascript或vbscript它可以工作。

我将不胜感激任何帮助。

我做了以下(C#):
尝试#1:

使用System;
使用MTMicrImage; //对组件的引用
公共类oMirImage:MTMicrImage.MicrImage //从接口派生
{创建基础实例以在实现中使用它<私人MTMicrImage.MicrImageClass obase = new
公共场合oMicrImage()
#region方法
//所有方法都在这里
#endregion

#region properties
//所有房产都在这里
#endregion
编译时我得到一个错误,说MTMicrImage.MicrImageClass和MTMicrImage.MicrImageClass。由于其保护级别而无法访问
如果类继承自base clalss和inteface
如下所示,则会发生同样的事情:


公共类oMirImage:MTMicrImage.MTMicrImageClass,
MTMicrImage.MicrImage //派生自界面

尝试#2

使用系统;
使用MTMicrImage; //使用System.Reflections引用组件
;公共类oMirImage:MTMicrImage.MicrImage //派生自界面
{
private MTMicrImage.MicrImageClass obase; //他的作品不是新的
被指定,但是具有空值。

public oMicrImage()



/>私有对象createInstance()
对象obj = null;
尝试
{
obj =
Activator.CreateCOMObjectFrom" c :\\Interop.file.dll",
" InstanceMane");
}
catch(例外e)


}
返回obj;
}

#region方法
//所有方法都在这里
#endregion

#region物业
//所有物业都在这里
#endregion
}

同样的问题如上所述。

想要一些奇怪的东西!请阅读以下内容:
如果我使用vb脚本或javascript它可以工作:
//使用javascript
var oMicrImage = WScript.CreateObject(" MTMicrImage.MicrImage")

> Thjis有效,它创造的对象恰到好处。

感谢您的时间。

Carlos Lozano


嗨Bob,


感谢您的时间。


是的,设置引用会创建互操作类。我可以在程序集中看到

接口,类和所有内容,但是不能创建一个

实例。不知道为什么。


公开基本代码的代码是:


使用系统;

使用InteropClass;


public class myClass:InteropClass.Interface

{

private InteropClass.OCXClass baseClass = new

InteropClass.OCXClass();


public myClass()

{

}

#region从界面创建的所有属性和方法


我只是在想这个问题。


我可以在javascript中创建的实例是直接调用OCX,而不是互操作程序集。我在想可能有问题。可怕的是,

互操作在创建时没有从OCX获得所有信息。


有没有办法通过直接调用OCX来创建实例来自C#

(不使用互操作)。即。我可以在一个非托管部分打电话吗?


谢谢,


Carlos Lozano写道:

嗨鲍勃,

感谢您的时间。

是的,设置引用会创建互操作类。我可以在程序集中看到
接口,类和所有内容,但是无法创建
实例。不知道为什么。

公开基本代码的代码是:

使用System;
使用InteropClass;

公共类myClass:InteropClass.Interface
私有InteropClass.OCXClass baseClass = new
InteropClass.OCXClass();

公共myClass()
{
}

#region从界面创建的所有属性和方法


#endregion
}

在编译时,我得到一个错误,说InteropClass.OCXClass因为它的保护级别不可访问。
我检查了类定义是公共的,但似乎它没有构造函数。
但是,使用vbscript或javascript函数CreateObject(....) ,工作正常。

任何想法?

Carlos Lozano

" Bob Powell [MVP]"写道:

如果您添加对COM对象的引用,将在您的解决方案中创建一个互操作类。然后,您可以访问COM对象的所有功能,就像它是C#类一样。

- 鲍勃鲍威尔[MVP]
Visual C#,系统。绘图

Ramuseco Limited .NET咨询
http:// www .ramuseco.com

在Windows窗体提示和技巧中找到优秀的Windows窗体文章
http://www.bobpowell.net/tipstricks.htm

使用GDI +常见问题回答这些GDI +问题
http://www.bobpowell.net/faqmain.htm
<所有新文章都提供了C#和VB.NET中的代码。
订阅提供的RSS源,绝不会错过任何新文章。


" Carlos Lozano" <钙********** @ discussions.microsoft.com>在消息中写道
新闻:47 ********************************** @ microsof t.com。 ..



创建OCX COM组件的正确方法是什么。该组件已经注册,但无法创建实例。我正在使用
参考创建的互操作模块。
如果我使用javascript或vbscript它可以工作。

我将不胜感激任何帮助。

我做了以下(C#):
尝试#1:

使用System;
使用MTMicrImage; //对组件的引用
公共类oMirImage:MTMicrImage.MicrImage //从接口派生
{创建基础实例以在实现中使用它<私人MTMicrImage.MicrImageClass obase = new
公共场合oMicrImage()
#region方法
//所有方法都在这里
#endregion

#region properties
//所有房产都在这里
#endregion
编译时我得到一个错误,说MTMicrImage.MicrImageClass和MTMicrImage.MicrImageClass。由于其保护级别而无法访问
如果类继承自base clalss和inteface
如下所示,则会发生同样的事情:


公共类oMirImage:MTMicrImage.MTMicrImageClass,
MTMicrImage.MicrImage //派生自界面

尝试#2

使用系统;
使用MTMicrImage; //使用System.Reflections引用组件
;公共类oMirImage:MTMicrImage.MicrImage //派生自界面
{
private MTMicrImage.MicrImageClass obase; //他的作品不是新的
被指定,但是具有空值。

public oMicrImage()



/>私有对象createInstance()
对象obj = null;
尝试
{
obj =
Activator.CreateCOMObjectFrom" c :\\Interop.file.dll",
" InstanceMane");
}
catch(例外e)


}
返回obj;
}

#region方法
//所有方法都在这里
#endregion

#region物业
//所有物业都在这里
#endregion
}

同样的问题如上所述。

想要一些奇怪的东西!请阅读以下内容:
如果我使用vb脚本或javascript它可以工作:
//使用javascript
var oMicrImage = WScript.CreateObject(" MTMicrImage.MicrImage")

> Thjis有效,它创造的对象恰到好处。

感谢您的时间。

Carlos Lozano




Hi,

What is the right way to create an OCX COM component. The component is
already registerred, but can''t create an instance. I am using the reference
to the interop module created.
If I use javascript or vbscript it works.

I will appreciate any help.

I do the following (C#):
Attempt # 1:

using System;
using MTMicrImage; // reference to the component

public class oMirImage : MTMicrImage.MicrImage // derives from interface
{
// create an instance of the base to use it in the implementation
private MTMicrImage.MicrImageClass obase = new
MTMicrImage.MicrImageClass();

public oMicrImage()
{
}

#region methods
// All methods go here
# endregion

#region properties
// All properties go here
#endregion
}

Whn compiling I get an error saying "MTMicrImage.MicrImageClass " is not
accessible due to its protection level
Same happend if the class inherits from the base clalss and the inteface as
follows:
..
..
public class oMirImage : MTMicrImage.MTMicrImageClass,
MTMicrImage.MicrImage // derives from interface
Attempt # 2

using System;
using MTMicrImage; // reference to the component
using System.Reflections;

public class oMirImage : MTMicrImage.MicrImage // derives from interface
{
private MTMicrImage.MicrImageClass obase; // his works of not "new"
is specified, but has null value.

public oMicrImage()
{
}

private object createInstance()
{

object obj = null;
try
{
obj =
Activator.CreateCOMObjectFrom"c:\\Interop.file.dll ",
"InstanceMane");
}
catch (Exception e)
{

}
return obj;
}

#region methods
// All methods go here
# endregion

#region properties
// All properties go here
#endregion
}

Same problem as above.

Want something weird! Read below:
If I use vb scripting or javascript it works:
//using javascript
var oMicrImage = WScript.CreateObject("MTMicrImage.MicrImage")

Thjis works, it creates the object just right.

Thank you for your time.

Carlos Lozano

解决方案

If you add a reference to the COM object an interop class will be created in
your solution. You can then access all the functionality of the COM object
as though it were a C# class.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Carlos Lozano" <Ca**********@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...

Hi,

What is the right way to create an OCX COM component. The component is
already registerred, but can''t create an instance. I am using the
reference
to the interop module created.
If I use javascript or vbscript it works.

I will appreciate any help.

I do the following (C#):
Attempt # 1:

using System;
using MTMicrImage; // reference to the component

public class oMirImage : MTMicrImage.MicrImage // derives from interface
{
// create an instance of the base to use it in the implementation
private MTMicrImage.MicrImageClass obase = new
MTMicrImage.MicrImageClass();

public oMicrImage()
{
}

#region methods
// All methods go here
# endregion

#region properties
// All properties go here
#endregion
}

Whn compiling I get an error saying "MTMicrImage.MicrImageClass " is not
accessible due to its protection level
Same happend if the class inherits from the base clalss and the inteface
as
follows:
.
.
public class oMirImage : MTMicrImage.MTMicrImageClass,
MTMicrImage.MicrImage // derives from interface
Attempt # 2

using System;
using MTMicrImage; // reference to the component
using System.Reflections;

public class oMirImage : MTMicrImage.MicrImage // derives from interface
{
private MTMicrImage.MicrImageClass obase; // his works of not "new"
is specified, but has null value.

public oMicrImage()
{
}

private object createInstance()
{

object obj = null;
try
{
obj =
Activator.CreateCOMObjectFrom"c:\\Interop.file.dll ",
"InstanceMane");
}
catch (Exception e)
{

}
return obj;
}

#region methods
// All methods go here
# endregion

#region properties
// All properties go here
#endregion
}

Same problem as above.

Want something weird! Read below:
If I use vb scripting or javascript it works:
//using javascript
var oMicrImage = WScript.CreateObject("MTMicrImage.MicrImage")

Thjis works, it creates the object just right.

Thank you for your time.

Carlos Lozano



Hi Bob,

Thank you for your time.

Yes, setting the reference creates the interop class. I can see the
interfaces, classes and everything in the assembly, but can''t create an
instance. Do not know why.

The code to expose the base code is:

using System;
using InteropClass;

public Class myClass : InteropClass.Interface
{
private InteropClass.OCXClass baseClass = new
InteropClass.OCXClass();

public myClass()
{
}

#region All properties and methods created from the interface


I was just thinking about the problem.

The instance I can create in javascript is by calling the OCX directly, not
the interop assembly. I am thinking there may be the problem. Poblably the
interop didn''t get all the information from the OCX when created.

Is there a way to create an instance by calling the OCX directly from C#
(not using the interop). ie. Can I call it within an unmanaged section?

Thank you,

"Carlos Lozano" wrote:

Hi Bob,

Thank you for your time.

Yes, setting the reference creates the interop class. I can see the
interfaces, classes and everything in the assembly, but can''t create an
instance. Do not know why.

The code to expose the base code is:

using System;
using InteropClass;

public Class myClass : InteropClass.Interface
{
private InteropClass.OCXClass baseClass = new
InteropClass.OCXClass();

public myClass()
{
}

#region All properties and methods created from the interface
.
.
#endregion
}

When compiling I get an error saying "InteropClass.OCXClass" is not
accessible due to its protection level.
I checked the class definition is public, but seems it has not constructor.
However, using vbscript or javascript function CreateObject(....), works fine.

Any ideas?

Carlos Lozano
"Bob Powell [MVP]" wrote:

If you add a reference to the COM object an interop class will be created in
your solution. You can then access all the functionality of the COM object
as though it were a C# class.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Carlos Lozano" <Ca**********@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...

Hi,

What is the right way to create an OCX COM component. The component is
already registerred, but can''t create an instance. I am using the
reference
to the interop module created.
If I use javascript or vbscript it works.

I will appreciate any help.

I do the following (C#):
Attempt # 1:

using System;
using MTMicrImage; // reference to the component

public class oMirImage : MTMicrImage.MicrImage // derives from interface
{
// create an instance of the base to use it in the implementation
private MTMicrImage.MicrImageClass obase = new
MTMicrImage.MicrImageClass();

public oMicrImage()
{
}

#region methods
// All methods go here
# endregion

#region properties
// All properties go here
#endregion
}

Whn compiling I get an error saying "MTMicrImage.MicrImageClass " is not
accessible due to its protection level
Same happend if the class inherits from the base clalss and the inteface
as
follows:
.
.
public class oMirImage : MTMicrImage.MTMicrImageClass,
MTMicrImage.MicrImage // derives from interface
Attempt # 2

using System;
using MTMicrImage; // reference to the component
using System.Reflections;

public class oMirImage : MTMicrImage.MicrImage // derives from interface
{
private MTMicrImage.MicrImageClass obase; // his works of not "new"
is specified, but has null value.

public oMicrImage()
{
}

private object createInstance()
{

object obj = null;
try
{
obj =
Activator.CreateCOMObjectFrom"c:\\Interop.file.dll ",
"InstanceMane");
}
catch (Exception e)
{

}
return obj;
}

#region methods
// All methods go here
# endregion

#region properties
// All properties go here
#endregion
}

Same problem as above.

Want something weird! Read below:
If I use vb scripting or javascript it works:
//using javascript
var oMicrImage = WScript.CreateObject("MTMicrImage.MicrImage")

Thjis works, it creates the object just right.

Thank you for your time.

Carlos Lozano




这篇关于创建COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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