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); }}
DescriptionThis 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.cs
AndCmdll.c
To 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