小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

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

 kittywei 2012-01-31

看看動(dòng)態(tài)庫(kù)創(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

運(yùn)行:

E:\> main
123 + 456 = 579

動(dòng)態(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);
    }
}
  • 首先,加載我們動(dòng)態(tài)庫(kù)dll.dll,使用的Assembly.Load,也可以用(相對(duì)或絕對(duì)路徑)

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

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

另一種寫(xiě)法:

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

編譯運(yùn)行:

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

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

  • EntryPoint

  • CharSet

  • CallingConvention

  • SetLastError

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

動(dòng)態(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個(gè)函數(shù)弄進(jìn)來(lái),然后用它們?nèi)ヌ幚砦覀円獎(jiǎng)討B(tài)加載的動(dòng)態(tài)庫(kù)。

直接編譯

E:\> csc main.cs

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多