. NET Framework uses Unmanaged DLL Functions

Source: Internet
Author: User

Today, on the MSDN of tw, I suddenly saw c # Call the dll compiled by c. The method is good. simple. active X is still written in the past, which is troublesome. for more information, see MSDN (v2005 ).

using System;using System.Runtime.InteropServices;class MainClass {    [DllImport("User32.dll")]    public static extern int MessageBox(int h, string m, string c, int type);    static int Main()     {        string myString;         Console.Write("Enter your message: ");        myString = Console.ReadLine();        return MessageBox(0, myString, "My Message Box", 0);    }}
Description

This example creates an external DLL that will be called from the C # program in Example 3.

Code
  Copy code
// cmdll.c// compile with: /LD /MDint __declspec(dllexport) SampleMethod(int i){    return i*10;}

 

This example uses two filesCM.csAndCmdll.cTo describe extern. The C file is the external DLL created in example 2, which is called from the C # program.

Code

  Copy code
// cm.csusing System;using System.Runtime.InteropServices;public class MainClass {    [DllImport("Cmdll.dll")]    public static extern int SampleMethod(int x);    static void Main()     {        Console.WriteLine("SampleMethod() returns {0}.",                 SampleMethod(5));    }}

Refer:

Extern (C # reference)

Ms-help: // MS. MSDNQTR. v80.chs/MS. MSDN. v80/MS. VisualStudio. v80.chs/dv_csref/html/9c3f02c4-51b8-4d80-9cb2-f2b6e1ae15c7.htm

DllImportAttribute class

Ms-help: // MS. MSDNQTR. v80.chs/MS. MSDN. v80/MS. NETDEVFX. v1_chs/cpref11/html/T_System_Runtime_InteropServices_DllImportAttribute.htm

Use Unmanaged DLL Functions

Ms-help: // MS. MSDNQTR. v80.chs/MS. MSDN. v80/MS. VisualStudio. v80.chs/dv_fxinterop/html/eca7606e-ebfb-4f47-b8d9-289903fdc045.htm

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.