Вызов native кода из C#/VB.Net без COM
??? ??? ?????????? ???????? ?????????, ??? ????? COM ????? ???????????? ??? ? ?????? ?????? ???????. ? ??? ??????, ????????????? ?????. ?????? ? ??????? ? ??????? ????????. ?????? ??????? ????? ??? ???????? ?? ???????. ??????, ? ????? ???????? ??-??????. ????????? ??? ????????? ?? ???????.
??????, ???? ?????? ???????? ???????? ?++ ??????? ? DLL, ????????? ??????? ??????? ?? System.Runtime.InteropServices. ??? ??? ?????, ?? ??????? ?????????? ????????? ??? ???, ????? ????? ???????? ? COM.
??-??????, ??? ????? ?????????? ???. ???????? ??????? C++ DLL (? VS ??? ???? ????????? ??????), ? ????? ??????????? ???????, ??????? ????? ????????. ????????,
LONG __declspec(dllexport) Qu(LONG a, LONG b)
{
return a+b;
}
???? ?? ????? – ??????? ??? ?????. ?? ???????? ????? ???????? ?? ? DEF ????? ??? ???????:
LIBRARY "SampleLib"
EXPORTS
Qu PRIVATE
?????? ? ????? C# ???????, ???????? ???? ????? ??? ???????:
[DllImport("SampleLib.dll")]
public static extern Int32 Qu(Int32 a, Int32 b);
?, ?????, ?? ?????? ??? ????????, ????????, ???:
class Program
{
[DllImport("SampleLib.dll")]
public static extern Int32 Qu(Int32 a, Int32 b);
static void Main(string[] args)
{
Int32 a = 5, b = 7;
Console.WriteLine("Adding {0} and {1}\n",a,b);
Int32 c = Qu(a, b);
Console.WriteLine("Result {0}\n",c);
Console.ReadKey();
}
}
????? ?? ??? ??????, ??????? ?? ????? ????? ??? COM ?? ????? ??? ????????????, ????????????, class factories… ??????
? ??, ? ???. ?????? ? ????, ??? COM – ??? ???? ????? ?? ??????. ?????? ?? ?????? ??????? ????? ???????????? ATL. ???????? ? Visual Studio ATL DLL ??????, ? ????? ????? ??????????????? ??? ?? ????? ?????. ?????? ???? ? ?? ??. ????? ??????????? interop assembly ? ??? ??????, ????? COM ?????? ????? ???????????? ??? ??????? ????? ?? C#. ??, ????????? ???? ?? ATL ?????? ???, ?? ? 95 ??????? ?? 100 ?? ????????? ??????? ??? ???? ?????? ? ???????.
? ?????? ???????? ? ??????? ???? (??????, ????? ??????? ???????? P/Invoke). ??????, ????????? ???????, ??? ???????? ??????? ????????? ?????????? ??????????????, ????? ????? ?????? ?????????? ??????. ????, ??? ???? ????? ??????????????? ???? ????????, ??? ????:
?????? ????? ? ???????? ????????? ???????? DllImport. ??? ? ??????? ??????? ?????? ??? DLL ? ???, ? ?? ????? ???? ?? ??? ???? ??????, ? ??? ??? ?????? ?? ?????????? ??????. ??? ??? ??? ????? ???????? ????? ?????????? ?????????? ? ?????????.
????? ????? ?????? ????? ? ???????? ??????????? ???????? ????? ?????? ? ???? ?????? .Net ? C#. ??????, ? ??????? ????, ?++ ??? ?????????? ??? LONG. ???????????? ??????? – ??? ???????????? long ? C# ?????????, ?? ??? ????? ??-??-??, ????????? C# long – ??? 64 ????, ? ?? 32. ?????? LONG – long, ??? ??? ??????????? ??????, ? ??????????? ?? ??????? ????? ??????? ?????? ????? ????????.
???????, ??? ???? ??? ? ?????????, ??? Sample.dll ???? ???????? ?????? ???????????? ?????, ?????????? ?? C# (??, ??? ??? Visual Basic.Net). ???? ?? ??????????? ??????? ??????? ? ????? ????????? ? Visual Studio, ?? ?? ?????? ???? ???????????? ?? ??, ??? Sample.dll ?? ?????? ??? ??????????. ??? ??, ? ??? ? ??????. ??????, ??? ????? ????? ?? ???? ???????????? ?? ?????? ??? ???????, ?? ? ??? ???????? ????????????? ?????? ????????.
? ??????, ??????? ?? ???? ????????, ????? ?????? ? ?????????? ??????.
??-??????, ??, ?? – ????????????, ?? ??????????. ? ??? ?????? ????, ??? ????? ???????????? ???? ??? ????? ???? ????, ??? ????? ???? ????? ?? ????? ?????????? ??? ??? ??? ????? ????, ????????, ????????? ??? ?? ?????????? ???? ? ??? ????????, ????? _??_ ?????????? ???????? ??? ???? ??????? ? ??? ????
? ??-??????, ???? ?? – ????????????, ??? ????????, ??? ??? ?????? ??????? ?????? ?? ????? ??????????? ??????. ?, ??? ??-??????, ????? ?????? ?????? ? ??? ?????? ??????? – COM/ATL+Interop assembly ??? P/Invoke ??? ???????? ? ???? ??????? ???? ??????, ??? ? ??????? ????? ??????????? ??????? ??????? ??????? ? ????? ?? ????? ???????????? ???????????.
??? ?? ????????, ??? ?????????????? ?????? ???????? ?? ??????? ????????????. ???? ????? ???????, ????? ??? ?????. ????????, ???? ??? ????? ??????? ?????-?????? ??????? Win32 API, ???????????? ?? ? COM-?????? – ???? ?? ????? ???????. ??????, ???? ??????????, ??? ?? ?? ??? ??????? – ? (!) ??? ???? ????? ?? ??? ??????.
? ?? ???? ? ??? ? ?? ??????? ????, ? ???-?????? ????? IT-Pro ???????? (??????, ?????????, ?????????, ? ?.?.), ????? ????? ?????? ???-?? ?????? ?? ??????? ??????, ?? ??? COM/ATL+Interop assembly ?????? ???? ??? ????? ? ????????.