Friday, October 9, 2009

Export native 64bit method from .NET assembly

It is well known fact that .NET assembly could be tweaked to export native method, similar way how normal DLLs do it. There is good description and tool from Selvin for 32bit native function.
My problem was how to do it for 64bits. Here you go.

1) you IlDAsm your assembly into il code.
2) Edit the IL and change header to look like this
for 32bit
.corflags 0x00000002
.vtfixup [1] int32 fromunmanaged at VT_01
.data VT_01 = int32[1]
for 64bit
.corflags 0x00000008 
.vtfixup [1] int64 fromunmanaged at VT_01
.data VT_01 = int64[1]
3) Header of your exported method would look similar to this. This is same for 32bit version.
.vtentry 1 : 1
.export [1] as Java_net_sf_jni4net_Bridge_initDotNet
4) You IlAsm the file back into DLL. For x64 you use /x64 flag.

1 comment:

Pete said...

Thanks for the _int64 part, however setting corflags to 0x00000008 is inaccurate. You can (and should) set it to 0x00000000.

0x00000008 flags the assembly as strong-signed.

I've tested with 0x00000000 and the 64-bit DLL can be loaded and called from unmanaged code just fine.