DllExport

Unmanaged Exports ( .NET DllExport )

1
2
Copyright (c) 2009-2015  Robert Giesecke
Copyright (c) 2016-2017  Denis Kuzmin <entry.reg@gmail.com>

Build status NuGet package License

1
2
3
4
5
6
7
8
9
10
[DllExport("Init", CallingConvention.Cdecl)]
public static int entrypoint(IntPtr L)
{
    // ... it will be called from Lua script

    lua_pushcclosure(L, onProc, 0);
    lua_setglobal(L, "onKeyDown");

    return 0;
}
1
2
3
4
5
[DllExport("Init", CallingConvention.Cdecl)]
// __cdecl is the default calling convention for our library as and for C and C++ programs
[DllExport(CallingConvention.StdCall)]
[DllExport("MyFunc")]
[DllExport]

Support of Modules: Library (.dll) and Executable (.exe) [?]

Where to look ? v1.2+ provides dynamic definitions of namespaces (ddNS feature), thus you can use what you want - details here

1
2
3
4
5
6
7
8
    Via Cecil or direct modification:

    Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

    000005B0                 00 C4 7B 01 00 00 00 2F 00 12 05       .Ä{..../...
    000005C0  00 00 02 00 00 00 00 00 00 00 00 00 00 00 26 00  ..............&.
    000005D0  20 02 00 00 00 00 00 00 00 49 2E 77 61 6E 74 2E   ........I.want.   <<<-
    000005E0  74 6F 2E 66 6C 79 00 00 00 00 00 00 00 00 00 00  to.fly..........  <<<-


Initially the original tool

1
UnmanagedExports
was distributed by Robert Giesecke as an closed-source tool under the MIT License:

Now, we will be more open ! all details here

License

It still under the MIT License (MIT) - be a ~free~ and open

&

How it works

Current features has been implemented through ILDasm & ILAsm that does the all required steps via

1
.export
directive.

What inside ? or how works the .export directive ?

Read about format PE32/PE32+, start with grammar from asmparse and move to writer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
if(PASM->m_pCurMethod->m_dwExportOrdinal == 0xFFFFFFFF)
{
  PASM->m_pCurMethod->m_dwExportOrdinal = $3;
  PASM->m_pCurMethod->m_szExportAlias = $6;
  if(PASM->m_pCurMethod->m_wVTEntry == 0) PASM->m_pCurMethod->m_wVTEntry = 1;
  if(PASM->m_pCurMethod->m_wVTSlot  == 0) PASM->m_pCurMethod->m_wVTSlot = $3 + 0x8000;
}
...
EATEntry*   pEATE = new EATEntry;
pEATE->dwOrdinal = pMD->m_dwExportOrdinal;
pEATE->szAlias = pMD->m_szExportAlias ? pMD->m_szExportAlias : pMD->m_szName;
pEATE->dwStubRVA = EmitExportStub(pGlobalLabel->m_GlobalOffset+dwDelta);
m_EATList.PUSH(pEATE);
...
// logic of definition of records into EXPORT_DIRECTORY (see details from PE format)
HRESULT Assembler::CreateExportDirectory()  
{
...
    IMAGE_EXPORT_DIRECTORY  exportDirIDD;
    DWORD                   exportDirDataSize;
    BYTE                   *exportDirData;
    EATEntry               *pEATE;
    unsigned                i, L, ordBase = 0xFFFFFFFF, Ldllname;
    ...
    ~ now we're ready to miracles ~

or read my short explanations from here: DllMain & the export-table; DllExport.dll; .exp & .lib; ordinals

How to get DllExport

Available variants:

How to Build

No requires additional steps for you, just build as you need.

Use build.bat if you need final NuGet package as a

1
DllExport.<version>.nupkg
etc.

How to Debug

For example, find the DllExport.MSBuild project in solution:

1
"<path_to_SolutionFile_for_debugging>.sln" /t:Build /p:Configuration=<Configuration>

use additional

1
Diagnostic
key to msbuild if you need details from .targets

1
"<path_to_SolutionFile_for_debugging>.sln" /verbosity:Diagnostic /t:Rebuild /p:Configuration=<Configuration>

Go to

1
Start Debugging
. Now you can debug at runtime.

coreclr - ILAsm / ILDasm

We use our custom versions of coreclr, special for DllExport project - https://github.com/3F/coreclr

This helps to avoid some problems (like this) and more…

To build minimal version (means that it does not include all components as for original coreclr repo):

1
git submodule update --init --recursive

Make sure that you have installed CMake, then build simply:

1
2
build_s all x86 x64 Release
build_s x86 Release

or use

1
2
build_coreclr_x86.cmd
build_coreclr_x86_x64.cmd

You can also use our binaries of coreclr separately if needed:


Support ?

just a note again… I mentioned earlier that DllExport is not priority for me (current impl.) “- I will do something from current tasks, but guys, please support it with me” and… why so many support from me o_o