Conari
🧬 One-touch unmanaged memory, runtime dynamic use of the unmanaged native C/C++ in .NET world, related P/Invoke features, and …
Install / Use
/learn @3F/ConariREADME
Conari
🧬 An unmanaged memory, modules, and raw data in one-touch.
Conari engine represents most flexible platform for working with unmanaged memory, modules, related P/Invoke features, and more around libraries, executable modules, runtime dynamic use of the unmanaged native C/C++ in .NET world and other raw data just in a few easy steps without configuring something, and... Even accessing to complex types like structures without their declaration at all.
Copyright (c) 2016-2024 Denis Kuzmin <x-3F@outlook.com> github/3F
Conari is waiting for your awesome contributions! https://github.com/3F/Conari/graphs/contributors
Why Conari
It was designed to be loyal to your needs on the fly!
🔍 Easy to start
using ConariL l = new("...");
🧰 Powerful types
No more manual type conversions and memory management complexities. Because nothing easier than just use it,
using dynamic l = new ConariX("regXwild.dll");
string data = "number = 888;";
bool found = l.replace<bool>(ref data, "+??;", "2034;");
// found: true; data: number = 2034;
🔨 Its amazing DLR features
using dynamic l = new ConariX("...");
l.<whatever_you_want>(curl, 10002, "https://");
Still not convinced? Here's full workable code example for regXwild:
using var c = ConariL.Make(new("regXwild"), out dynamic l);
// ready for everything even without configuring
using var u = NativeStruct.Make.f<UIntPtr>("start", "end").Struct;
/* Hey! We just generated a structure like
[StructLayout(LayoutKind.Sequential)]
private struct MatchResult
{
public UIntPtr start;
public UIntPtr end;
}*/
if(l.match<bool>("n = '888';", "'*'", 2/*MATCH_RESULT*/, u))
{
/* Now we just generated and invoked this
REGXWILD_API_L bool match
(
const rxwtypes::TCHAR* input,
const rxwtypes::TCHAR* pattern,
rxwtypes::flagcfg_t options,
EssRxW::MatchResult* result
)
*/
dynamic v = u.Access; // just access the EssRxW::MatchResult* result
// v.start == 4; v.end == 9;
}
// Yes, a 4 lines and your task is done; Free memory, Free hands.
[⏯] DllExport + Conari
[DllExport] // DllExportModifiedClassLibrary.dll
public static IntPtr callme(TCharPtr str, IntPtr structure)
{
if(str != "Hello world!") return IntPtr.Zero;
structure.Native().f<int>("x", "y").build(out dynamic v);
if(v.x > v.y)
{
structure.Access().write<int>(8);
}
return new NativeArray<int>(-1, v.x, 1, v.y);
}
... // host side via C/C++, Java, Rust, Python, ... or even same dotnet C#
using NativeString<TCharPtr> ns = new("Hello world!");
using NativeStruct<Arg> nstruct = new(new Arg() { x = 7, y = 5 });
using dynamic l = new ConariX("DllExportModifiedClassLibrary.dll");
IntPtr ptr = l.callme<IntPtr>(ns, nstruct);
using NativeArray<int> nr = new(4, ptr); // (nstruct.Data.x == 8) != (nr[1] == 7)
🚀 Awesome speed
Optional caching of 0x29 opcodes (Calli) and more.
test of regXwild's algorithms [340x10000 Unicode] | +icase [x32]| +icase [x64] | ----------------------------------------------|--------------|-----------------|--- regXwild **native C++**EXTalgorithm | **~50ms** | **~26ms** |<<regexp-c++11(regex_search) | ~59309ms | ~53334ms | regexp-c++11(regex_match with endings .*) | ~59503ms | ~53817ms | .NET Regex engine [Compiled] | ~38310ms | ~37242ms | .NET Regex engine | ~31565ms | ~30975ms | regXwild via **Conari** v1.3 (Lambda) -EXT| **~54ms** | **~35ms** |<<regXwild via **Conari** v1.3 (DLR) -EXT` | ~214ms | ~226ms |
🔧 The easiest (most ever) access to any data in unmanaged memory
// Everything will be generated at runtime
memory.Native()
.f<WORD>("Machine", "NumberOfSections") // IMAGE_FILE_HEADER (0xF4)
.align<DWORD>(3)
.t<WORD>("SizeOfOptionalHeader")
.t<WORD>("Characteristics")
.region()
.t<WORD>("Magic") // IMAGE_OPTIONAL_HEADER (0x108)
.build(out dynamic ifh);
if(ifh.SizeOfOptionalHeader == 0xF0) { // IMAGE_OPTIONAL_HEADER64
memory.move(0x6C);
}
// Use it !
ifh.NumberOfSections // 6
ifh.Characteristics // IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_LARGE_ADDRESS_AWARE | IMAGE_FILE_DLL
ifh.Machine // IMAGE_FILE_MACHINE_AMD64
ifh.Magic // PE64
dynamic l = ptr.Native().f<int>("x", "y").build();
l.x // 17
l.y // -23
🏄 Most powerful PInvoke and even most convenient use of WinAPI without preparing something
Conari will generate and adapt everything at runtime! Specially for you! For example, below we don't provide neither user32.ShowWindow() nor user32.MessageBoxA(), even no kernel32.GetModuleHandleA/W()
dynamic user32 = new User32();
user32.ShowWindow(0x000A0A28, 3);
user32.MessageBoxA(0, "Conari in action", "Hello!", 0);
dynamic kernel32 = new Kernel32();
kernel32.GetModuleHandleA<IntPtr>("libcurl-x64");
kernel32.GetModuleHandleW<IntPtr>((WCharPtr)ustr);
Because our recipe is simple, Just use it! and have fun.
🔖 Modern .NET Core
Conari is ready for .NET Core starting from 1.4. Even for .NET Standard 2.0 which does not cover unmanaged EmitCalli due to missing implementation for System.Private.CoreLib. Now this is another one of independent solution for everyone as https://github.com/3F/UnmanagedEmitCalli
🍰 Open and Free
Open Source project; MIT License; Fork! Star! Contribute! Share! Enjoy!
Conari is available for everyone from 2016 🎉
Take a look closer
Conari generally provides two mode,
Fully automatic way through its dynamic features (DLR). For example, when using the unmanaged code:
var ptr = d.test<IntPtr>(); //lambda ~ bind<Func<IntPtr>>("test")();
var codec = d.avcodec_find_encoder<IntPtr>(AV_CODEC_ID_MP2); //lambda ~ bind<Func<ulong, IntPtr>>("avcodec_find_encoder")(AV_CODEC_ID_MP2);
d.push(); //lambda ~ bind<Action>("push")();
d.create<int>(ref cid, out data); //lambda ~ bind<MyFunc<Guid, object>>("create")(ref cid, out data);
This (or like) does not require the any configuration from you, because Conari will do it automatically.
Semi-automatic way through its custom lambda expressions. For example, when using the unmanaged code:
using(var l = new ConariL("Library.dll"))
{
l.bind<Action<int, int>>("call")(2, 1);
double num = l.bind<Func<IntPtr, int, double>>("tonumber")(L, 4);
}
This also does not require neither the creation of any additional delegates nor the configuring something. Just a more control through l.bind<...>("...") methods that still make you happy;
// invoke it immediately
l.bind<Action<int, string>>("set")(-1, "Hello from Conari !");
// or later
set(-1, "Hello from Conari !");
Native C/C++ structures without declaration [?]:
// IMAGE_FILE_HEADER: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680313.aspx
dynamic ifh = binaryData.Native()
.t<WORD, WORD>(null, "NumberOfSections")
.align<DWORD>(3)
.t<WORD, WORD>("SizeOfOptionalHeader")
.build();
if(ifh.SizeOfOptionalHeader == 0xF0) { // IMAGE_OPTIONAL_HEADER64
...
}
// IMAGE_DATA_DIRECTORY: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680305.aspx
binaryData.Native()
.t<DWORD>("VirtualAddress")
.t<DWORD>("Size")
.build(out dynamic idd);
DWORD offset = rva2Offset(idd.VirtualAddress);
IntPtr ptr ...
Raw mt = ptr.Native()
.align<int>(2, "a", "b")
.t<IntPtr>("name")
.Raw;
- {byte[0x0000000c]} byte[]
[0] 0x05 byte --
[1] 0x00 byte |
[2] 0x00 byte |
[3] 0x00 byte --^ a = 5
[4] 0x07 byte --
[5] 0x00 byte |
[6] 0x00 byte |
[7] 0x00 byte --^ b = 7
[8] 0x20 byte --
[9] 0x78 byte |_ point


