Проблемы с запуском dll - PullRequest
0 голосов
/ 05 декабря 2018

При сборке dll ошибок нет, но она не работает.

При нажатии кнопки в программе должна появиться кнопка, появится сообщение "ТЕСТ".Но кнопка просто не появляется, несмотря на то, что во время сборки ничего не происходит.

Точно такой же код в 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 голосов
/ 05 декабря 2018

Похоже, вы перепутали понятия 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");
    }
    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...