How to Call C# DLL in C++

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 54800 Views - Last Post: 16 August 2011 - 01:04 AM Rate Topic: -----

#1 plenitude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-July 08

How to Call C# DLL in C++

Posted 29 July 2008 - 01:39 AM

I am a begineer in C# I have created sinmple DLL but hoe to call functions of DLL from C++ code.it can be simple client program for C++.
Following is code of DLL:
using System;
using System.Collections.Generic;
using System.Text;

namespace mcMath
{
	public class mcMathComp
	{
		private bool bTest = false;
		public mcMathComp()
		{
			 
			
			 
		}
		public void McTestMethod()
		{
			throw new System.NotImplementedException();
		}

		public long Add(long val1, long val2)
		{
			throw new System.NotImplementedException();
			return val1 + val2;
		} 

		public bool Extra
		{
			get
			{
				throw new System.NotImplementedException();
				return bTest;
			}
			set
			{
				bTest = Extra;
			}
		}

 

	}
}



thanks in Advance..
Please explain it simple way as I am a begineer
:)

Is This A Good Question/Topic? 0
  • +

Replies To: How to Call C# DLL in C++

#2 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: How to Call C# DLL in C++

Posted 29 July 2008 - 05:09 AM

I don't really understand what you want. if you are looking to call a C++ method from your C# code and you have the C++ DLL and .h files, we need to know what the method is you want to call, it's return type, and any parameters it takes. normally the .h file will have this information.
Was This Post Helpful? 0
  • +
  • -

#3 plenitude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-July 08

Re: How to Call C# DLL in C++

Posted 02 September 2008 - 03:23 AM

Hi Zakary,

I was busy in other assignments :rolleyes: .
Now I am back at this one..... :)

I want to call C# DLL from C++ code

plz help me some one,.......plz plz

As I am begineer plz explain ,if possible, by a simple example or code

Thankz in Advance.... B)
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: How to Call C# DLL in C++

Posted 02 September 2008 - 06:39 AM

Have you tried MSDN?
Was This Post Helpful? 2

#5 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: How to Call C# DLL in C++

Posted 02 September 2008 - 12:29 PM

View Postplenitude, on 2 Sep, 2008 - 06:23 AM, said:

Hi Zakary,

I was busy in other assignments :rolleyes: .
Now I am back at this one..... :)

I want to call C# DLL from C++ code

plz help me some one,.......plz plz

As I am begineer plz explain ,if possible, by a simple example or code

Thankz in Advance.... B)


you should post this question in the C++ form. They would be able to help you there.
Was This Post Helpful? 1

#6 plenitude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-July 08

Re: How to Call C# DLL in C++

Posted 04 September 2008 - 09:47 PM

I am a begineer,I have created DLL in C#

How to call DLL from C++ code?

Can u give a small client program as to how to call C# DLL

Following is code of DLL:
using System;
using System.Collections.Generic;
using System.Text;

namespace mcMath
{
	public class mcMathComp
	{
		private bool bTest = false;
		public mcMathComp()
		{
				   
			 
		}
		public void McTestMethod()
		{
			throw new System.NotImplementedException();
		}

		public long Add(long val1, long val2)
		{
			throw new System.NotImplementedException();
			return val1 + val2;
		} 

		public bool Extra
		{
			get
			{
				throw new System.NotImplementedException();
				return bTest;
			}
			set
			{
				bTest = Extra;
			}
		}

	}
}



Thankz in Advance :)
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: How to Call C# DLL in C++

Posted 05 September 2008 - 04:01 AM

Was there a problem with the reply I made in your original thread? It's rather complicated, and requires that the C# DLL be properly built to support being called from C++.
Was This Post Helpful? 0
  • +
  • -

#8 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How to Call C# DLL in C++

Posted 05 September 2008 - 09:36 AM

Well in the other thread someone did ask that he post here to get help.

Basically if you are using mannaged C++.Net then it is no problem to call C# dll from C++.

HOWEVER if you are working with non-mannaged C++ then it does become a little complicated since the C# dll will need to run on the .Net framework.

