Question

Is it possible to embed a win32 .dll into a .net project? Ideally I don't want to distribute a .dll along with a .exe. I've read about using AssemblyReslove for .net dll's (here) but it doesn't seem to be working for my .dll's (created with the Intel Fortran compiler). Firstly the event "AppDomain.CurrentDomain.AssemblyResolve" never gets called. However even if I force an assembly load before using the .dll I get the following error:

An unhandled exception of type 'System.BadImageFormatException' occurred in mscorlib.dll.
Additional information: Could not load file or assembly '16896 bytes loaded from DLLtest2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Any ideas what I might be doing wrong - or another approach I could try?

Code used for Assembly Load:

using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DLLtest2.Dll1.dll"))
{
   byte[] assemblyData = new byte[stream.Length];
   stream.Read(assemblyData, 0, assemblyData.Length);
   Assembly.Load(assemblyData);
}

Thanks!

Was it helpful?

Solution

Assembly.Load is for .NET (managed) assemblies only. For native DLLs you need to use PInvoke.

Given the following (Intel) Fortran subroutine:

subroutine DoWork(n, A)
    !DEC$ ATTRIBUTES DLLEXPORT :: DoWork
    !DEC$ ATTRIBUTES ALIAS: 'DoWork' :: DoWork
    !DEC$ ATTRIBUTES REFERENCE :: n, A
    integer, intent(in) :: n
    real(8), dimension(n, n), intent(inout) :: A
end subroutine

Use the following method signature in the C# code.

[DllImport("FortranStuff.dll", EntryPoint = "DoWork", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void DoWork(ref int n, [In, Out] double[,] A);

In this example, FortranStuff.dll needs to be in the same directory as the calling assembly. You could modify the attribute to use a relative or absolute file path ("..\bin\FortranStuff.dll"), but I would recommend just placing it side by side to reduce confusion.

OTHER TIPS

I don't think so! they must be linked in compiling time! and if you embed in you program i think it can be simple to decompile it and save the dll

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top