How to gradually implement dynamic library loading and type Matching

Source: Internet
Author: User

For more information about how to load dynamic databases, match types, and export functions of dynamic link libraries, see the macro definition below:
# Define libexport_api extern "C" _ declspec (dllexport)

The first step is to start with a simple call and define a simple function, which only implements an integer addition sum:

Libexport_api int mysum (int A, int B) {return a + B ;}
C # import definition:

Public class refcomm
{
[Dllimport ("libencrypt. dll ",
Entrypoint = "mysum ",
Charset = charset. Auto, callingconvention = callingconvention. stdcall)]
Public static extern int mysum (int A, int B );
}
Call the test in C:

Int isum = refcomm. mysum (,);

Run the command and check that isum is 5. The call is correct. The first step is the completion of the test. It indicates that the custom dynamic link library function can be called in C.

Step 2: I have defined the string operation function (for simplicity, the previous function name is used) and the returned result is a string:

Libexport_api char * mysum (char * a, char * B) {sprintf (B, "% s", a); return ;}
C # import definition:

Public class refcomm
{
[Dllimport ("libencrypt. dll ",
Entrypoint = "mysum ",
Charset = charset. Auto,
Callingconvention = callingconvention. stdcall)]
Public static extern string mysum (string a, string B );
}
Call the test in C:

String strdest = "";
String strtmp = refcomm. mysum ("45", strdest );

The strtmp value is 45, but the strdest value is empty. When I modify the dynamic link library implementation, the returned result is string B:

Libexport_api char * mysum (char * a, char * B) {sprintf (B, "% s", a) return B ;}
Modify C # import definition and change string B to ref mode:

Public class refcomm
{
[Dllimport ("libencrypt. dll ",
Entrypoint = "mysum ",
Charset = charset. Auto, callingconvention = callingconvention. stdcall)]
Public static extern string mysum (string a, ref string B );
}
Call the test again in C:

String strdest = "";
String strtmp = refcomm. mysum ("45", ref strdest );
The strtmp and strdest operations are incorrect. They contain invisible characters. Modify the C # import definition and change charset from auto to ANSI:

Public class refcomm
{
[Dllimport ("libencrypt. dll ",
Entrypoint = "mysum ",
Charset = charset. ANSI, callingconvention = callingconvention. stdcall)]
Public static extern string mysum (string a, string B);} in C #, call the test again: String strdest = ""; string strtmp = refcomm. mysum ("45", ref strdest); The strtmp value is "45", but the strdest value is not assigned. The second step implements the function return string, but the output fails in the function exit parameter. Modify C # import definition again, and change string B to reference (REF): Public class refcomm {[dllimport ("libencrypt. DLL ", entrypoint =" mysum ", charset = charset. ANSI, callingconvention = callingconvention. stdcall)] public static extern string mysum (string a, ref string B);} The call failed during running and the execution cannot continue. Step 3: Modify the dynamic link library implementation and Change B to a double pointer: libexport_api char * mysum (char * a, char ** B) {sprintf (* B ), "% s", a); return * B;} C # import definition: public class refcomm {[dllimport ("libencrypt. DLL ", entrypoint =" mysum ", charset = charset. ANSI, callingconvention = callingconvention. stdcall)] public static extern string mysum (string a, ref string B);} call test in C #: String strdest = ""; string strtmp = refcomm. mysum ("45", ref strdest); run the command to view the result. Strtmp and strdest are both "45" and the call is correct. The third step outputs the correct output result of function exit parameters. Step 4: Modify the dynamic link library implementation to implement integer parameter output: libexport_api int mysum (int A, int B, int * c) {* c = A + B; return * C;} C # import definition: public class refcomm {[dllimport ("libencrypt. DLL ", entrypoint =" mysum ", charset = charset. ANSI, callingconvention = callingconvention. stdcall)] public static extern int mysum (int A, int B, ref int C);} call test in C #: int C = 0; int isum = refcomm. mysum (, ref C); run the command to check that both isum and C are 5. The call is correct. After the experiments in the above steps, I have basically mastered how to define the dynamic library function and how to define the import in C #. With this foundation, I soon implemented the call of the variable-length encryption function in C, so far. Iii. Conclusion C # Calls the dynamic link library function written in C ++. to export the parameter output, use a pointer. For strings, use a double pointer, for the C # import definition, you need to use the reference (REF) definition. For function return values, the C # import definition must be consistent with the c ++ dynamic library function Declaration definition. Otherwise, function call may fail. When defining the import, pay attention to the charset and callingconvention parameters. Otherwise, the call fails or the result is abnormal. During running, the dynamic link library is placed in the directory of the C # program. Here I am a dynamic link library of C #, and the two dynamic link libraries are run in the same directory.

How to gradually implement dynamic library loading and type Matching

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.