0

Currently I need to call a C# function from C++ code on windows phone, I read a lot about P/Invoke, this thread might be a duplicate.....

But so far I didn't find a "complete" thread includes projects and hook up teaches me how to do this Also there are tons of terminology confused me. So I have to create this thread:

Q1. If I don't want to use COM, the tech I should use is called ? This tech requires me to create a C# dll, then I can call the function in the C# dll from C++ code, right?


--------On C# side:

Q2. Project wise I should place the function I want to call from C++ code in a C# DLL project. So the type of the project I created is Is there anything else I need to manually setup on project side?

Q3. In the *.cs file, Use deligate, all the reference use deligate. But some of reference use

[DllExport( "blah.dll", CallingConvention=CallingConvention.StdCall) ]

Some of them not. Which confuses me. Is Dllexport must required for the dll function in *.cs?


--------On C++ side:

Q4. Project setting wise, People say, I should use C++/CLI(Common Language Infrastructure) What confused me here is, is (CLI) a setting in project? How do I know my C++ project is CLI or not? or how do I set that? How do I know my C++ project is Managed or not? or how do I set that?

Actually I new/delete in the C++, so it should be machine code(unmanaged) But does the C++ project that calls C# function has to be managed? And does managed/unmanaged has something to do with CLI?


--------On both C#/C++ project side:

Q5 None of the tutorial tells me how to hook up the projects!

I did this by adding the to the like this: Common Properties->Add New Reference

But when try to call the C# function from C++, It tells me error, unsolved external. I tried put the function in a C# name space, then in C++, I write:

using namespace MyCSharpNamespace;

It gives me an error:

error C2871: 'MyCSharpNamespace' : a namespace with this name does not exist

If it's a C++ dll, I just need to include the C++ header file, then it's done. But what do I do with a *.cs file?

I'll really appreciate the answer from you guys.

ArielX
  • 113
  • 6
  • You are not doing this correctly, add a new project and use the "Windows Runtime Component" project template. Create a public ref class, you can directly call it from your C# code. The language extension is called C++/CX, nothing to do with C++/CLI. – Hans Passant Apr 13 '15 at 17:27
  • I didn't get it, "you can directly call it from your C# code" But I want to call the C# function from C++. – ArielX Apr 13 '15 at 20:07
  • It works just as well the other way around. Events are also well supported. – Hans Passant Apr 13 '15 at 23:11

1 Answers1

0

OK, I just realize some part of me is wrong, I just know what is Reverse P/Invoke:

Reverse P/Invoke means:

Start the project in C#, Create a C# delegate function. Create a C++ dll. Call a function in C# -> call a function in C++ -> call the C# delegation function.

This is how the function stack look like:

|-> C# delegate function

|-> C++ dll function

|-> C# main function

Is that correct? So this is not calling a C# function from C++ .... What do I do if I want to call a C# function from C++ .... I mean, I want to start a C++ project, and call a C# function in that C++ project....

ArielX
  • 113
  • 6
  • 1
    Several ways to do this; see this question: http://stackoverflow.com/questions/778590/calling-c-sharp-code-from-c. To do this from unadulterated, non-CLI C++ code, COM would probably be the preferred method, and to use COM you just have to mark the project as COM-visible in its AssemblyInfo.cs file and make sure your classes follow some basic visibility and structure rules. – KeithS Apr 13 '15 at 20:53