Why are you mixing languages? Why not just use 1 or the other. Chances are you can do what you need in C# rather than using C++ unless you need to do something outside of the .net framework in which case this will get complicated.
Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to Call C# DLL in C++

Posted 05 September 2008 - 09:45 AM

Please don't create duplicate topics, it doesnt help you to get help any faster, and can actually slow it down since people don't know which thread to reply to :) I'm merging the two topics :)
Was This Post Helpful? 0
  • +
  • -

#10 plenitude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-July 08

Re: How to Call C# DLL in C++

Posted 09 September 2008 - 04:57 AM

Hi
I was told to post it on c/c++ FORUM RATHER ON c#

I 'll take care nxt time...

Thanx JACKOFALL.... I did worked on msdn link tht u provided...
but in my case I don't have to create client ,just have to do my part of creating DLL in C# & tell how to call my DLL in sombody elses C++ code..................... :ph34r:

plz help
how to tell the other person way to call my C# DLL
Was This Post Helpful? 0
  • +
  • -

#11 plenitude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-July 08

Re: How to Call C# DLL in C++

Posted 14 September 2008 - 09:39 PM

View Postplenitude, on 9 Sep, 2008 - 04:57 AM, said:

Hi
I was told to post it on c/c++ FORUM RATHER ON c#

I 'll take care nxt time...

Thanx JACKOFALL.... I did worked on msdn link tht u provided...
but in my case I don't have to create client ,just have to do my part of creating DLL in C# & tell how to call my DLL in sombody elses C++ code..................... :ph34r:

plz help
how to tell the other person way to call my C# DLL



Somebody plz help I am really struck........help help
Was This Post Helpful? 0
  • +
  • -

#12 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: How to Call C# DLL in C++

Posted 14 September 2008 - 11:32 PM

NickDMax is pretty much spot on.

The only other way I can think of doing this is by using COM. Your C# dll Will have to use COM and Interop.

Let me google and see what I find: ... ah here we go
http://www.codeproje...ManagedCOM.aspx

If you don't know COM or how it works this may not be the best solution for you.
Was This Post Helpful? 0
  • +
  • -

#13 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: How to Call C# DLL in C++

Posted 15 September 2008 - 03:45 PM

View Postskaoth, on 15 Sep, 2008 - 02:32 AM, said:

The only other way I can think of doing this is by using COM. Your C# dll Will have to use COM and Interop.


That's essentially what the MSDN link above is about. It shows how to create a COM callable wrapper using Interop, and calling the wrapper like any other Coclass.

The inherent problem here is that even though the files have a .DLL extension, they are very different than real DLL's. The same is true of managed EXE's (supposedly, managed EXE's and DLL's are exactly the same to the system). One can't just load a DLL like this with LoadLibrary and use it directly (at least in a traditional sense).

I particularly like the idea of using only one programming language here. Bridging something like .NET and a native executable is going to be complicated. Bridging Java and C++ is hard. Perl to C++ is hard. Python to C++ is hard. Heck, in some cases, bridging C++ to C++ is hard!
Was This Post Helpful? 0
  • +
  • -

#14 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: How to Call C# DLL in C++

Posted 15 September 2008 - 04:01 PM

Yeah... I didn't follow that link.. should be paying more attention.

Quote

particularly like the idea of using only one programming language


This is the ideal situation and generally is the best solution. Unfortunately, that isn't always the case. I know find myself having to bridge java and C/C++!!

For this particular problem (if there is not way to get out of using multiple languages) I'd actually go for calling unmanaged code in managed code. Then again I'm probably one of the few left that still likes the COM paradigm.
Was This Post Helpful? 0
  • +
  • -

#15 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: How to Call C# DLL in C++

Posted 15 September 2008 - 04:03 PM

View Postskaoth, on 15 Sep, 2008 - 07:01 PM, said:

For this particular problem (if there is not way to get out of using multiple languages) I'd actually go for calling unmanaged code in managed code. Then again I'm probably one of the few left that still likes the COM paradigm.


At least going in that direction, you don't even need to use COM (just DllImport like with other API calls).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2