When you study C # In actual work, you may ask: why do we need to provide some existing functions (for example, some functions in windows, some methods that have been compiled in C ++) re-compile
Write code. C # Is there any way to directly use these existing functions? The answer is yes. You can directly call these functions through dllimport in C.
The namespace using system. runtime. interopservices where dllimport is located;
The explanation of dllimportattribute in msdn is as follows: This attribute can be applied to methods. The dllimportattribute attribute provides the information necessary to call a function exported from an unmanaged DLL. As the minimum requirement, the name of the DLL containing the entry point must be provided.
The dllimport attribute is defined as follows:
Namespace system. runtime. interopservices
{
[Attributeusage (attributetargets. Method)]
Public class dllimportattribute: system. Attribute
{
Public dllimportattribute (string dllname ){...}
Public callingconvention;
Public charset;
Public String entrypoint;
Public bool exactspelling;
Public bool preservesig;
Public bool setlasterror;
Public String Value {get {...}}
}
}
Note:
1. dllimport can only be placed on method declaration.
2. dllimport has a single positioning parameter: Specify the dllname parameter that contains the DLL name of the imported method.
3. dllimport has five naming parameters:
A. The callingconvention parameter indicates the call convention of the entry point. If no callingconvention is specified, use the default value callingconvention. winapi.
B. The charset parameter indicates the character set used in the entry point. If charset is not specified, the default value charset. Auto is used.
C. The entrypoint parameter specifies the name of the DLL entry point. If entrypoint is not specified, the method name is used.
D. The exactspelling parameter indicates whether the entrypoint must exactly match the spelling of the indicated entry point. If exactspelling is not specified, use the default value false.
E. The preservesig parameter indicates whether the method signature should be retained or converted. When the signature is converted, it is converted to
Return Value and the signature of an additional output parameter named retval returned value. If preservesig is not specified, the default value true is used.
The F and setlasterror parameters indicate whether the method retains Win32 "previous error ". If setlasterror is not specified, the default value false is used.
4. It is a one-time attribute class.
5. In addition, the method modified with the dllimport attribute must have an extern modifier.
Usage of dllimport:
Dllimport ("mydllimport. dll")]
Private Static extern int mysum (int A, int B );
The IC Card Reader introduces the SDK to import the DLL in the following way, and the error "-84" is returned.
[Dllimport ("mwrf32.dll", callingconvention = callingconvention. stdcall, charset = charset. Auto, setlasterror = true)]
Public static extern short rf_hl_writehex (INT icdev, short mode, short ADR, ref int SNR, string sdata );
Last changed
[Dllimport ("mwrf32.dll")]
public static extern short rf_HL_writehex(int icdev, short mode, short Adr, ref int Snr, string sdata);
Problem Solving