C # Call an API function whose parameter is the function pointer-compile a global exception handler using setunhandledexceptionfilter as an example.

Source: Internet
Author: User

If you want to call an API function in C #, you must declare the following namespace. Otherwise, you cannot call the API function:
Using system. runtime. interopservices;

Next, declare the import and export functions. Here, we use an API function to write the INI file as an example:

[Dllimport ("Kernel32")]
Private Static extern long writeprivateprofilestring (string section,
String key, string Val, string filepath );

To call setunhandledexceptionfilter, the problem is not only as simple as calling an API, but also how to pass a C # function as a function pointer to an API function, the standard method of C # is to use a proxy as a function pointer. For example, a proxy can declare it as follows:

Public Delegate bool enumthreadwindowscallback (intptr hwnd, intptr lparam );

I wrote a C # applet through my experiments, and roughly implemented a global exception interception program. The program still has some problems and is not perfect, the rtlmovememory function is used in the program to create a global exception. The last parameter 200 is used to input an invalid parameter, which can cause exceptions on my computer, if an exception cannot be thrown on your computer, adjust this parameter.
The Code is as follows:
Form1.cs
---------------------------------------------------------------------
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. runtime. interopservices;

Namespace setunhandledexceptionfilter
{
Public partial class form1: Form
{


Public Delegate int32 callback (ref long );

Callback mycall;

[Dllimport ("Kernel32")]
Private Static extern void rtlmovememory (ref byte DST,
Ref byte SRC, int32 Len );

[Dllimport ("Kernel32")]
Private Static extern int32 setunhandledexceptionfilter (callback CB );

Public static int32 newexceptionfilter (ref long)
{
MessageBox. Show ("A global exception is intercepted! ");
Return 0;
}

Public form1 ()
{
Initializecomponent ();

Mycall = new callback (newexceptionfilter );
Setunhandledexceptionfilter (mycall );

}

Private void button#click (Object sender, eventargs E)
{
Byte A = 1, B = 2;
MessageBox. Show ("A =" + A. tostring ());
Rtlmovememory (Ref A, ref B, 200 );
MessageBox. Show ("A =" + A. tostring ());

}
}
}
---------------------------------------------------------------------

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.