|
![]() |
|
---|---|
// cm.cs using 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)); } } |
SampleMethod() returns 50. |
生成項目:
使用 Visual C++ 命令行將 Cmdll.c
編譯為 DLL:
cl /LD /MD Cmdll.c
使用命令行編譯 CM.cs
:
csc CM.cs
這將創(chuàng)建可執(zhí)行文件 CM.exe
。運行此程序時,SampleMethod
將值 5 傳遞到 DLL 文件,該文件將此值乘以 10 返回。
|