运行dll时出现问题

时间:2018-12-05 11:28:07

标签: c# c++-cli

构建 dll 时没有错误,但是它不起作用。

单击时,程序中应出现一个按钮,消息“ TEST”将出现。 但是,即使在构建过程中不发出任何按钮,按钮也不会出现。

C#中完全相同的代码可以工作,但cli中需要。

我的DllExport:

#pragma region Usings
#include "stdafx.h"
using namespace System;
using namespace System::Runtime::InteropServices;


#pragma endregion

namespace RGiesecke
{
    namespace DllExport
    {
        /// <summary>
        /// Used to control how to create an unmanaged export for a static method.
        /// </summary>
        /// <remarks>
        /// You are not bound to using this class in this assembly.
        /// By default, any attribute named "RGiesecke.DllExport.DllExportAttribute.DllExportAttribute" will do the trick.
        /// Even if it is declared to be only visible inside the assembly with the static methods you want to export.
        /// In such a case the naming and typing of the fileds/properties is critical or otherwise the provided values will not be used.
        /// </remarks>
        [AttributeUsage(AttributeTargets::Method, AllowMultiple = false)]
        private ref class DllExportAttribute : Attribute
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="DllExportAttribute"/> class.
            /// </summary>
        public:
            DllExportAttribute()
            {
            }

            /// <summary>
            /// Initializes a new instance of the <see cref="DllExportAttribute"/> class.
            /// </summary>
            /// <param name="exportName">Name of the unmanaged export.
            /// <seealso cref="ExportName"/></param>
            DllExportAttribute(String ^exportName) //: DllExportAttribute(exportName, System::Runtime::InteropServices::CallingConvention::StdCall)


            {


            }


            /// <summary>
            /// Initializes a new instance of the <see cref="DllExportAttribute"/> class.
            /// </summary>
            /// <param name="exportName">Name of the unmanaged export.
            /// <seealso cref="ExportName"/></param>
            /// <param name="callingConvention">The calling convention of the unmanaged .
            /// <seealso cref="CallingConvention"/></param>
            DllExportAttribute(String ^exportName,  System::Runtime::InteropServices::CallingConvention callingConvention)
            {

                ExportName = exportName;
                CallingConvention = callingConvention;
            }

        private:
            static  System::Runtime::InteropServices::CallingConvention CConv = safe_cast< System::Runtime::InteropServices::CallingConvention>(0);


            /// <summary>
            /// Gets or sets the calling convention that will be used by the unmanaged export.
            /// </summary>
            /// <value>The calling convention.</value>
        public:
            property  System::Runtime::InteropServices::CallingConvention CallingConvention
            {
                 System::Runtime::InteropServices::CallingConvention get()
                {
                    return CConv;
                }
                void set( System::Runtime::InteropServices::CallingConvention value)
                {
                    CConv = value;
                }
            }

        private:
            String ^ExpName;

            /// <summary>
            /// Gets or sets the name of the unmanaged export.
            /// </summary>
            /// <value>The name of the export.</value>
        public:
            property String ^ExportName
            {
                String ^get()
                {
                    return ExpName;
                }
                void set(String ^value)
                {
                    ExpName = value;
                }
            }
        };
    }
}

我的课:

// ISHDAN.h

#pragma once
#include "DllExport\RGiesecke.DllExport.DllExportAttribute.h"

using namespace System::Windows::Forms;
using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace RGiesecke::DllExport;
namespace Class2 {

    public ref class Class1
    {

        [DllExport("ISHDAN", CallingConvention = System::Runtime::InteropServices::CallingConvention::Cdecl)]
        double static ISHDAN(String ^connTemp, String ^connEoi, String ^cex, String ^SV, String ^SO, String ^izd, String ^TP, String ^cher)
        {
            MessageBox::Show("TEST");
          return 1;
        }
    };
}

请您看一下代码,并给我一些提示吗?

1 个答案:

答案 0 :(得分:0)

似乎您已经混淆了C ++和C ++ / CLI概念,并且将它们合并不会使任何一个工作。

  • 在C ++ / CLI中,您无需将任何标签标记为DllExport。将课程公开并进行编译。在C#中,以与C#DLL相同的方式将其添加为引用。
  • 我不确定为什么要创建自己的DllExportAttribute。如果您自己制作,.Net编译器将不执行任何操作。

这是您需要做的所有事情:

  • 删除您的类DllExportAttribute。
  • 只需使用公共方法将C ++ / CLI类定义为公共类即可。

    namespace Class2
    {
        public ref class Class1
        {
        public:
            double static ISHDAN(String ^connTemp, String ^connEoi, String ^cex, String ^SV, String ^SO, String ^izd, String ^TP, String ^cher)
            {
                MessageBox::Show("TEST");
                return 1;
            }
        };
    }
    
  • 像从C#中调用普通方法一样调用它。

    static void Main(string[] args)
    {
        Class2.Class1.ISHDAN("temp", "eoi", "cex", "sv", "so", "izd", "tp", "cher");
    }