Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to write a dll in C++ as a class library and call it’s methods in a C# program.

Note, with the VS 2010 C++ Express edition there is no MFC DLL. I have to put my methods in a C++ class and compile it to a DLL using the Class Library option.

Again, how do I call those C++ methods in C#. A code snippet, a website, or exactly what to type into my search engine would be appreciated.

I'm using VS 2010 Express for both C++ and C#.
Posted

ok first..C# has support for unmanaged code (which your case will be) since you're writing it in C++ (but if you'll be using VC++, It will be managed then).

Now after creating the DLL file (and pls remember to save it as a .DLL), you'll use the System.Runtime.InteropServices (which handles managed-unmanaged code interoperability) namespace of .NET to do your import

so first..add the System.Runtime.InteropServices namespace to your code..

and then follow the code sample below..which imports a DLL named : msvcrt and access the int typed method in it named : puts(string _c)

--remember that you do the DLL imports for each method called--

C#
// PInvokeTest.cs
using System;
using System.Runtime.InteropServices;

class PlatformInvokeTest
{
    /* DLL Import with 1st method */
    [DllImport("msvcrt.dll")]
    public static extern int puts(string c);

    /* DLL Import with 2nd method */
    [DllImport("msvcrt.dll")]
    internal static extern int _flushall();

/* Methods in DLL being used below  */
    public static void Main()
    {
        puts("Test");
        _flushall();
    }
}

but I must add that depending on what you want to do with the DLL, there are quite a number of things you can do (optional), such as Marshalling, assigning Entry Point, etc..So I would suggest you read some articles on it..There are loads of them online

If you like the above, Accept Answer and give a Vote.. :)
 
Share this answer
 
v2
Below is the C++ part.

Visual Studio 2010 C++
New project
Win32 Console Application
Name: calculator
Application type: DLL


// calculator.cpp : Defines the exported functions for the DLL application.


#include stdafx

static double Add (double x, double y)
{
return (x+y);
}
The above builds successfully as “calculator.dll”

----------------------------------------------------------

Below is the C# code
// Form1.cs
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

[DllImport("calculator.dll", EntryPoint = "Add")]
public static extern double Add(double x, double y);



public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

label1.Text = Add(2.2, 3.4).ToString();
}

}
}

The C# program built and ran till I clicked on button1. The following exception was generated:
“EntryPointNotFoundException was unhandled”

Unable to find an entry point named Add in DLL ‘calculator.dll'
 
Share this answer
 
v2
Hi,

do you mean MC++?!
If so you can easily reference it in c# and use it.
But if you want to write a dll in VC++ ( I mean unmanaged code), then you have to do what Lantei said.
 
Share this answer
 
OK,
you need to define them in your c++ dll.
To do so add a .def file to your project and enter your functions name in it like this:

VB
LIBRARY calculator

EXPORTS
Add @1


So whole your def file should be like that.
hope this can help you.
 
Share this answer
 
v2

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