Dynamic dll library loading in C #

Source: Internet
Author: User
C # uses the export function in the dynamic link library. It can only be displayed and called in the program according to the dllatri.pdf, and cannot be dynamically loaded in the running stage, however, we can use the loadlibrary and freelibrary of Win32 API to achieve the goal. The problem is that the address of the export function obtained based on the getprocaddress () function cannot be used in C, many methods on the Internet use function call stack operations to operate in C. in fact, if the export function does not return a value, you can use another API function to dynamically bind the DLL Library:
Lresult callwindowproc (wndproc lpprevwndfunc,
Hwnd,
Uint MSG,
Wparam,
Lparam
);
If the exported function does not return any values, you can directly call callwindowproc (functionname,). If you need to pass a simple parameter to the exported function, you can also customize a message, encapsulate real parameters with messages. [Dllimport ("Kernel32")]
Public extern static int loadlibrary (string lplibfilename); [dllimport ("Kernel32")]
Public extern static bool freelibrary (INT hlibmodule); [dllimport ("Kernel32", charset = charset. ANSI)]
Public extern static int getprocaddress (INT hmodule, string lpprocname); [dllimport ("USER32", entrypoint = "callwindowproc")]
Public static extern int callwindowproc (INT lpprevwndfunc, int hwnd, int MSG, int wparam, int lparam); Private void button#click (Object sender, system. eventargs E)
{
Int hmod = loadlibrary ("*****. dll ");
Int pfname = getprocaddress (hmod ,"*****");
Callwindowproc (pfname, 0, 0 );
Freelibrary (hmod );
}

 

Related Article

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.