首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#非托管导出无法工作

C#非托管导出无法工作
EN

Stack Overflow用户
提问于 2017-02-26 22:45:36
回答 1查看 2.7K关注 0票数 5

我正在努力使非管理出口基本示例正常工作。

我正在采取的步骤如下:

  • 创建一个新的类库项目。
  • 添加UnmanagedExports与nuget。
  • 将CPU目标更改为x86。
  • 将我遵循的教程中的代码添加到.cs文件中。
  • 构建

该项目构建成功,但当我使用DLL导出查看器检查我的dll时,我看不到我的任何函数。

我使用的是32位操作系统和SharpDevelop 4.4 (我也尝试过其他SharpDevelop版本和64位操作系统,结果是相同的)。

我的sln文件:

代码语言:javascript
运行
复制
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{AFAA816C-65B2-4B58-9FB2-EB7482AA0F5F}"
    EndProject
    Global
        GlobalSection(SolutionConfigurationPlatforms) = preSolution
            Debug|x86 = Debug|x86
            Release|x86 = Release|x86
        EndGlobalSection
        GlobalSection(ProjectConfigurationPlatforms) = postSolution
            {AFAA816C-65B2-4B58-9FB2-EB7482AA0F5F}.Debug|x86.ActiveCfg = Debug|x86
            {AFAA816C-65B2-4B58-9FB2-EB7482AA0F5F}.Debug|x86.Build.0 = Debug|x86
            {AFAA816C-65B2-4B58-9FB2-EB7482AA0F5F}.Release|x86.ActiveCfg = Release|x86
            {AFAA816C-65B2-4B58-9FB2-EB7482AA0F5F}.Release|x86.Build.0 = Release|x86
        EndGlobalSection
    EndGlobal

我的csproj文件:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
  <PropertyGroup>
    <ProjectGuid>{AFAA816C-65B2-4B58-9FB2-EB7482AA0F5F}</ProjectGuid>
    <ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
    <OutputType>Library</OutputType>
    <RootNamespace>test</RootNamespace>
    <AssemblyName>test</AssemblyName>
    <TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <TargetFrameworkProfile />
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Platform)' == 'x86' ">
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
    <OutputPath>bin\Debug\</OutputPath>
    <DebugSymbols>True</DebugSymbols>
    <DebugType>Full</DebugType>
    <Optimize>False</Optimize>
    <CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
    <OutputPath>bin\Release\</OutputPath>
    <DebugSymbols>False</DebugSymbols>
    <DebugType>None</DebugType>
    <Optimize>True</Optimize>
    <CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
    <DefineConstants>TRACE</DefineConstants>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="RGiesecke.DllExport.Metadata">
      <HintPath>..\packages\UnmanagedExports.1.2.7\lib\net\RGiesecke.DllExport.Metadata.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="MyClass.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="packages.config" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

我的cs档案:

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Text;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;

namespace Testme
{
    class Test
    {
        [DllExport("Add", CallingConvention = CallingConvention.StdCall)]
        public static int Add(int left, int right)
        {
            return left + right;
        }
........

Dll导出查看器

工作Dll的dll导出查看器(从教程网站下载)

对于我的操作系统,两个DLL (我的和下载的)都有相同的大小,但对于DLL Export则没有。

我错过了什么??

EN

回答 1

Stack Overflow用户

发布于 2019-04-11 01:25:51

我也遇到了同样的问题,我用这种方法解决了这个问题,您需要在这一行csproj文件下面添加下面一行

代码语言:javascript
运行
复制
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

  <!-- Remove this comment and insert This line-->
  <Import Project="packages/UnmanagedExports.1.2.7/tools/RGiesecke.DllExport.targets" Condition="Exists('packages/UnmanagedExports.1.2.7/tools/RGiesecke.DllExport.targets')"/>

检查文件夹"packages/UnmanagedExports.1.2.7/tools/RGiesecke.DllExport.targets“中存在的文件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42475169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档