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

上一篇 / 下一篇  2009-02-24 14:15:14 / 个人分类:DLL相关

    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);

        }

    }


TAG:

 

评分:0

我来说两句

Open Toolbar