熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#實現動態調用Windows DLL

2022-06-13   來源: .NET編程 

  部分代碼來自於網絡廢話不多說上代碼

  調用方法

  object obj = WinDllInvoke(Kerneldll Beep new object[] { } typeof(void));
函數代碼

   [SystemRuntimeInteropServicesDllImport(kernel)]
          private static extern IntPtr LoadLibrary(string lpLibFileName);
 
          [SystemRuntimeInteropServicesDllImport(kernel)]
          private static extern IntPtr GetProcAddress(IntPtr hModule string lpProcName);
 
          [SystemRuntimeInteropServicesDllImport(kernel)]
          private static extern IntPtr FreeLibrary(IntPtr hLibModule);
 
         ///
         /// 動態調用Windows DLL
         ///
         /// Dll文件名
         /// 待調用的函數名
         /// 函數參數
         /// 返回值
         /// 調用結果
         private static object WinDllInvoke(string fileName string funName object[] objParams Type returnType)
         {
             IntPtr libHandle = IntPtrZero;

             try
             {
                 //獲取函數地址
                 libHandle = LoadLibrary(fileName);
                 if (libHandle == IntPtrZero) return null;
                 IntPtr procAddres = GetProcAddress(libHandle funName);
                 if (procAddres == IntPtrZero) return null;
                
                 //獲取參數類型
                 Type[] paramTypes = new Type[objParamsLength];
                 for (int i = ; i < objParamsLength; ++i)
                 {
                     paramTypes[i] = objParams[i]GetType();
                 }

                 //構建調用方法模型
                 AssemblyName asembyName = new AssemblyName();
                 asembyNameName = WinDllInvoke_Assembly;
                 AssemblyBuilder asembyBuilder = AppDomainCurrentDomainDefineDynamicAssembly(asembyName AssemblyBuilderAccessRun);
                 ModuleBuilder moduleBuilder = asembyBuilderDefineDynamicModule(WinDllInvoke);
                 MethodBuilder methodBuilder = moduleBuilderDefineGlobalMethod(InvokeFun MethodAttributesPublic | MethodAttributesStatic returnType paramTypes);

                 //獲取一個 ILGenerator 用於發送所需的 IL
                 ILGenerator IL = methodBuilderGetILGenerator();
                 for (int j = ; j < paramTypesLength; ++j)
                 {
                     //將參數壓入堆棧
                     if (paramTypes[j]IsValueType)
                     {
                         ILEmit(OpCodesLdarg j); //By Value
                     }
                     else
                     {
                         ILEmit(OpCodesLdarga j); //By Addrsss
                     }
                 }

                 // 判斷處理器類型
                 if (IntPtrSize == )
                 {
                     ILEmit(OpCodesLdc_I procAddresToInt());
                 }
                 else if (IntPtrSize == )
                 {
                     ILEmit(OpCodesLdc_I procAddresToInt());
                 }
                 else
                 {
                     throw new PlatformNotSupportedException(不好意思

  偶不認得你哦!);
                 }

                 ILEmitCalli(OpCodesCalli CallingConventionStdCall returnType paramTypes);
                 ILEmit(OpCodesRet); // 返回值
                 moduleBuilderCreateGlobalFunctions();

                 // 取得方法信息
                 MethodInfo methodInfo = moduleBuilderGetMethod(InvokeFun);

                 return methodInfoInvoke(null objParams);// 調用方法並返回其值
             }
             catch { return null; }
             finally
             {
                 if (libHandle != IntPtrZero) FreeLibrary(libHandle); //釋放資源
             }
         }


From:http://tw.wingwit.com/Article/program/net/201311/12149.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.