static void Main(string[] args)
        {
            bool         isLoaded   = false;
            const string moduleName = "CppDynamicLinkLibrary";

            // Check whether or not the module is loaded.
            isLoaded = IsModuleLoaded(moduleName);
            Console.WriteLine("Module \"{0}\" is {1}loaded", moduleName, isLoaded ? "" : "not ");

            // Call the functions exported from the module.
            {
                string str = "HelloWorld";
                int    length;

                length = NativeMethods.GetStringLength1(str);
                Console.WriteLine("GetStringLength1(\"{0}\") => {1}", str, length);

                length = NativeMethods.GetStringLength2(str);
                Console.WriteLine("GetStringLength2(\"{0}\") => {1}", str, length);
            }

            // Call the callback functions exported from the module.
            {
                CompareCallback cmpFunc = new CompareCallback(CompareInts);
                int             max     = NativeMethods.Max(2, 3, cmpFunc);

                // Make sure the lifetime of the delegate instance covers the
                // lifetime of the unmanaged code; otherwise, the delegate
                // will not be available after it is garbage-collected, and
                // you may get the Access Violation or Illegal Instruction
                // error.
                GC.KeepAlive(cmpFunc);
                Console.WriteLine("Max(2, 3) => {0}", max);
            }

            // Use the class exported from the module.
            {
                CSimpleObjectWrapper obj = new CSimpleObjectWrapper();
                obj.FloatProperty = 1.2F;
                float fProp = obj.FloatProperty;
                Console.WriteLine("Class: CSimpleObject::FloatProperty = {0:F2}", fProp);
            }

            // You cannot unload the C++ DLL CppDynamicLinkLibrary by calling
            // GetModuleHandle and FreeLibrary.

            // Check whether or not the module is loaded.
            isLoaded = IsModuleLoaded(moduleName);
            Console.WriteLine("Module \"{0}\" is {1}loaded", moduleName, isLoaded ? "" : "not ");
        }
示例#2
0
        static void Main(string[] args)
        {
            bool         isLoaded   = false;
            const string moduleName = "CppDynamicLinkLibrary";

            // 检查模块是否被加载
            isLoaded = IsModuleLoaded(moduleName);
            Console.WriteLine("模块 \"{0}\" {1}被加载", moduleName, isLoaded ? "" : "没有");

            // 从模块中调用被导出的函数
            {
                string str = "HelloWorld";
                int    length;

                length = NativeMethods.GetStringLength1(str);
                Console.WriteLine("GetStringLength1(\"{0}\") => {1}", str, length);

                length = NativeMethods.GetStringLength2(str);
                Console.WriteLine("GetStringLength2(\"{0}\") => {1}", str, length);
            }

            // 从模块中调用被导出的回调函数
            {
                CompareCallback cmpFunc = new CompareCallback(CompareInts);
                int             max     = NativeMethods.Max(2, 3, cmpFunc);

                // 确保该委托的实例的生命周期涵盖整个非托管代码
                // 否则,该委托将无法使用,在垃圾回收后。
                // 你可能会收到访问冲突或非法指令错误
                GC.KeepAlive(cmpFunc);
                Console.WriteLine("Max(2, 3) => {0}", max);
            }

            // 从模块中使用被导出类.
            {
                CSimpleObjectWrapper obj = new CSimpleObjectWrapper();
                obj.FloatProperty = 1.2F;
                float fProp = obj.FloatProperty;
                Console.WriteLine("Class: CSimpleObject::FloatProperty = {0:F2}", fProp);
            }

            // 你不通过调用GetModuleHandle 和 FreeLibrary
            // 能卸载C++ DLL CppDynamicLinkLibrary

            // 检查模块是否被加载。
            isLoaded = IsModuleLoaded(moduleName);
            Console.WriteLine("模块 \"{0}\" {1}被加载", moduleName, isLoaded ? "" : "没有");
        }
示例#3
0
        static void Main(string[] args)
        {
            bool isLoaded = false;
            const string moduleName = "CppDynamicLinkLibrary";

            // 检查模块是否被加载
            isLoaded = IsModuleLoaded(moduleName);
            Console.WriteLine("模块 \"{0}\" {1}被加载", moduleName, isLoaded ? "" : "没有");

            // 从模块中调用被导出的函数
            {
                string str = "HelloWorld";
                int length;

                length = NativeMethods.GetStringLength1(str);
                Console.WriteLine("GetStringLength1(\"{0}\") => {1}", str, length);

                length = NativeMethods.GetStringLength2(str);
                Console.WriteLine("GetStringLength2(\"{0}\") => {1}", str, length);
            }

            // 从模块中调用被导出的回调函数
            {
                CompareCallback cmpFunc = new CompareCallback(CompareInts);
                int max = NativeMethods.Max(2, 3, cmpFunc);

                // 确保该委托的实例的生命周期涵盖整个非托管代码
                // 否则,该委托将无法使用,在垃圾回收后。
                // 你可能会收到访问冲突或非法指令错误
                GC.KeepAlive(cmpFunc);
                Console.WriteLine("Max(2, 3) => {0}", max);
            }

            // 从模块中使用被导出类.
            {
                CSimpleObjectWrapper obj = new CSimpleObjectWrapper();
                obj.FloatProperty = 1.2F;
                float fProp = obj.FloatProperty;
                Console.WriteLine("Class: CSimpleObject::FloatProperty = {0:F2}", fProp);
            }

            // 你不通过调用GetModuleHandle 和 FreeLibrary
            // 能卸载C++ DLL CppDynamicLinkLibrary

            // 检查模块是否被加载。
            isLoaded = IsModuleLoaded(moduleName);
            Console.WriteLine("模块 \"{0}\" {1}被加载", moduleName, isLoaded ? "" : "没有");
        }