How To Call DLL in ASP

Source: Internet
Author: User

How To Call DLL in ASP

. Net DLL is not a dynamic connection library in a strict sense, but a class or class library. It cannot be used directly in ASP, VB, and other applications.
Environment. We can use the com package (COM callable wrapper (CCW) to repackage. Net DLL and use this intermediate medium to implement the corresponding functions.

The following is a small example for your reference.
1. Create a new class project in. Net Visual Studio.
2. Add a new class in the project,CodeAs follows:

Using system;

Namespace classlibrary1
{

// Define the interface, which is very important (the methods in this interface are the same as those in the COM component)
Public interface iclass1
{

String test ();

}

Public class class1: iclass1
{

Private string T = "";
// Method in the implementation Interface
Public String test ()
{

Return "OK ";
}
}
}

3. In the. NET command line window, create a key pair.
You can use the tool sn.exe that comes with. Net to create a key pair.
For example, Sn-K testdll. Keys
4. Create a strong nameProgramSet (strong name Assembly)
In the assemblyinfo. CS file, add the [Assembly: assemblykeyfile (@ "C:" test. Keys ")] attribute

5. Compile the entire project and generate classlibrary1.dll (This dll can be found in the "bin" Debug path .)
6. In the. NET command line window, generate a Type Library
Tlbexp classlibrary1.dll/out: classlibrary1.tlb

7. register the installation accessory in the. NET command line window
Regasm/TLB: classlibrary1.tlb classlibrary1.dll

8. In the. NET command line window, install. Net accessories in the Global Assembly Cache (GAC ).
Gacutil/I classlibrary1.dll

9. Test in ASP
<%
Set S = Createobject ("classlibrary1.class1 ")
Response. Write (S. Test ())
%>
-----------------------------------------------

The dynamic connection library (DLL) can accelerate the execution of key parts of the application. At the same time, the use of DLL through a unified COM interface can also maximize the reusability of components. You don't have to worry about the language used by the DLL. You only need to pay attention to the functions and interface parameters provided by the DLL for use.
At present, there are more and more DLL components written based on. NET technology. If traditional ASP can also use the functions provided by these components, it can avoid unnecessary effort by many programmers. Recently, in my work, I need to call the DLL generated in the. NET environment in ASP. I have done some research and shared my experience. In fact, they are all very simple things ~

1. Call traditional ActiveX DLL in ASP
To enable com to "see" Your components, you must first register them in the registry:
Regsvr32 mydll. dll
Then create an object in ASP:
Set mydll = server. Createobject ("mydll. Method ")
Mydll. Method
In this way, you can use the attributes and methods in the component.

2. Call. Net-Based DLL in ASP
For component registration, one method is automatic registration, in. the Automatic Registration of COM components can be implemented in the IDE environment of net2003 or 2005. The settings are as follows: project name-> right-click "properties"-> Configure properties-> Generate-> select "register for com InterOP" and compile the project.
Another method is to manually register:

Run the command line to convert the current directory to: C: \ WINDOWS \ Microsoft. NET \ framework \ v2.0.50727.
Enter the command: regasmMydll. dll /TLB:Mydll. TLB/Codebase
Note the usage of codebase. If the. Net assembly you want to register does not have a strong name, add/codebase when using regasm for registration.
After registration, create an object and use it in the same way as above:
Set mydll = server. Createobject ("mydll. Method ")
Mydll. Method

In fact, not only ASP, but other COM programs (including VBA, extended stored procedures, and so on) Call DLL methods are the same.

For details about regasm parameters, see:
Http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/cptools/html/cpgrfassemblyregistrationtoolregasmexe. asp

 

There are two methods
Encrypt (string ptoencrypt, string skey) // Encrypt
Decrypt (string ptodecrypt, string skey) // decrypt

Then you write the code like this:
Using system;
Using system. runtime. interopservices;
Namespace
{
// First create an interface, which is required by com
[GUID ("61bb24cb-4c2c-40f8-9e13-1ac5e558d56a")]
Public interface iencrypt
{
String encrypt (string ptoencrypt, string skey );
String decrypt (string ptodecrypt, string skey );
}

// Write interface implementation
[GUID ("CB52E990-185E-4448-A7E8-C88ECAD563AB")]
Public class name: iencrypt
{
Public String encrypt (string ptoencrypt, string skey)
{
// Copy the FAQ encryption code
}

Public String decrypt (string ptodecrypt, string skey)
{
// Copy the FAQ decryption code
}
}
}

Then, use vs.net to package it into a. dll class library file. Assume the file is named myencrypt. dll.
Use the following Tool
Regasm myencrypt. dll/TLB: myencrypt. TLB
This. TLB file is a type library and can be referenced by VB6 and VC ++ 6.

Note that the guid above is generated using the guid creation tool in the vs.net tool menu.
Note that assemblyinfo is automatically generated by vs.net. A strong name and version number must be added to the CS file. Because the COM component requires a version number, do not change the version number or use vs.net's automatic version number. * It is best to use such a version number.
A fixed version such as 1.1.1.1 must be added at a time and cannot be added multiple times.

Procedure
1. Net Assembly generation:
 
Using system;
Using system. IO;
Using system. text;
Using system. runtime. interopservices;
Using system. Security. cryptography;
Namespace CSIC
{
// First create an interface, which is required by com
[GUID ("61bb24cb-4c2c-40f8-9e13-1ac5e558d56a")]
Public interface iencrypt
{
String encrypt (string ptoencrypt, string skey );
String decrypt (string ptodecrypt, string skey );
}

// Write interface implementation
[GUID ("CB52E990-185E-4448-A7E8-C88ECAD563AB")]
Public class crypt: iencrypt
{
Public String encrypt (string ptoencrypt, string skey)
{
// Copy the FAQ encryption code
}

Public String decrypt (string ptodecrypt, string skey)
{
// Copy the FAQ decryption code
}
}
}

Save the above file as a class library project of the vs.net project.
Then use vs.net's "vsitual Studio. NET tool" --> vistual Studio. NET Command Prompt
In the command line
Cd c: "<press enter>
Sn-K mykey. SNK <press enter>
Generate a strong name file named mykey. SNK in the root directory of the C drive, and close the prompt window.
In the assemblyinfo. CS file automatically generated by the class library project in vs.net
[Assembly: assemblykeyfile ("")]
Change
[Assembly: assemblykeyfile (@ "C:" mykey. SNK ")]
Set [Assembly: assemblyversion ("1. 0. *")]
Change
[Assembly: assemblyversion ("1.0.0.0")] // Note: Your COM component version is 1.0.0.0.

Then press SHIFT + Ctrl + B to generate the dll library (in the release mode), for example, CSIC. crypt. dll.
At this time, the Assembly is successfully created.

2. register the Assembly and create a Type Library
Still use the vistual Studio. NET command prompt in the Start Menu
Enter your project directory. Assume It is D: "myproject" bin "release.
In the dialog box, enter
D: <press enter>
CD myproject "bin" Release <press enter>
Enter the Dir command to view the CSIC. crypt. dll file.
Enter regasm/TLB: CSIC. crypt. tlb csic. crypt. dll. <press enter>
Then the CSIC. crypt. TLB library file is generated under this directory. Do not close this prompt window.
At this time, the. NET assembly of this. dll will become a standard COM component, but it cannot be used yet. It must be changed to a global COM component.

The regasm utility will create a Type Library and register it in the Windows registry so that classes in physserver2.dll can be used on the COM Client.

3. Add the assembly to the Global Assembly Cache

Enter the prompt window and enter
Gacutil/I CSIC. crypt. dll <press enter>
Then, your DLL is copied to the Global Assembly Cache. That is to say, the DLL component can be used no matter which hard disk of the computer.

Iv. Usage
ASP usage
Set OBJ = server. Createobject ("CSIC. crypt ")
Dim str1
Str1 = obj. Encrypt ("content to be encrypted", "password") // Encrypt
Dim str2
Str2 = obj. decrypt ("content to be decrypted", "password") // decrypt

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.