2010-01-11 67 views
4

我想创建一个可以从非托管代码(Delphi 5)访问的.NET程序集。非托管导出:无法编译程序集

我发现Unmanaged Exports和遵循的步骤有,但我甚至无法successfuly编译基本的例子:

using RGiesecke.DllExport; 

namespace DelphiNET 
{ 
    public class Class1 
    { 
     [DllExport("add")] 
     public static int Add(int left, int right) 
     { 
      return left + right; 
     } 
    } 
} 

DelphiNET.csproj项目文件:

... 
<ItemGroup> 
    <Compile Include="Class1.cs" /> 
    <Compile Include="DllExport\DllExportAttribute.cs" /> 
    <Compile Include="Properties\AssemblyInfo.cs" /> 
</ItemGroup> 
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 
<Import Project="DllExport\RGiesecke.DllExport.targets" /> 
... 

以下是错误:

------ Build started: Project: DelphiNET, Configuration: Release Any CPU ------ 
c:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:TRACE /debug:pdbonly /filealign:512 /optimize+ /out:obj\Release\DelphiNET.dll /target:library Class1.cs DllExport\DllExportAttribute.cs Properties\AssemblyInfo.cs 

Compile complete -- 0 errors, 0 warnings 
DelphiNET -> C:\DelphiNET\bin\Release\DelphiNET.dll 
ILDasm: calling 'C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ildasm.exe' with /quoteallnames /nobar "/out:C:\Documents and Settings\Lukas\Local Settings\Temp\tmp29F\DelphiNET.il" "C:\DelphiNET\bin\Release\DelphiNET.dll" 
C:\DelphiNET\bin\Release\DelphiNET.dll : warning EXP0009: Platform is AnyCpu, generating creating binaries for each CPU platform in a separate folder... 
ILAsm: calling 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ILAsm.exe' with /nologo "/out:C:\DelphiNET\bin\Release\x86\DelphiNET.dll" "C:\Documents and Settings\Lukas\Local Settings\Temp\tmp29F\DelphiNET.x86.il" /DLL "/resource=C:\Documents and Settings\Lukas\Local Settings\Temp\tmp29F\DelphiNET.res" /optimize 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembling 'C:\Documents and Settings\Lukas\Local Settings\Temp\tmp29F\DelphiNET.x86.il' to DLL --> 'C:\DelphiNET\bin\Release\x86\DelphiNET.dll' 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : Source file is ANSI 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::.ctor 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::.ctor 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::.ctor 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::get_CallingConvention 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::set_CallingConvention 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::get_ExportName 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::set_ExportName 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : ***** FAILURE ***** 
C:\DelphiNET\DllExport\RGiesecke.DllExport.targets(8,5): error : 
Done building project "DelphiNET.csproj" -- FAILED. 
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ========== 

操作系统:WinXPSP3,Microsoft Visual C# 2008 Express Edition with SP1,.NET 3.5 SP1

任何想法有什么不对?谢谢。


23:40编辑:

我发现的bug。它是在功能的名称 - 太相同。当你改变其中的一个时,它就可以工作。

+1

您是否尝试过只使用x86或x64代替AnyCPU? – 2010-01-11 16:43:45

+0

这应该不成问题。在这种情况下,它将保持原始组件不变,但为x86和x64版本创建子文件夹。 – 2010-01-11 17:00:26

+0

在我看来,它像对待ilasm.exe的诊断输出一样,就好像它们是错误的一样。 – 2010-01-11 17:33:16

回答

8

顺便提一下,我刚更新了档案。 你甚至可以把它全部摆放在你身上,而当你拿着this来代替。

这是一个项目模板,设置一切,应该工作得很好。

我在前面的版本中找到了一些观点,我做了一些并非总是如此的假设。 我之前实现的一个潜在问题是用于发布配置的/ optimize开关。在这种情况下,有时ILAsm会扼杀IL,我还没有看到新版本。

+0

+1好东西!你有没有使用Brian Long的文章(http://www.blong.com/Articles/DotNetInteropD8/Interop1/Win32AndDotNetInterop.htm和http://www.blong.com/Conferences/BorConUK2002/Interop1/Win32AndDotNetInterop.htm)? – 2010-01-11 19:09:50

+0

不,但我确实看到过不时提及的事情,所以我知道这是可能的,并试图让它透明地工作。我使用了“.Net 2.0 IL Assembler”这本书来确保我没有最终做出任何货物崇拜的东西。 ;-) – 2010-01-11 19:26:28

+0

这是一个非常不错的巧合:-) 该模板不适用于WinXp SP3上的Visual Studion 2008 Express SP1,但它适用于Windows 7 64位上的VS 2010 beta 2(bot在VirtualBox中运行) – 2010-01-11 19:55:26

1

万一有人会遇到同样的问题...

在我身边的一些错误:

文件:DllExportAttribute.cs

public CallingConvention CallingConvention { get; set; } 
public string ExportName { get; set; } 

文件:$项目名称$的.csproj

这些很容易修复。

这是一个非常严重的。从结果\ x86 \ MyDll.dll中制作implib时 - lib中的dll名称更改为\ MyDll.dll ...您可以制作tdump MyDll.dll,并将在导出部分“\ MyDll.dll”中取代“MyDll.dll” .DLL”。

由于这个问题的dll不能由软认定使用造成的lib中找到......在我的情况,因此它只能在C:\ MYDLL.DLL

通过使解决“coff2omf -lib :ca的MyDll.lib“在最初的lib。但之前,花了一天寻找解决方案...

0

看来,[DllExport(“...)]与导出名称“add”引发错误,与“sub”相同。 来自ilasm的vs 2010中的消息是“汇编文件名到DLL ...源文件是UNICODE”。

感谢这真的很棒!

0

如果有人也遇到它,我也得到这个错误,当导出的函数 名称是“init”,所以更改名称解决了问题。

所以这会产生这样的错误:

[DLLEXPORT( “初始化”)