473,561 Members | 3,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a COM object

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.Mic rImage // derives from interface
{
// create an instance of the base to use it in the implementation
private MTMicrImage.Mic rImageClass obase = new
MTMicrImage.Mic rImageClass();

public oMicrImage()
{
}

#region methods
// All methods go here
# endregion

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

Whn compiling I get an error saying "MTMicrImage.Mi crImageClass " 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.MTM icrImageClass,
MTMicrImage.Mic rImage // derives from interface
Attempt # 2

using System;
using MTMicrImage; // reference to the component
using System.Reflecti ons;

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

public oMicrImage()
{
}

private object createInstance( )
{

object obj = null;
try
{
obj =
Activator.Creat eCOMObjectFrom" c:\\Interop.fil e.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.CreateO bject("MTMicrIm age.MicrImage")

Thjis works, it creates the object just right.

Thank you for your time.

Carlos Lozano
Nov 17 '05 #1
15 6727
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**********@d iscussions.micr osoft.com> wrote in message
news:47******** *************** ***********@mic rosoft.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.Mic rImage // derives from interface
{
// create an instance of the base to use it in the implementation
private MTMicrImage.Mic rImageClass obase = new
MTMicrImage.Mic rImageClass();

public oMicrImage()
{
}

#region methods
// All methods go here
# endregion

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

Whn compiling I get an error saying "MTMicrImage.Mi crImageClass " 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.MTM icrImageClass,
MTMicrImage.Mic rImage // derives from interface
Attempt # 2

using System;
using MTMicrImage; // reference to the component
using System.Reflecti ons;

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

public oMicrImage()
{
}

private object createInstance( )
{

object obj = null;
try
{
obj =
Activator.Creat eCOMObjectFrom" c:\\Interop.fil e.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.CreateO bject("MTMicrIm age.MicrImage")

Thjis works, it creates the object just right.

Thank you for your time.

Carlos Lozano

Nov 17 '05 #2
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.In terface
{
private InteropClass.OC XClass baseClass = new
InteropClass.OC XClass();

public myClass()
{
}

#region All properties and methods created from the interface
Nov 17 '05 #3
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.In terface
{
private InteropClass.OC XClass baseClass = new
InteropClass.OC XClass();

public myClass()
{
}

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

When compiling I get an error saying "InteropClass.O CXClass" 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**********@d iscussions.micr osoft.com> wrote in message
news:47******** *************** ***********@mic rosoft.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.Mic rImage // derives from interface
{
// create an instance of the base to use it in the implementation
private MTMicrImage.Mic rImageClass obase = new
MTMicrImage.Mic rImageClass();

public oMicrImage()
{
}

#region methods
// All methods go here
# endregion

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

Whn compiling I get an error saying "MTMicrImage.Mi crImageClass " 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.MTM icrImageClass,
MTMicrImage.Mic rImage // derives from interface
Attempt # 2

using System;
using MTMicrImage; // reference to the component
using System.Reflecti ons;

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

public oMicrImage()
{
}

private object createInstance( )
{

object obj = null;
try
{
obj =
Activator.Creat eCOMObjectFrom" c:\\Interop.fil e.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.CreateO bject("MTMicrIm age.MicrImage")

Thjis works, it creates the object just right.

Thank you for your time.

Carlos Lozano


Nov 17 '05 #4
"baseClass" is declared as private, which makes it unreachacle from other
classes, are you sure you access baseClass from the declaring class?

....
private InteropClass.OC XClass baseClass ...

Willy.
"Carlos Lozano" <Ca**********@d iscussions.micr osoft.com> wrote in message
news:91******** *************** ***********@mic rosoft.com...
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.In terface
{
private InteropClass.OC XClass baseClass = new
InteropClass.OC XClass();

public myClass()
{
}

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

When compiling I get an error saying "InteropClass.O CXClass" 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**********@d iscussions.micr osoft.com> wrote in message
news:47******** *************** ***********@mic rosoft.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.Mic rImage // derives from
> interface
> {
> // create an instance of the base to use it in the implementation
> private MTMicrImage.Mic rImageClass obase = new
> MTMicrImage.Mic rImageClass();
>
> public oMicrImage()
> {
> }
>
> #region methods
> // All methods go here
> # endregion
>
> #region properties
> // All properties go here
> #endregion
> }
>
> Whn compiling I get an error saying "MTMicrImage.Mi crImageClass " 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.MTM icrImageClass,
> MTMicrImage.Mic rImage // derives from interface
>
>
> Attempt # 2
>
> using System;
> using MTMicrImage; // reference to the component
> using System.Reflecti ons;
>
> public class oMirImage : MTMicrImage.Mic rImage // derives from
> interface
> {
> private MTMicrImage.Mic rImageClass obase; // his works of not
> "new"
> is specified, but has null value.
>
> public oMicrImage()
> {
> }
>
> private object createInstance( )
> {
>
> object obj = null;
> try
> {
> obj =
> Activator.Creat eCOMObjectFrom" c:\\Interop.fil e.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.CreateO bject("MTMicrIm age.MicrImage")
>
> Thjis works, it creates the object just right.
>
> Thank you for your time.
>
> Carlos Lozano


Nov 17 '05 #5
Hi Willy,

Thank you Willy.

Yes, that is correct the private class object will serve to get all the base
class methods and properties.
I do not need access to the base class from outside. I will use them in the
implementation of the interface within the wrapper class.

In fact that is not the problem. The problem is to create an instance of the
base class.
using :
private Interop.OCXClas s myclass = new Interop.OCXClas s();

gives the error stated before.

I could also derive from the class and the interface to call the OCXClass as
the base implementation, but will still get the error message.

somthing like:
public Class myClass : InteropClass.OC XClass, InteropClass.In terface
{
public myClass()
{
}

public string Method()
{
return base.Method();
}

#region All properties and methods created from the interface
Nov 17 '05 #6
Are you sure the compiler complains about this statement....
private Interop.OCXClas s myclass = new Interop.OCXClas s();

How is the class declared in the interop assembly when you look at it using
Ildasm.exe?

Willy.

"Carlos Lozano" <Ca**********@d iscussions.micr osoft.com> wrote in message
news:DD******** *************** ***********@mic rosoft.com...
Hi Willy,

Thank you Willy.

Yes, that is correct the private class object will serve to get all the
base
class methods and properties.
I do not need access to the base class from outside. I will use them in
the
implementation of the interface within the wrapper class.

In fact that is not the problem. The problem is to create an instance of
the
base class.
using :
private Interop.OCXClas s myclass = new Interop.OCXClas s();

gives the error stated before.

I could also derive from the class and the interface to call the OCXClass
as
the base implementation, but will still get the error message.

somthing like:
public Class myClass : InteropClass.OC XClass, InteropClass.In terface
{
public myClass()
{
}

public string Method()
{
return base.Method();
}

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

Any ideas?

Thank you,

Carlos
private OCXClass Classobject
"Willy Denoyette [MVP]" wrote:
"baseClass" is declared as private, which makes it unreachacle from other
classes, are you sure you access baseClass from the declaring class?

....
private InteropClass.OC XClass baseClass ...

Willy.
"Carlos Lozano" <Ca**********@d iscussions.micr osoft.com> wrote in message
news:91******** *************** ***********@mic rosoft.com...
> 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.In terface
> {
> private InteropClass.OC XClass baseClass = new
> InteropClass.OC XClass();
>
> public myClass()
> {
> }
>
> #region All properties and methods created from the interface
> .
> .
> #endregion
> }
>
> When compiling I get an error saying "InteropClass.O CXClass" 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**********@d iscussions.micr osoft.com> wrote in
>> message
>> news:47******** *************** ***********@mic rosoft.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.Mic rImage // derives from
>> > interface
>> > {
>> > // create an instance of the base to use it in the
>> > implementation
>> > private MTMicrImage.Mic rImageClass obase = new
>> > MTMicrImage.Mic rImageClass();
>> >
>> > public oMicrImage()
>> > {
>> > }
>> >
>> > #region methods
>> > // All methods go here
>> > # endregion
>> >
>> > #region properties
>> > // All properties go here
>> > #endregion
>> > }
>> >
>> > Whn compiling I get an error saying "MTMicrImage.Mi crImageClass " 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.MTM icrImageClass,
>> > MTMicrImage.Mic rImage // derives from interface
>> >
>> >
>> > Attempt # 2
>> >
>> > using System;
>> > using MTMicrImage; // reference to the component
>> > using System.Reflecti ons;
>> >
>> > public class oMirImage : MTMicrImage.Mic rImage // derives from
>> > interface
>> > {
>> > private MTMicrImage.Mic rImageClass obase; // his works of not
>> > "new"
>> > is specified, but has null value.
>> >
>> > public oMicrImage()
>> > {
>> > }
>> >
>> > private object createInstance( )
>> > {
>> >
>> > object obj = null;
>> > try
>> > {
>> > obj =
>> > Activator.Creat eCOMObjectFrom" c:\\Interop.fil e.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.CreateO bject("MTMicrIm age.MicrImage")
>> >
>> > Thjis works, it creates the object just right.
>> >
>> > Thank you for your time.
>> >
>> > Carlos Lozano
>>
>>
>>


Nov 17 '05 #7
I have copied part of the class declaration for your review.
In fact the declaration of the constructor seems to be the problem as below.

.method assembly specialname rtspecialname
instance void .ctor() runtime managed internalcall
{
}

it is "internalca ll" which makes it inaccesible.

I tired changing it to:
//.method assembly specialname rtspecialname
.method public specialname rtspecialname
instance void .ctor() runtime managed internalcall
{
}

and:
//.method assembly specialname rtspecialname
.method public specialname rtspecialname
instance void .ctor() runtime managed
{
}

but give different kind of problems, so I went back to the original interop
file.

In the object browser the clas constructor shows to be public.

In addition, as mentioned before I can create an instance of the class using
javascript. The only difference is javascript creates it directly from the
OCX file, c# uses the interop assembly. I think the problem is in the interop
assembly.

I also tried creating it manually.
used:
1) tlbimp // imported the OCX file into the interop file.
2) ildasm // Created IL code
3) ilasm // recompiled as dll.

Thank you,

Carlos

here is the whole definition of the class. I removed the methods and
properties.

.class public auto ansi import MicrImageClass
extends [mscorlib]System.Object
implements MTMicrImage._Mi crImage,
MTMicrImage.Mic rImage,
MTMicrImage.__M icrImage_Event
{
.custom instance void
[mscorlib]System.Runtime. InteropServices .GuidAttribute: :.ctor(string) = ( 01
00 24 39 45 38 45 42 41 41 38 2D 35 37 33 43 // ..$9E8EBAA8-573C

2D 34 35 44 32 2D 41 36 34 43 2D 44 44 39 33 34 //
-45D2-A64C-DD934

38 39 37 34 34 44 45 00 00 ) //
89744DE..
.custom instance void
[mscorlib]System.Runtime. InteropServices .ComSourceInter facesAttribute: :.ctor(string)
= ( 01 00 19 4D 54 4D 69 63 72 49 6D 61 67 65 2E 5F // ...MTMicrImage. _

5F 4D 69 63 72 49 6D 61 67 65 00 00 00
00 ) // _MicrImage....
.custom instance void
[mscorlib]System.Runtime. InteropServices .TypeLibTypeAtt ribute::.ctor(i nt16) =
( 01 00 20 00 00 00 ) // .. ...
.custom instance void
[mscorlib]System.Runtime. InteropServices .ClassInterface Attribute::.cto r(int16) = ( 01 00 00 00 00 00 )
.method assembly specialname rtspecialname
instance void .ctor() runtime managed internalcall
{
} // end of method MicrImageClass: :.ctor

// methods go here

} // end of class MicrImageClass

"Willy Denoyette [MVP]" wrote:
Are you sure the compiler complains about this statement....
private Interop.OCXClas s myclass = new Interop.OCXClas s();

How is the class declared in the interop assembly when you look at it using
Ildasm.exe?

Willy.

"Carlos Lozano" <Ca**********@d iscussions.micr osoft.com> wrote in message
news:DD******** *************** ***********@mic rosoft.com...
Hi Willy,

Thank you Willy.

Yes, that is correct the private class object will serve to get all the
base
class methods and properties.
I do not need access to the base class from outside. I will use them in
the
implementation of the interface within the wrapper class.

In fact that is not the problem. The problem is to create an instance of
the
base class.
using :
private Interop.OCXClas s myclass = new Interop.OCXClas s();

gives the error stated before.

I could also derive from the class and the interface to call the OCXClass
as
the base implementation, but will still get the error message.

somthing like:
public Class myClass : InteropClass.OC XClass, InteropClass.In terface
{
public myClass()
{
}

public string Method()
{
return base.Method();
}

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

Any ideas?

Thank you,

Carlos
private OCXClass Classobject
"Willy Denoyette [MVP]" wrote:
"baseClass" is declared as private, which makes it unreachacle from other
classes, are you sure you access baseClass from the declaring class?

....
private InteropClass.OC XClass baseClass ...

Willy.
"Carlos Lozano" <Ca**********@d iscussions.micr osoft.com> wrote in message
news:91******** *************** ***********@mic rosoft.com...
> 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.In terface
> {
> private InteropClass.OC XClass baseClass = new
> InteropClass.OC XClass();
>
> public myClass()
> {
> }
>
> #region All properties and methods created from the interface
> .
> .
> #endregion
> }
>
> When compiling I get an error saying "InteropClass.O CXClass" 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**********@d iscussions.micr osoft.com> wrote in
>> message
>> news:47******** *************** ***********@mic rosoft.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.Mic rImage // derives from
>> > interface
>> > {
>> > // create an instance of the base to use it in the
>> > implementation
>> > private MTMicrImage.Mic rImageClass obase = new
>> > MTMicrImage.Mic rImageClass();
>> >
>> > public oMicrImage()
>> > {
>> > }
>> >
>> > #region methods
>> > // All methods go here
>> > # endregion
>> >
>> > #region properties
>> > // All properties go here
>> > #endregion
>> > }
>> >
>> > Whn compiling I get an error saying "MTMicrImage.Mi crImageClass " 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.MTM icrImageClass,
>> > MTMicrImage.Mic rImage // derives from interface
>> >
>> >
>> > Attempt # 2
>> >
>> > using System;
>> > using MTMicrImage; // reference to the component
>> > using System.Reflecti ons;
>> >
>> > public class oMirImage : MTMicrImage.Mic rImage // derives from
>> > interface
>> > {
>> > private MTMicrImage.Mic rImageClass obase; // his works of not
>> > "new"
>> > is specified, but has null value.
>> >
>> > public oMicrImage()
>> > {
>> > }
>> >
>> > private object createInstance( )
>> > {
>> >
>> > object obj = null;
>> > try
>> > {
>> > obj =
>> > Activator.Creat eCOMObjectFrom" c:\\Interop.fil e.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.CreateO bject("MTMicrIm age.MicrImage")
>> >
>> > Thjis works, it creates the object just right.
>> >
>> > Thank you for your time.
>> >
>> > Carlos Lozano
>>
>>
>>


Nov 17 '05 #8
Inline

Willy.

"Carlos Lozano" <Ca**********@d iscussions.micr osoft.com> wrote in message
news:A1******** *************** ***********@mic rosoft.com...
I have copied part of the class declaration for your review.
In fact the declaration of the constructor seems to be the problem as
below.

.method assembly specialname rtspecialname
instance void .ctor() runtime managed internalcall
{
}

it is "internalca ll" which makes it inaccesible.
No, its not! Don't mess with this, "internalca ll" means that the CLR is
called to contruct the RCW.
The problem is ".method assembly" ... , this means that the class is not
meant to be created from outside the Control code, if this class is authored
using VB6, it means that the class 'instancing' attribute is set to
PublicNotCreata ble (the default when using the Class wizard in a VB Control
project), which translates into assembly (or internal in C#).
I tired changing it to:
//.method assembly specialname rtspecialname
.method public specialname rtspecialname
instance void .ctor() runtime managed internalcall
{
}

and:
//.method assembly specialname rtspecialname
.method public specialname rtspecialname
instance void .ctor() runtime managed
{
}

but give different kind of problems, so I went back to the original
interop
file.

Not supprisingly.
In the object browser the clas constructor shows to be public.

In addition, as mentioned before I can create an instance of the class
using
javascript. The only difference is javascript creates it directly from the
OCX file, c# uses the interop assembly. I think the problem is in the
interop
assembly.
No the problem is that you are trying to create a non creatable object. Take
a look at your ocx file using oleview.exe , you will notice that the class
interface you are trying to create from C# is marked "noncreatab le". So I
guess you are mixing things up and you do create a different class using vbs
or Jscript.

I also tried creating it manually.
used:
1) tlbimp // imported the OCX file into the interop file.
2) ildasm // Created IL code
3) ilasm // recompiled as dll.

OCX are activeX controls, you should use aximp instead of tlbimp.
Thank you,

Carlos

Nov 17 '05 #9
Hi Willy.

I really appreciate you have taken the time to helping me with this. I just
found an alternate "hybrid" solution (part in VB6, part c#), but will be
looking into your last suggestion as I prefer a 100% c# solution.

Description:
I found the OCX was developed using VB6 (using the object viewer in VB6).
As it was not possible to create the instance in .NET (I tried C#.NET,
VB.NET), I decided to create it using VB6.

The hybrid solution is as follows:
1) Creating a a DLL module using a VB6 class to create the instance as an
object
2) Passing the object instance to the C# class and cast it with the class
derived from the interop interface.

It seems to be working okay.

Thank you,

Carlos Lozano

"Willy Denoyette [MVP]" wrote:
Inline

Willy.

"Carlos Lozano" <Ca**********@d iscussions.micr osoft.com> wrote in message
news:A1******** *************** ***********@mic rosoft.com...
I have copied part of the class declaration for your review.
In fact the declaration of the constructor seems to be the problem as
below.

.method assembly specialname rtspecialname
instance void .ctor() runtime managed internalcall
{
}

it is "internalca ll" which makes it inaccesible.

No, its not! Don't mess with this, "internalca ll" means that the CLR is
called to contruct the RCW.
The problem is ".method assembly" ... , this means that the class is not
meant to be created from outside the Control code, if this class is authored
using VB6, it means that the class 'instancing' attribute is set to
PublicNotCreata ble (the default when using the Class wizard in a VB Control
project), which translates into assembly (or internal in C#).
I tired changing it to:
//.method assembly specialname rtspecialname
.method public specialname rtspecialname
instance void .ctor() runtime managed internalcall
{
}

and:
//.method assembly specialname rtspecialname
.method public specialname rtspecialname
instance void .ctor() runtime managed
{
}

but give different kind of problems, so I went back to the original
interop
file.


Not supprisingly.
In the object browser the clas constructor shows to be public.

In addition, as mentioned before I can create an instance of the class
using
javascript. The only difference is javascript creates it directly from the
OCX file, c# uses the interop assembly. I think the problem is in the
interop
assembly.

No the problem is that you are trying to create a non creatable object. Take
a look at your ocx file using oleview.exe , you will notice that the class
interface you are trying to create from C# is marked "noncreatab le". So I
guess you are mixing things up and you do create a different class using vbs
or Jscript.

I also tried creating it manually.
used:
1) tlbimp // imported the OCX file into the interop file.
2) ildasm // Created IL code
3) ilasm // recompiled as dll.


OCX are activeX controls, you should use aximp instead of tlbimp.
Thank you,

Carlos


Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3987
by: mcmg | last post by:
Hi, I have an asp app that works fine on a windows xp machine but does not work on a windows 2000 server. I have the following code in my global.asa: <OBJECT RUNAT=Server SCOPE=SESSION ID=MyID
6
1757
by: Davinci_Jeremie | last post by:
Hi Newbee here to C# I have a simple questions... In a Hello world example how does the class object Hello exist with out creating it? I come from object pascal where everything object is created then used. In object pascal you can call a method a "class" and it means you can call it by the name of the class and the function name with out...
12
3141
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without restarting the application. What I did was to create an AppDomain that loaded the plugins and everything was great, until I tried to pass...
16
2343
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one or another (could also be 3) different web servers to use these Web Services. The Web Services are identical on all the machines. I tried just...
17
1925
by: Lee Harr | last post by:
I understand how to create a property like this: class RC(object): def _set_pwm(self, v): self._pwm01 = v % 256 def _get_pwm(self): return self._pwm01 pwm01 = property(_get_pwm, _set_pwm)
6
1777
by: wcc | last post by:
Hello, How do I create a class using a variable as the class name? For example, in the code below, I'd like replace the line class TestClass(object): with something like class eval(className) (object):
2
4927
by: Moses | last post by:
Hi All, Is is possible to catch the error of an undefined element while creating an object for it. Consider we are not having an element with id indicator but we are trying to make the object for it indicator = document.getElementById('indicator');
11
3719
by: Matthew Wells | last post by:
Hello. I have figured out how to create an instance of an object only knowing the type by string. string sName = "MyClassName"; Type t = Type.GetType(sName); Object objNew = Activator.CreateInstance(t); This works, but now I need to declare an array like
9
2966
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I read somewhere "using kernel stuff in thread is not good.." if ManualResetEvent object is created in thread but not actually used, will it affect performance? Bob
0
7568
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8085
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7928
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5469
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5185
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3615
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3598
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
892
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.