Dynamic Loading of hosted DLL

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;

Namespace webservice1
{
/// <Summary>
/// Summary of dllinvoke
/// </Summary>
Public class dllinvoke
{
[Dllimport ("kernel32.dll")]
Private extern static intptr loadlibrary (string path );

[Dllimport ("kernel32.dll")]
Private extern static intptr getprocaddress (intptr Lib, string funcname );

[Dllimport ("kernel32.dll")]
Private extern static bool freelibrary (intptr Lib );

Private intptr hlib;
Public dllinvoke (string dllpath)
{
Hlib = loadlibrary (dllpath );
}

~ Dllinvoke ()
{
Freelibrary (hlib );
}

// Convert the function to be executed to the Delegate
Public Delegate invoke (string apiname, type T)
{
Intptr API = getprocaddress (hlib, apiname );
Return (delegate) Marshal. getdelegateforfunctionpointer (API, t );
}

}
}

The main function of the above class is to dynamically find the hosted DLL function to be loaded through the API, and return the proxy (pointer) of the function. It seems that C ++ can also load it like this, next time, experiment in C ++

BelowCodeYes:

Public class test
{
Public Delegate int setdbconfig (string IPaddress, string dbname, string username, string psw); // declare the method through delegation

Dllinvoke DLL;
Setdbconfig;

Public Test
{
DLL = new dllinvoke (server. mappath (@"~ /Bin/dllagain. dll "); // instantiate the DLL
Setdbconfig = (setdbconfig) DLL. Invoke ("setdbconfig", typeof (setdbconfig ));
}

Public static void main ()
{
Setdbconfig (string IPaddress, string dbname, string username, string psw); // call this operation
}
}

 

From: http://zhouweigang01.blog.163.com/blog/static/934090720095492458949/

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.