我想创建一个可以从非托管代码 (Delphi 5) 访问的 .NET 程序集。
我找到了Unmanaged Exports并按照那里的步骤操作,但我什至无法成功编译基本示例:
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 SP1、.NET 3.5 SP1
知道出了什么问题吗?谢谢。
<小时/>编辑23:40:
我发现了这个错误。它在函数的名称中 - add 和 Add 太相似了。当您更改其中之一时,它就会起作用。
最佳答案
顺便说一下,我刚刚更新了存档。 当您使用this时,您甚至可以为您准备好一切。相反。
这是一个项目模板,可以设置所有内容并且应该可以正常工作。
我确实在之前的版本中发现了一些观点,其中我做出了一些并不总是正确的假设。 我之前的实现的一个潜在问题是用于发布配置的/optimize 开关。在这种情况下,有时 ILAsm 会被 IL 阻塞,我在新版本中没有看到这种情况。
关于.net - 非托管导出 : Cannot compile assembly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2043214/