Solved

C# ActiveX DLL

Posted on 2004-09-08
11
1,111 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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 …

838 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