乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C Sharp與.net學(xué)習(xí)筆記(二)

       kittywei 2012-01-31

      看看動態(tài)庫創(chuàng)建與使用相關(guān)的東西

      生成與使用(托管的)dll

      • dll.cs ==> dll.dll

      // file: dll.cs
      public class Calc
      {
          public static int Add(int a, int b)
          {
              return a + b;
          }
      }
      • main.cs ==> main.exe

      // file: main.cs
      using System;
      
      class App
      {
          public static void Main()
          {
              Console.WriteLine("{0} + {1} = {2}", 123, 456, Calc.Add(123, 456));
          }
      }

      編譯:

      E:\> csc /target:library dll.cs
      E:\> csc /reference:dll.dll  main.cs

      運行:

      E:\> main
      123 + 456 = 579

      動態(tài)加載dll

      using System;
      using System.Reflection;
      
      class App
      {
          public static void Main()
          {
              Assembly dll = Assembly.Load("dll");
              Type calcType = dll.GetType("Calc");
              object[] parameters = new object[]{123, 456};
              object res = calcType.InvokeMember("Add2",
                      BindingFlags.Default | BindingFlags.InvokeMethod,
                      null,
                      null,
                      parameters);
              Console.WriteLine("{0} + {1} = {2}", 123, 456, (int)res);
          }
      }
      • 首先,加載我們動態(tài)庫dll.dll,使用的Assembly.Load,也可以用(相對或絕對路徑)

      Assembly.LoadFrom("dll.dll");
      • 然后,獲取需要的類型,存于calcType
      • 最后通過InvokeMember調(diào)用該類型中的成員函數(shù)。如果調(diào)用的不是static成員,還需要創(chuàng)建該類型的實例

              Object obj = Activator.CreateInstance(calcType);
              object res = calcType.InvokeMember("Add",
                      BindingFlags.Default | BindingFlags.InvokeMethod,
                      null,
                      obj,
                      parameters);

      另一種寫法:

              MethodInfo addMethod = calcType.GetMethod("Add");
              object[] parameters = new object[]{123, 456};
              object obj = Activator.CreateInstance(calcType);
              object res = addMethod.Invoke(obj, parameters);

      使用非托管 dll

      • dll2.cpp ==> dll2.dll

      // file: dll2.cpp
      extern "C" int __declspec(dllexport) Add(int a, int b)
      {
          return a + b;
      }
      • main.cs ==> main.exe

      // file: main.cs
      using System;
      using System.Runtime.InteropServices;
      
      class App
      {
          [DllImport("dll2.dll")]
          public static extern int Add(int a, int b);
      
          public static void Main()
          {
              Console.WriteLine("{0} + {1} = {2}", 123, 456, Add(123, 456));
          }
      }

      編譯運行:

      E:\> cl dll2.cpp /LD
      E:\> csc main.cs
      E:\> main
      123 + 456 = 579

      這個東西被稱為Platform Invoke(P/Invoke). DllImport還有一些屬性

      • EntryPoint

      • CharSet

      • CallingConvention

      • SetLastError

      [DllImport("dll2.dll", EntryPoint="Add", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Winapi, SetLastError=true)]

      動態(tài)加載dll

      這又需要回到Win32 Api函數(shù)LoadLibrary、FreeLibrary這些東西了

      // file: main.cs
      using System;
      using System.Runtime.InteropServices;
      
      class App
      {
          [DllImport("kernel32.dll")]
          public static extern IntPtr LoadLibrary(string dllFileName);
      
          [DllImport("kernel32.dll")]
          public static extern IntPtr GetProcAddress(IntPtr dllHandle, string functionName);
      
          [DllImport("kernel32.dll")]
          public static extern bool FreeLibrary(IntPtr dllHandle);
      
          private delegate int AddDelegate(int a, int b);
      
          public static void Main()
          {
              IntPtr handle = LoadLibrary("dll2.dll");
              IntPtr pAddFunc = GetProcAddress(handle, "Add");
              AddDelegate Add = (AddDelegate)Marshal.GetDelegateForFunctionPointer(
                      pAddFunc, typeof(AddDelegate));
      
              Console.WriteLine("{0} + {1} = {2}", 123, 456, Add(123, 456));
              FreeLibrary(handle);
          }
      }

      先用P/Invoke把這3個函數(shù)弄進(jìn)來,然后用它們?nèi)ヌ幚砦覀円獎討B(tài)加載的動態(tài)庫。

      直接編譯

      E:\> csc main.cs

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多