在C#中显示加载C++的Dll

上一篇 / 下一篇  2008-11-19 16:58:11 / 个人分类:Windows编程相关

今天好不容易安装了VC++,就写了个Dll,并在C#中调用,效果还好,能加载成功:

    class NativeMethod

    {

        public delegate int Operation(int m,int n);

 

        [DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

        public static extern IntPtr LoadLibraryExA(string filePath,IntPtr hFile,int flag);

 

        [DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

        public static extern Operation GetProcAddress(IntPtr hModule,string procName);

 

        [DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

        public static extern bool FreeLibrary(IntPtr hModule);

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            IntPtr handle = NativeMethod.LoadLibraryExA(@"E:\Program Files\Microsoft Visual  Studio\MyProjects\DllLib\Debug\DllLib",IntPtr.Zero,0);

            NativeMethod.Operation Add = NativeMethod.GetProcAddress(handle,"Add");

            int result = Add(2,3);

            NativeMethod.FreeLibrary(handle);

        }

    }

  那个C++的Dll中就一个int Add(int nLeft,int nRight)函数。


TAG: Windows编程相关

 

评分:0

我来说两句

Open Toolbar