Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have C# as my front end application and I want to call c++ dll from my c# but I am getting error. I am posting my code here please help me out how to resolve this:

Program.cs
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCSharp
{
     class Program
     {
          [DllImport("C:\\Users\\xyz\\source\\repos\\Project1\\Debug\\TestCpp.dll", CallingConvention = CallingConvention.Cdecl)]
          public static extern void DisplayHelloFromDLL(StringBuilder name, int appId);

          static void Main(string[] args)
          {
               try
               {
                   StringBuilder str = new StringBuilder("name");                
                   DisplayHelloFromDLL(str, str.Length);
                   str.Clear();
               }
               catch(DllNotFoundException exception)
               {
                    Console.WriteLine(exception.Message);
               }
               catch(Exception exception)
               {
                   Console.WriteLine("General exception " + exception.Message);
               }
               finally
               {
                   Console.WriteLine("Try again");
               }
          }
     }
 }

and cpp code like below:
Header: source.h
#include <string>
using namespace std;

extern "C"
{
    namespace Test
    {
        class test
        {
        public:
            test();
            __declspec(dllexport) void DisplayHelloFromDLL(char * name, int appId);
        }
    }
}

c++ class: source.cpp
#include <stdio.h>
#include "CppSource.h"

Test::test::test()
{
    printf("This is default constructor");
}
void Test::test::DisplayHelloFromDLL(char * name, int appId)
{
    printf("Hello from DLL !\n");
    printf("Name is %s\n", name);
    printf("Length is %d \n", appId);
}

Code is building successfully but when I run this I got Unable to find an entry point named 'DisplayHelloFromDLL' in DLL.

Same CPP code when I am writing without using namespace and class, it is working fine. i.e.

Header: source.h
extern "C"
{
    __declspec(dllexport) void DisplayHelloFromDLL(char * name, int appId);
}

c++ class: source.cpp
#include "CppSource.h"

void DisplayHelloFromDLL(char * name, int appId)
{
    printf("Hello from DLL !\n");
    printf("Name is %s\n", name);
    printf("Length is %d \n", appId);
}

So how do I use DLL which has namespaces and claases in my c# application.

What I have tried:

Whatever I tried posted in the question.
Posted
Updated 25-Apr-20 22:44pm

The first problem is that DisplayHelloFromDLL is an instance method of the test class - which means that just like the C# equivalent:
C#
public class test
    {
    public void test()
        {
        printf("This is default constructor");
        }
    void DisplayHelloFromDLL(char * name, int appId)
        {
        printf("Hello from DLL !\n");
        printf("Name is %s\n", name);
        printf("Length is %d \n", appId);
        }   
    }
You cannot call it without referencing an instance of the test class:
C#
text t = new test();
t.DisplayHelloFromDLL(str, str.Length);

To call it without an instance, the method needs to be static: Static Keyword in C++[^] or not a part of a class at all.

And there is the real problem: you can't use C++ classes in C#, unless the DLL is built for CLI - and then you can just add a reference to it and use it as if it was in C#.
 
Share this answer
 
Your import statement is missing some important details. And dont use the full path for the import but locate the dll in the directory of the app.

Read my article and code about C# interoperbility Calling All Stations and compare the difference to your solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900