简体   繁体   English

在.net Project中嵌入Win32 dll

[英]Embed a win32 dll in .net Project

Is it possible to embed a win32 .dll into a .net project? 是否可以将Win32 .dll嵌入到.net项目中? Ideally I don't want to distribute a .dll along with a .exe. 理想情况下,我不想与.exe一起分发.dll。 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). 我已经读过关于将AssemblyReslove用于.net dll的信息( 在此处 ),但它似乎不适用于我的.dll的(使用Intel Fortran编译器创建的)。 Firstly the event "AppDomain.CurrentDomain.AssemblyResolve" never gets called. 首先,永远不会调用事件“ AppDomain.CurrentDomain.AssemblyResolve”。 However even if I force an assembly load before using the .dll I get the following error: 但是,即使我在使用.dll之前强行加载程序集,也会出现以下错误:

An unhandled exception of type 'System.BadImageFormatException' occurred in mscorlib.dll. mscorlib.dll中发生了'System.BadImageFormatException'类型的未处理异常。
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. 附加信息:无法加载文件或程序集'从DLLtest2,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一加载的'16896字节'。 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! 谢谢!

Assembly.Load is for .NET (managed) assemblies only. Assembly.Load仅适用于.NET(托管)程序集。 For native DLLs you need to use PInvoke. 对于本机DLL,您需要使用PInvoke。

Given the following (Intel) Fortran subroutine: 给定以下(英特尔)Fortran子例程:

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. 在C#代码中使用以下方法签名。

[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. 在此的示例FortranStuff.dll必须与调用程序集在同一目录中。 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. 您可以修改属性以使用相对或绝对文件路径(“ .. \\ bin \\ FortranStuff.dll”),但是我建议您将其并排放置以减少混乱。

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 如果您将其嵌入程序中,我认为将其反编译并保存dll可能很简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM