Skip to content
Denis Kuzmin [ github.com/3F ] edited this page Jul 16, 2018 · 4 revisions

Just include Conari.dll for your project, and look the following namespace:

net.r_eg.Conari;

Now you can work flexibly with any unmanaged code (Libraries, Executable Modules, other native and binary data).

Basic work

.dll / .exe

using(var l = new ConariL("regXwild.dll")) // Immediately load unmanaged regXwild.dll
{
    // ... now we're ready for miracles

    // `bool searchEssC(const TCHAR* data, const TCHAR* filter, bool ignoreCase);`
    bool result = l.DLR.searchEssC<bool>((WCharPtr)data, (WCharPtr)filter, false);

    // ...
}

Full integration

class MyConari: ConariL
{
    public MyConari(string lib, string prefix = null)
        : base((Config)lib, prefix)
    { }
    // ... powerful engine is ready for your awesome applications
}

To get all available Exported-Functions & Variables

foreach(var proc in l.PE.ExportedProcNames) {
    // ...
}
// or:
string[] list = l.PE.ExportedProcNamesArray;

Calling Convention, Name-Decoration, Aliases

using(var l = new ConariL("Library.dll", CallingConvention.StdCall))
{
    //...
    l.Mangling = true; // _get_SevenStdCall@0 <-> get_SevenStdCall
    l.Convention = CallingConvention.Cdecl; // the default convention for Conari as and for most C and C++ programs
    l.Aliases["getFlag"] = l.Aliases["getD_True"]; // getFlag() -> getD_True() -> ...
}

DLR vs Lambda & Exported-Functions

Architecture of Conari engine provides a few ways for work with unmanaged libraries.

  • Direct binding via lambda-functions - semi-automatic way.
  • Direct binding via DLR - fully automatic way.

It does not require the creation of any additional delegate. The Conari engine will do it automatically instead of you.

Just use bind<> methods and have fun !

l.bind<...>("function")

you already may invoke it immediately:

l.bind<Action<LuaState, string>>("setglobal")(L, "onKeyDown");

or later:

var set = l.bind<Action<LuaState, string>>("setglobal");
...
set(L, "onKeyDown");

DLR features are similar to lambda-functions above, except that an binding fully generated at runtime !

dlr.any_function_of_Lib(L, 123, "arg3", ...)

for return value:

var data = dlr.any_function_of_Lib<return_type>(L, 123, "arg3", ...)

For example:

// all this will be generated at runtime:
var ptr     = d.test<IntPtr>();
var codec   = d.avcodec_find_encoder<IntPtr>(AV_CODEC_ID_MP2);
              d.push();
              d.create<int>(ref cid, out data);

Exported-Variables

l.ExVar.DLR.ADDR_SPEC // 0x00001CE8
l.ExVar.get<UInt32>("ADDR_SPEC"); // 0x00001CE8
l.ExVar.getField(typeof(UInt32).NativeSize(), "ADDR_SPEC"); // Native.Core.Field via raw size
l.Svc.native("lpProcName"); // Raw access via NativeData & Native.Core !

Lazy loading

using(var l = new ConariL(
                    new Config("Library.dll") {
                        LazyLoading = true
                    }))
{
    ...
}

Native & raw data

Even for raw data you can feel free and be a hero:

using net.r_eg.Conari.Native;
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   |_ pointer to allocated string: (CharPtr)name
        [10]   0xf0    byte   |
        [11]   0x56    byte --
...
dynamic idd = NativeData
                ._(data)
                    .t<DWORD>("VirtualAddress") // idd.VirtualAddress
                    .t<DWORD>("Size")           // idd.Size
                    .Raw.Type;

if(idd.VirtualAddress < 1) {
    // ...
}

Other

Conari engine already contains many useful types & helpers for work with unmanaged data like UnmanagedStructure, UnmanagedString, CharPtr, WCharPtr, ... The additional events, PE32/PE32 features, powerful binding between C/C++ & C# and fully flexible raw access at all.

  • The details about it, you can find here
  • The list of projects that already uses Conari, here

If you don't know "what and how": see also available unit-tests for Conari engine, or create issue for any question.

πŸ—Ž

πŸ“‚

  • πŸ“’ Q/A
Clone this wiki locally