TTGroup
TTGroup

Reputation: 3703

Error when using namespace?

I'm researching about dllexport and dllimport between C++ and C#.

I have read this article http://functionx.com/csharp2/libraries/cppcli.htm

My C++ library code:

// Business.h

#pragma once

using namespace System;

namespace Business {

    public ref class Finance
    {
    public:
    double CalculateDiscount(double MarkedPrice,
                             double DiscountRate)
    {
        return MarkedPrice * DiscountRate / 100;
    }
    };
}

and here is C# code:

using System;
using System.Runtime.InteropServices;
using Business;

namespace DepartmentStore
{
    class Exercise
    {
        [DllImport("Business.dll")]
        public static extern double CalculateDiscount(double price,
                                                      double discount)

        static int Main()
        {
            Finance fin = new Finance();

        double markedPrice  = 275.50;
        double discountRate =  25.00; // %
            double discountAmount = fin.CalculateDiscount(markedPrice,
                                                          discountDate);
            double netPrice = markedPrice - discountAmount);

            Console.WriteLine("Marked Price:    {0:C}", markedPrice);
            Console.WriteLine("Discount Rate:   {0:P}", discountRate / 100);
            Console.WriteLine("Discount Amount: {0:C}", discountAmount);
            Console.WriteLine("Net Price:       {0:C}\n", netPrice);

        return 0;
        }
    }
}

But It encountered the error "The type or namespace name 'Business' could not be found (are you missing a using directive or an assembly reference?)"while building.

Someone can tell me how to fix it.

Many thanks

Upvotes: 0

Views: 243

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545518

DLL function importing and exporting uses a C ABI. This binary interface knows nothing about namespaces. Furthermore, the DLL you have created uses C++/CLI, not the C ABI. To use it, you wouldn’t use DLLImport, you’d reference the DLL in the project settings.

Upvotes: 6

Related Questions