非托管导出:无法编译程序集

发布于 2024-08-17 20:00:35 字数 4011 浏览 2 评论 0原文

我想创建一个可以从非托管代码(Delphi 5)访问的.NET 程序集。

我找到了非托管导出并按照其中的步骤操作,但我无法成功编译甚至是基本示例:

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:

我发现了这个错误。它在函数的名称中 - addAdd 太相似了。当你改变其中之一时,它就会起作用。

I want to create a .NET assembly that can be accessed from unmanaged code (Delphi 5).

I have found Unmanaged Exports and followed the steps there but I am unable to successfuly compile even the basic example:

using RGiesecke.DllExport;

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

DelphiNET.csproj project file:

...
<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" />
...

Here is the error:

------ 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 ==========

OS: WinXPSP3, Microsoft Visual C# 2008 Express Edition with SP1, .NET 3.5 SP1

Any idea what's wrong? Thanks.


Edit 23:40:

I have found the bug. It is in the name of the function - add and Add are too same. When you change one of them, it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清醇 2024-08-24 20:00:35

顺便说一句,我刚刚更新了存档。
当您使用此 相反。

这是一个项目模板,可以设置所有内容并且应该可以正常工作。

我确实在之前的版本中发现了一些观点,其中我做出了一些并不总是正确的假设。
我之前的实现的一个潜在问题是用于发布配置的 /optimize 开关。在这种情况下,有时 ILAsm 会被 IL 阻塞,但我在新版本中没有看到这种情况。

Incidentally, I just updated the archive.
You can even get it all laid out for you, when you take this instead.

It's a project template that sets everything up and should work just fine.

I did find some points in the previous version, where I made some assumptions that weren't always true.
And one potential problem with my previous implementation was the /optimize switch which is used for release configurations. Sometimes ILAsm would choke on the IL in that case, I haven't seen that with the new version.

小梨窩很甜 2024-08-24 20:00:35

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

我这边的一些错误:

文件:DllExportAttribute.cs

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

文件:$projectname$.csproj

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>  <!-- not working -->
<!-- change to -->
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets"/> <!-- working -->

这些很容易修复。

这是一个相当严重的问题。当从生成的 \x86\MyDll.dll 生成 implib 时 - lib 中的 dll 名称更改为 \MyDll.dll ...您可以制作 tdump MyDll.dll 并将进入导出部分“\MyDll.dll”而不是“MyDll” .dll”。

由于这个问题,使用生成库的软件无法找到 dll...在我的情况下,它只能在 c:\MyDll.dll 中找到 通过

在初始库。但在此之前花了一天时间寻找解决方案......

Just in case someone will meet the same problems...

Some errors on my side:

file: DllExportAttribute.cs

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

file: $projectname$.csproj

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>  <!-- not working -->
<!-- change to -->
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets"/> <!-- working -->

Those are easy to fix.

Here is one pretty serious. When making implib from resulting \x86\MyDll.dll - the name of dll in lib changes to \MyDll.dll ... you can make tdump MyDll.dll and will get in exports section "\MyDll.dll" instead of "MyDll.dll".

Because of this problem dll can't be found by the soft that use resulting lib... in my situation it can be found only in c:\MyDll.dll

Solved by making "coff2omf -lib:ca MyDll.lib" on the initial lib. But before that spent a day for looking solution...

毁虫ゝ 2024-08-24 20:00:35

似乎导出名称为“add”的 [DllExport("...)] 会引发错误,与“sub”相同。
vs 2010 中来自 ilasm 的消息是“将文件名组装到 dll ...源文件是 UNICODE”。

感谢这个非常伟大的工作!

It seems that [DllExport("...)] with the export name "add" throws an error, same with "sub".
Message in vs 2010 from ilasm is "assembling filename to dll ... Source file is UNICODE".

Thanks for this really great work!

愛放△進行李 2024-08-24 20:00:35

如果有人也遇到它,我在导出函数时也遇到这个错误
名称是“init”,因此更改名称可以解决问题。

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

[DllExport("init")]

If anyone also encounters it , i also got this error when the exported function
name was "init" so changing the name fixed the problem.

So this produces such an error:

[DllExport("init")]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文