ActiveX Dll in Csharp

A

Avi

Hi,

Can someone please let me know if it is possible to create ActiveX dll
(COM component) in Csharp. If Yes, Can you please provide me some
pointer how to create it.

My objective is to create a function for impersonation that i will be
further using in my testing tool.

Thanks in advance,
Avinash.
 
P

Pavel Minaev

Hi,

Can someone please let me know if it is possible to create ActiveX dll
(COM component) in Csharp. If Yes, Can you please provide me some
pointer how to create it.

My objective is to create a function for impersonation that i will be
further using in my testing tool.

If you want to create a COM component in C#, then, yes, that is
possible. You might want to start with a sample here, and explore the
links in it:

http://msdn.microsoft.com/en-us/library/c3fd4a20.aspx

If you need an ActiveX _control_, then, to the best of my knowledge,
you can't make a properly working one using WinForms.
 
A

Avi

Finally I found the way out. Process seems to be little confusing, so,
listing down the steps or the benefit of others.

1) create a class file myCOMobj.cs (you can use notepad to do that)
2) Add the Sample Code below

//
********************************************************************************************************//
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace myCOMobj
{
public interface ImyCOMobj
{
string sayHello();
}
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("2F3357F5-2AB4-4d24-81C1-B54A915CAEF0")]
public class myCOM: ImyCOMobj
{
public myCOM()
{
//
// TODO: Add constructor logic here
//
}
public string sayHello()
{
return "Hello From C#";
}
}
}
//
********************************************************************************************************//

3) Create a strong Key Name using the below command
c:\Temp>sn.exe -k MyKey.snk
* you will see the message "Key pair written to MyKey.snk" and a
file will be created

4) Compile the original .cs file to make a dll using this strong key.
Use the command below achieve this.
C:\Temp>csc /keyfile:"c:\temp\myKey.snk" /target:library myCOM.cs
*this will create a dll file named myCOM.dll in c:\temp directory

5) Convert the .dll to type using command
C:\Temp>regasm myCom.dll /tlb myCom.tlb

6) Add the assembly into GAC cache using command
C:\Temp>gacutil -i myCOM.dll

Testing of Component:
Component should be visible to any application on that machine having
ability to use COM objects. For verification you can test this using
Excel.
1) Open Excel, Press ALT+F11 -> VBA editor will open
2) Tools > References > MyCOM should be listed in the Available
resources. Select that and Click ok.
3) Insert a module (Insert > Module)
4) Type the Code Below

Sub Test()
Dim objMyCOM As myCOM.myCOM
Set objMyCOM = New myCOM.myCOM
MsgBox objMyCOM.sayHello
End Sub

*Message Box "Hello From C#" should be displayed. If Everything goes
fine.

Regards,
Avinash
 
A

Avi

Finally I found the way out. Process seems to be little confusing, so,
listing down the steps or the benefit of others.

1) create a class file myCOMobj.cs (you can use notepad to do that)
2) Add the Sample Code below

//
********************************************************************************************************//
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace myCOMobj
{
public interface ImyCOMobj
{
string sayHello();
}
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("2F3357F5-2AB4-4d24-81C1-B54A915CAEF0")]
public class myCOM: ImyCOMobj
{
public myCOM()
{
//
// TODO: Add constructor logic here
//
}
public string sayHello()
{
return "Hello From C#";
}
}
}
//
********************************************************************************************************//

3) Create a strong Key Name using the below command
c:\Temp>sn.exe -k MyKey.snk
* you will see the message "Key pair written to MyKey.snk" and a
file will be created

4) Compile the original .cs file to make a dll using this strong key.
Use the command below achieve this.
C:\Temp>csc /keyfile:"c:\temp\myKey.snk" /target:library myCOM.cs
*this will create a dll file named myCOM.dll in c:\temp directory

5) Convert the .dll to type using command
C:\Temp>regasm myCom.dll /tlb myCom.tlb

6) Add the assembly into GAC cache using command
C:\Temp>gacutil -i myCOM.dll

Testing of Component:
Component should be visible to any application on that machine having
ability to use COM objects. For verification you can test this using
Excel.
1) Open Excel, Press ALT+F11 -> VBA editor will open
2) Tools > References > MyCOM should be listed in the Available
resources. Select that and Click ok.
3) Insert a module (Insert > Module)
4) Type the Code Below

Sub Test()
Dim objMyCOM As myCOM.myCOM
Set objMyCOM = New myCOM.myCOM
MsgBox objMyCOM.sayHello
End Sub

*Message Box "Hello From C#" should be displayed. If Everything goes
fine.

Regards,
Avinash
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top