Solved

C# ActiveX DLL

Posted on 2004-09-08
11
1,108 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

759 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

19 Experts available now in Live!

Get 1:1 Help Now