. Before the emergence of NET, many mature libraries were stored in various applications in the form of C/C ++ or COM. These libraries or programs have been running for a long time, and very stable and mature .. NET provides a variety of methods to call unmanaged code in the managed code, but the calling performance will suffer a lot of losses. to optimize the performance of platform calling as much as possible, Here are several notes.
Minimize calls to unmanaged code. Therefore, avoid calling unmanaged code in the loop body as much as possible. Instead, try to write the entire loop body to unmanaged code, that is, a single platform call is used to complete as many tasks as possible;
The dll path specified by the DllImportAttribute can be placed in the program root directory as much as possible, because. NET searches for dll files under the root directory, searches under the windows System directory, and finally searches under all directories under the PATH environment variable. If not found, DllNotFoundException will be thrown;
If the name of the called function is specified, it is identified by ExactSpelling = True, because if this function is not found when MessageBox function is called ,. net will try to search for MessageBoxA or MessageBoxW, which is determined by CharSet in the DllImportAttribute attribute, if CharSet is set to CharSet. ansi searches for MessageBoxA, if it is CharSet. if Unicode is used, search for MessageBoxW;