Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

.net - Unmanaged Exports: Cannot compile assembly

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="DllExportDllExportAttribute.cs" />
  <Compile Include="PropertiesAssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets" />
<Import Project="DllExportRGiesecke.DllExport.targets" />
...

Here is the error:

------ Build started: Project: DelphiNET, Configuration: Release Any CPU ------
c:WINDOWSMicrosoft.NETFrameworkv3.5Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:TRACE /debug:pdbonly /filealign:512 /optimize+ /out:objReleaseDelphiNET.dll /target:library Class1.cs DllExportDllExportAttribute.cs PropertiesAssemblyInfo.cs

Compile complete -- 0 errors, 0 warnings
DelphiNET -> C:DelphiNETinReleaseDelphiNET.dll
ILDasm: calling 'C:Program FilesMicrosoft SDKsWindowsv6.0ABinildasm.exe' with /quoteallnames /nobar "/out:C:Documents and SettingsLukasLocal SettingsTempmp29FDelphiNET.il" "C:DelphiNETinReleaseDelphiNET.dll"
C:DelphiNETinReleaseDelphiNET.dll : warning EXP0009: Platform is AnyCpu, generating creating binaries for each CPU platform in a separate folder...
ILAsm: calling 'c:WINDOWSMicrosoft.NETFrameworkv2.0.50727ILAsm.exe' with /nologo "/out:C:DelphiNETinReleasex86DelphiNET.dll" "C:Documents and SettingsLukasLocal SettingsTempmp29FDelphiNET.x86.il" /DLL "/resource=C:Documents and SettingsLukasLocal SettingsTempmp29FDelphiNET.res"  /optimize  
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : 
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : Assembling 'C:Documents and SettingsLukasLocal SettingsTempmp29FDelphiNET.x86.il'  to DLL --> 'C:DelphiNETinReleasex86DelphiNET.dll'
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : Source file is ANSI
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : 
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::.ctor
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::.ctor
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::.ctor
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::get_CallingConvention
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::set_CallingConvention
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::get_ExportName
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : Assembled method RGiesecke.DllExport.DllExportAttribute::set_ExportName
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : 
C:DelphiNETDllExportRGiesecke.DllExport.targets(8,5): error : ***** FAILURE ***** 
C:DelphiNETDllExportRGiesecke.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...