简体   繁体   English

有关从C ++调用C#函数,在Windows Phone上反向P /调用的问题

[英]Questions about Calling C# function from C++, Reversed P/Invoke on windows phone

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..... 当前,我需要在Windows Phone上从C ++代码调用C#函数,我读了很多有关P / Invoke的内容,该线程可能是重复的.....

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. Q1。 If I don't want to use COM, the tech I should use is called ? 如果我不想使用COM,则应该使用的技术称为? This tech requires me to create a C# dll, then I can call the function in the C# dll from C++ code, right? 这项技术需要我创建一个C#dll,然后我才能从C ++代码中调用C#dll中的函数,对吗?


--------On C# side: --------在C#方面:

Q2. Q2。 Project wise I should place the function I want to call from C++ code in a C# DLL project. 从项目的角度来看,我应该将要从C ++代码调用的函数放在C#DLL项目中。 So the type of the project I created is Is there anything else I need to manually setup on project side? 因此,我创建的项目的类型是在项目侧手动设置是否还有其他需要?

Q3. Q3。 In the *.cs file, Use deligate, all the reference use deligate. 在* .cs文件中,使用deligate,所有引用都使用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? * .cs中的dll函数是否需要Dllexport?


--------On C++ side: --------在C ++方面:

Q4. 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? 人们说,明智的项目设置是,我应该使用C ++ / CLI(通用语言基础结构)这里让我感到困惑的是,(CLI)是项目中的设置吗? How do I know my C++ project is CLI or not? 我怎么知道我的C ++项目是否为CLI? or how do I set that? 或如何设置? How do I know my C++ project is Managed or not? 我怎么知道我的C ++项目是否受管理? 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? 实际上,我是在C ++中新建/删除的,因此应该是机器代码(非托管),但是调用C#函数的C ++项目是否必须进行托管? And does managed/unmanaged has something to do with CLI? 托管/非托管是否与CLI有关?


--------On both C#/C++ project side: --------在C#/ C ++项目的两个方面:

Q5 None of the tutorial tells me how to hook up the projects! Q5本教程都没有告诉我如何连接项目!

I did this by adding the to the like this: Common Properties->Add New Reference 我这样做是通过将添加到这样的:Common Properties-> Add New Reference

But when try to call the C# function from C++, It tells me error, unsolved external. 但是,当尝试从C ++调用C#函数时,它告诉我错误,外部未解决。 I tried put the function in a C# name space, then in C++, I write: 我尝试将函数放在C#名称空间中,然后在C ++中编写:

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. 如果它是C ++ dll,我只需要包含C ++头文件,就可以完成。 But what do I do with a *.cs file? 但是,我如何处理* .cs文件?

I'll really appreciate the answer from you guys. 我将非常感谢你们的答复。

OK, I just realize some part of me is wrong, I just know what is Reverse P/Invoke: 好的,我只是意识到自己的某些部分是错误的,我只知道什么是反向P /调用:

Reverse P/Invoke means: 反向P /调用是指:

Start the project in C#, Create a C# delegate function. 在C#中启动项目,创建一个C#委托函数。 Create a C++ dll. 创建一个C ++ dll。 Call a function in C# -> call a function in C++ -> call the C# delegation function. 在C#中调用函数->在C ++中调用函数->调用C#委托函数。

This is how the function stack look like: 函数堆栈如下所示:

|-> C# delegate function |-> C#委托函数

|-> C++ dll function |-> C ++ dll函数

|-> C# main function |-> C#主要功能

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.... 因此,这不是从C ++调用C#函数...。如果我想从C ++调用C#函数,该怎么办...。我的意思是,我想启动一个C ++项目,并在其中调用C#函数。 C ++项目...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM