Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I am trying to import a c++ dll into my C# windows project. But i am getting an error "Unable to find an entry point named 'Add' in DLL 'Test.dll'"

My c++ source code:
C#
#pragma once

using namespace System;

namespace Test {

    public ref class Class1
    {
    public: int Add(int i, int j)
        {
            return(i + j);
        }
    };
}



My C# windows source code:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Test;// my c++ dll refernce


namespace WindowsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("Test.dll")]
        public static extern int Add(int a, int b);
        private void button1_Click(object sender, EventArgs e)
        {
            int p = Add(1, 2);
        }
    }
}


I placed the c++ dll in c# application output path i e debug folder.

Please help to find me the error i made in the c++ or c# code.
Posted
Comments
[no name] 27-Jul-12 9:03am    
You need to export your "Add" function.
code4Better 30-Jul-12 3:13am    
I tried exporting the function and even the class itself as shown below. But still am getting same error. Please help!

#pragma once

using namespace System;

namespace Test {

public class __declspec(dllexport) Class1
{
public:static int Add(int i, int j)
{
return(i + j);
}
};
}

Your Add() function is a member of Test::Class1 and yet in your C# code you define it as public static. Change it to a stand-alone function not in any namespace or class and export its name, like this:
C++
__declspec(dllexport) int Add(int i, int j)
{
    return(i + j);
}
 
Share this answer
 
Comments
code4Better 30-Jul-12 4:51am    
I tried exporting the function and even the class itself as shown below. But still am getting same error. Please help!
#pragma once using namespace System;
namespace Test
{
public class __declspec(dllexport) Class1
{
public:static int Add(int i, int j)
{ return(i + j);
}
};
}
Richard MacCutchan 30-Jul-12 5:57am    
I have not been able to get this to work, and tried a Google search for help. The answer, as shown in some of these links, would appear to be that it is not possible.
code4Better 31-Jul-12 6:06am    
Any way thank you very much Mr.Richard
You need to export your function from the C++ class using __declspec(dllexport)

http://msdn.microsoft.com/en-us/library/a90k134d(v=vs.80).aspx[^]
 
Share this answer
 
Comments
code4Better 30-Jul-12 4:51am    
I tried exporting the function and even the class itself as shown below. But still am getting same error. Please help!
#pragma once using namespace System; namespace Test
{
public class __declspec(dllexport) Class1
{
public:static int Add(int i, int j)
{ return(i + j); }
};
}
The default directory of DLLImport is the Windows/System32 directory, not your application. You need to specify the location of your dll in absolute terms like

[DllImport("Test.dll")]

Check this out for an example of DLLImport:

http://www.pinvoke.net/default.aspx/user32.CascadeChildWindows[^]
 
Share this answer
 
Comments
code4Better 30-Jul-12 3:13am    
fds
code4Better 30-Jul-12 4:53am    
I tried exporting the function and even the class itself as shown below. But still am getting same error. Please help!
#pragma once using namespace System;
namespace Test
{
public class __declspec(dllexport) Class1
{ public:static int Add(int i, int j)
{ return(i + j);
}
};
}
In the above c++ code i used ref keyword which will make above c++ application as managed application. So no need to use DllImport attribute where as you can directly reference the dll(add reference in the solution explorer) and use it in C# code
 
Share this answer
 

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

  Print Answers RSS


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