Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys and thanks for your replies and answers in advance

Iam trying to export my C++ methods to my C# project.

I have been able to export C++ fundtions to C# but im having a great deal with the methods

++export C++ functions i use the P/Invoke++
C++
extern "C" double Sum( int a, int b )
{
   return a+b;
}


my C# code is
C#
[DllImport ("test.dll")] //this is in C#
public static extern void Sum(int x, int y);

Console.WriteLine(Sum(3,4);


this way of doing things work but i just dont understand why I cant call the methods just as above
this is the mothod i need to call from my C#

C++
void myClass::Adder()
{
  MessageBox("something is being done in here");

}
Posted
Updated 10-Apr-12 5:56am
v2

I guess you are asking about how to marshal C++ class methods to C#. Then check here

How to Marshal a C++ Class[^]

http://blogs.msdn.com/b/sanpil/archive/2004/07/07/175855.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Apr-12 21:53pm    
Probably you understood this question better than I did. :-) My 5.
--SA
Lakamraju Raghuram 10-Apr-12 22:32pm    
but your answer dealt with "WHY interop" and I find it nice. The OP will take some time to appreciate it (or hope he will)
Your C pibrary export is using the wrong calling convention. By default, PInvoke uses the platform convention WinApi. This defaults to the _stdcall calling convention on normal Windows and Cdecl on Win CE platforms.

Change your extern decorator to:
extern double __stdcall Sum( int a, int b )


Also, when you decalred the function in your C# code, you declared the return type as void instead of double. That means your C# code isn't expecting a return value!
 
Share this answer
 
The quick answer is: because .NET platform is radically different from the unmanaged native platform you C++ application uses.

Not that those native methods exported by your DLL are not accessible or something like that; they simply make no sense. Two different kinds of code run "on different machines".

You C++ DLL code is compiled to the code using some particular instruction-set architecture (http://en.wikipedia.org/wiki/Instruction_set_architecture[^]) of the CPU directly: registers, virtual memory, CPU instructions, etc.

The managed .NET code is compiled for the CLR (Common Language Runtime, http://en.wikipedia.org/wiki/Common_Language_Runtime[^]). If you do not use P/Invoke, all the code is compiled into the byte code for the virtual machine (http://en.wikipedia.org/wiki/Common_Language_Runtime[^]). If you also only use the target "Any CPU", your .NET code will run on any system with any other instruction set architecture where CLR in implemented.

Moreover, you can run this code without recompilation on many other OS on different hardware, if you use some alternative CLR implementation, such as Mono (http://en.wikipedia.org/wiki/Mono_%28software%29[^]).

It can work this way, because the code is compiled to the target physical instruction set architecture during run time. This is called JIT — Just-in-Time compilation, please see http://en.wikipedia.org/wiki/Just-in-time_compilation[^]. The time of this compilation is… well, different. In most cases, it happens on-demand, per method, when some method is about to be called for the first time.

If you think about it, you will see why the direct calls across the different platforms are not possible. That's why the special mechanism is required. In practice, two approaches are available right now: using P/Invoke or using mixed-mode (managed+unmanaged) projects: C++/CLI + native C++.

If you need to learn P/Invoke, please see:
http://en.wikipedia.org/wiki/Platform_Invocation_Services[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

This Code Project article can also be useful:
http://www.codeproject.com/csharp/EssentialPInvoke.asp[^].

For C++/CLI, please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].

—SA
 
Share this answer
 
v3
Comments
Lakamraju Raghuram 10-Apr-12 12:44pm    
+5.
Sergey Alexandrovich Kryukov 10-Apr-12 21:51pm    
Thank you, Lakamraju.
--SA
[no name] 10-Apr-12 21:10pm    
5!
Sergey Alexandrovich Kryukov 10-Apr-12 21:51pm    
Thank you, Pranit.
--SA
Your C DLL is probably compiled targeting x86 and your C# code is compiled as AnyCPU and running on a 64-bit machine. You cannot mix 32- and 64-b code in the same process. Go into the C# apps project properties and change the CPU target to x86 then recompile your app.
 
Share this answer
 
OK this problem is solved! thank you very much for all your support.
the issue i was having was that the DLL which i was creating had a dependency upon another DLL therefore i had to call this DLL into the same folder as my output DLL
 
Share this answer
 
OK this problem is solved! thank you very much for all your support.
the issue i was having was that the DLL which i was creating had a dependency upon another DLL therefore i had to call this DLL into the same folder as my output DLL. Now the error is "unable to find an entry point named "Sum" in test.dll
 
Share this answer
 
Comments
Lakamraju Raghuram 11-Apr-12 11:59am    
At last you are getting some where ( or is it no where ?)
YES THE TARGET PLATFORM IS X86 ON MY c#. i just dont understand what the issue is. im a newbie in C++

if i have a method
C++
void test::goesTo()
{
//DO something in here
}

how would i be able to call this method?
 
Share this answer
 
Comments
[no name] 11-Apr-12 10:24am    
This is getting really confusing.
You know that you have to export the function from your DLL by using extern "C", and you know that to use that function in your C# app you have to DLLImport it just as you have already demonstrated that you know how to do it. And now you asking how to do something that you already know how to do?

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