[英]How to get string from managed c++ dll (using wrapper class) in c#
i have done this using unmanaged dll but having some difficulty using managed dll. 我已经使用非托管DLL完成了此操作,但是在使用托管DLL时遇到了一些困难。
I want to pass a string to my managed c++ wrapper class which process it and return modified string 我想将字符串传递给托管的c ++包装器类,该类对该字符串进行处理并返回修改后的字符串
the purpose of c++ dll is to return hex code of a file(and later modifies it to do some complicated tasks within dll) which i pass it as a string, for this approcach i have used managed c++ dll instead of unmanaged one. C ++ DLL的目的是返回文件的十六进制代码(然后将其修改为在DLL中执行一些复杂的任务),我将其作为字符串传递,为此,我使用了托管的C ++ DLL而不是非托管的DLL。
my c++ class is as follows : 我的C ++类如下:
using namespace std;
//main function - entry point of program __declspec(dllexport)
const char* hexclass:: getfilename(char* filename)
{
//to open file
ifstream::pos_type size;
string virusScanStatus;
char* fileAccessError;
string errorDes ="Exception has Occured With Current File Operation";
//hexcode logic goes here
fileAccessError = new char[errorDes.size()];
errorDes.copy(fileAccessError,errorDes.size());
return fileAccessError;
}
my c++ wrapper class is as follows : header file including c++ file here (not shown for code readabitily) 我的c ++包装器类如下:头文件,其中包括c ++文件(不可读显示代码)
using namespace System;
namespace CWrapperHexValue {
public ref class cWrapperHexValue
{
public:
cWrapperHexValue();
const char* hexValue;
const char* getHexValue(char* fileName);
private:
hexclass* pHexClass;
};
}
and my wrapper class is as follows : 我的包装器类如下:
// This is the main DLL file.
#pragma once
#include "stdafx.h"
#include "CWrapperHexValue.h"
#include "D:\Projects\program10\program10\hexclass.cpp"
#include "D:\Projects\program10\program10\hexclass.h"
CWrapperHexValue::cWrapperHexValue::cWrapperHexValue()
{
pHexClass = new hexclass();
}
const char* CWrapperHexValue::cWrapperHexValue::getHexValue(char* fileName)
{
hexValue= pHexClass -> getfilename(fileName);
return hexValue;
}
and finally my c# code to send filename is as follows : 最后我的C#代码发送文件名如下:
//my c++ dll name is CWrapperHexValue
CWrapperHexValue.cWrapperHexValue objHexClass = new CWrapperHexValue.cWrapperHexValue();
byte[] fileNameBytes = Encoding.ASCII.GetBytes(fileNameForHexScan);
unsafe
{
fixed (byte* p= fileNameBytes)
{
sbyte* sp = (sbyte*)p;
sbyte* returnSp = objHexClass.getHexValue(sp);
}
}
now how do i get back the returnSp value as a string or any other better way to pass and get string, please provide useful code because i have not much experience with c++/c# cli conversion 现在如何将returnSp值作为字符串或任何其他更好的传递和获取字符串的方法取回,请提供有用的代码,因为我对c ++ / c#cli转换没有太多经验
please advice how can i improve my code for better memory management becuase i have to pass a whole lot of system files one by one and get their hex code 请提出建议,我该如何改善代码以更好地进行内存管理,因为我必须一一传递大量系统文件并获取其十六进制代码
Depending on what 取决于什么
// hexcode logic
is, it might be better/easier to implement it in C# and forget about the marshaling. 是,在C#中实现它可能会更好/更容易,而无需理会封送处理。
If you really need to marshal strings from C++ to C#, this topic will likely help you quite a bit: How to marshall c++ char* to C# string using P/INVOKE (code is here) 如果您确实需要将字符串从C ++编组为C#,则本主题可能会为您提供很多帮助: 如何使用P / INVOKE将C ++ char *编组为C#字符串 (代码在此处)
Basically just build a native DLL out of your native code and you can just pinvoke it from C# rather than have the intermediate managed C++ class if all you need is a string. 基本上,只是用您的本机代码构建一个本机DLL,如果需要的只是一个字符串,则可以从C#调用它,而不是使用中间托管C ++类。
In terms of memory management, if you can move the hexcode logic into C#, that would eliminate the need to marshal strings and likely save you some processing, but more importantly, be less painful to debug. 在内存管理方面,如果您可以将十六进制代码逻辑移至C#中,则将无需封送字符串,并可以节省一些处理时间,但更重要的是,调试起来不会那么麻烦。 Namely, why can't you do this?
即,为什么您不能这样做?
C# C#
public void SomeFunction()
{
string filepath = "foo.bar"
string hexVal = HexCodeLogicInManaged(filepath);
... other code ...
}
private string HexCodeLogicInManaged(string filepath)
{
// hexcode logic converted from native to managed
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.