管理C形成c#和C之间的桥梁

管理C形成c#和C之间的桥梁,第1张

概述我有点生锈,其实真的生锈了我的C.自从大一学年以来,没有碰到过,所以已经有一段时间了. 无论如何,我正在做大多数人做的事情.从C调用C#代码.我在网上做了一些研究,似乎我需要创建一些管理C来形成桥梁.使用__declspec(dllexport),然后创建一个dll,并将整个东西作为一个包装器. 但我的问题是 – 我真的很难找到例子.我发现一些基本的东西,有人想使用C#版本的String.ToUp 我有点生锈,其实真的生锈了我的C.自从大一学年以来,没有碰到过,所以已经有一段时间了.

无论如何,我正在做大多数人做的事情.从C调用C#代码.我在网上做了一些研究,似乎我需要创建一些管理C来形成桥梁.使用__declspec(dllexport),然后创建一个dll,并将整个东西作为一个包装器.

但我的问题是 – 我真的很难找到例子.我发现一些基本的东西,有人想使用C#版本的String.toupper(),但这是非常基本的,只是一个小代码片段.

任何人都有什么想法可以在哪里寻找更具体的东西?注意,我不想使用COM.目标是不要碰C#代码.

解决方法 虽然我打败我写一个例子,我会发布,无论如何,以防万一…

编写包装器以访问自己的库的过程与访问一个标准的.Net库一样.

一个名为CsharpProject的C#类代码示例:

using System;namespace CsharpProject {    public class CsharpClass {        public string name { get; set; }        public int Value { get; set; }        public string GetdisplayString() {            return string.Format("{0}: {1}",this.name,this.Value);        }    }}

您将创建一个受管理的C类库项目(例如CsharpWrapper),并添加您的C#项目作为参考.为了使用相同的头文件进行内部使用和引用项目,您需要一种使用正确的declspec的方法.这可以通过定义预处理器指令(在这种情况下为CSHARPWRAPPER_EXPORTS)并使用#ifdef在头文件中的C/C++接口中设置导出宏来完成.非管理的接口头文件必须包含非托管的东西(或者由预处理器滤除).

非管理C接口头文件(CppInterface.h):

#pragma once#include <string>// Sets the interface function's decoration as export or import#ifdef CSHARPWRAPPER_EXPORTS #define EXPORT_SPEC __declspec( dllexport )#else#define EXPORT_SPEC __declspec( dllimport )#endif// Unmanaged interface functions must use all unmanaged typesEXPORT_SPEC std::string GetdisplayString(const char * pname,int iValue);

然后,您可以创建一个内部头文件,以便能够包含在托管库文件中.这将添加using namespace语句,并且可以包括您需要的帮助器函数.

管理C接口头文件(CsharpInterface.h):

#pragma once#include <string>// .Net System namespacesusing namespace System;using namespace System::Runtime::InteropServices;// C# Projectsusing namespace CsharpProject;//////////////////////////////////////////////////// String Conversion FunctionsinlineString ^ ToManagedString(const char * pString) { return Marshal::PtrToStringAnsi(IntPtr((char *) pString));}inlineconst std::string ToStdString(String ^ strString) { IntPtr ptrString = IntPtr::Zero; std::string strStdString; try {  ptrString = Marshal::StringToHGlobalAnsi(strString);  strStdString = (char *) ptrString.topointer(); } finally {  if (ptrString != IntPtr::Zero) {   Marshal::FreeHGlobal(ptrString);  } } return strStdString;}

然后你只需编写你的接口代码来做包装.

托管C接口源文件(CppInterface.cpp):

#include "CppInterface.h"#include "CsharpInterface.h"std::string GetdisplayString(const char * pname,int iValue) { CsharpClass ^ oCsharpObject = gcnew CsharpClass(); oCsharpObject->name = ToManagedString(pname); oCsharpObject->Value = iValue; return ToStdString(oCsharpObject->GetdisplayString());}

然后在非托管项目中包含非托管头,告诉链接器在链接时使用生成的.lib文件,并确保.Net和包装DLL与您的非托管应用程序在同一个文件夹中.

#include <stdlib.h>// Include the wrapper header#include "CppInterface.h"voID main() { // Call the unmanaged wrapper function std::string strdisplayString = GetdisplayString("Test",123); // Do something with it printf("%s\n",strdisplayString.c_str());}
总结

以上是内存溢出为你收集整理的管理C形成c#和C之间的桥梁全部内容,希望文章能够帮你解决管理C形成c#和C之间的桥梁所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1233388.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存