Solved

C# ActiveX DLL

Posted on 2004-09-08
11
1,109 Views
Last Modified: 2008-01-09
Could someone please help me with some example source code on how to write an ActiveX DLL in C#. I have an existing DLL written in VB 6.0 and I have a decent understanding of COM and COM+.

This DLL will only be called from classic ASP pages and it will be run on two dedicated COM+ servers.

All I need is a working sample with one or two methods.

The first method should make a connection to a MS SQL Server, run a simple select statement and write the results to the calling ASP page by using Response.Write.

In the VB DLL I use the ASPTypeLibrary Class to get access to the ASP Request, Response, Session, Server and Application objects. Is it possible to do the same in C#?
0
Comment
Question by:hans_larson
  • 5
  • 4
11 Comments
 
LVL 2

Expert Comment

by:netjkus
ID: 12009503
I am not sure if you wanted to write an Active X component? I think you are trying to use C# code in asp.

But, basically in .NET - MS recommends to re-write the ActiveX code using C# components. Just handling a Connection to SQLServer need not be an activeX at all. You can create a standard Library to do this job.

to create a DLL , try this :
http://www.c-sharpcorner.com/2/pr12.asp

you can get most of the part done using the regular DLL. Unless you want to use some components that are done in Active X and you want to keep using them in C#, like Imaging etc., you donot need ActiveX in .net. You can write Unmanaged code if you are planning to use in non - .net applications. This will not help you use any of the .net features.
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12009788
netjkus:

My current environment is Classic ASP and the reason for wanting to move to C# is because the VB 6.0 ActiveX DLL is single-threaded and I need a multi-threaded DLL and figured I can do this is C#.

I know a little more about C# than I do about C++ so that's the motivation to use C# right there.

Look at:  http://www.codeproject.com/csharp/estransactions.asp

Esentialy this is what I need to do, but I also need to be able to access the ASP intrinsic objects from within C#. So an example of using the component described in the above URL that writes the recordset using the ASP Response object is what I need.

0
 
LVL 4

Expert Comment

by:thedude112286
ID: 12020530
Just compile this as a dll.

[ClassInterface(ClassInterfaceType.AutoDual)] // the key to exposing a C# app as a ActiveX object
public class YourClass{
      // all your methods and stuff go here
}

Good info can be found at http://www.devhood.com/messages/message_view-2.aspx?thread_id=16569.
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12023333
thanks, now how to I use the ASP intrinsic objects in C#?

Example: How do I use Response.Write instead of using return?
0
 
LVL 4

Expert Comment

by:thedude112286
ID: 12029944
Why not try this:

[ClassInterface(ClassInterfaceType.AutoDual)] // the key to exposing a C# app as a ActiveX object
public class YourClass{
      public string GetMsg() { return "Hello World"; }
}

And in asp:
Response.Write(YourClassObject.GetMsg())

Or is this not what you are talking about?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:hans_larson
ID: 12030953
That will work to just write the output for a single line of text. I could also get away with concatenating a really long string, but that will just kill performance.

I need to be able to access the ASP objects to use the Request and Session objects also.
0
 
LVL 4

Expert Comment

by:thedude112286
ID: 12060187
[ClassInterface(ClassInterfaceType.AutoDual)] // the key to exposing a C# app as a ActiveX object
public class YourClass{
      HttpResponse Response;
      HttpRequest Request;
      HttpSessionState Session;

      public YourClass(HttpResponse response, HttpRequest request, HttpSessionState session) {
            Response = response;
            Request = request;
            Session = session;
      }

      public void GetMsg() {
            Response.Write("Hello World");
      }
}

See if this works
0
 
LVL 4

Accepted Solution

by:
thedude112286 earned 125 total points
ID: 12060190
You would call the class in my last post like this:

YourClass c = new YourClass(Response, Request, Session);
c.GetMsg();
0
 
LVL 3

Author Comment

by:hans_larson
ID: 12060196
Thanks. I will try this and let you know if it works. Why do I need to call the class?

Can't I just call it from ASP using Server.CreateObject ?
0
 
LVL 4

Expert Comment

by:thedude112286
ID: 12060961
That should work.  Treat it just as you would a normal COM object.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now