【问题标题】:How to get string from managed c++ dll (using wrapper class) in c#如何在 c# 中从托管 c++ dll(使用包装类)获取字符串
【发布时间】:2015-06-05 17:41:17
【问题描述】:

我使用非托管 dll 完成了这项工作,但使用托管 dll 时遇到了一些困难。

我想将一个字符串传递给我的托管 c++ 包装类,该类对其进行处理并返回修改后的字符串

c++ dll 的目的是返回文件的十六进制代码(然后修改它以在 dll 中执行一些复杂的任务),我将它作为字符串传递,对于这个方法,我使用托管 c++ dll 而不是非托管的.

我的 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;
}

我的 c++ 包装类如下: 此处包含 c++ 文件的头文件(代码可读性未显示)

 using namespace System;

 namespace CWrapperHexValue {

public ref class cWrapperHexValue
{
    public:
        cWrapperHexValue();
        const char* hexValue;
        const char* getHexValue(char* fileName);
    private:
        hexclass* pHexClass;

};
}

我的包装类如下:

// 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;
     }

最后我发送文件名的 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);
            }
        }

现在我如何将 returnSp 值作为字符串或任何其他更好的方式来传递和获取字符串,请提供有用的代码,因为我对 c++/c# cli 转换没有太多经验

请建议我如何改进我的代码以获得更好的内存管理,因为我必须一个接一个地传递大量系统文件并获取它们的十六进制代码

【问题讨论】:

    标签: c# c++ c unmanaged managed


    【解决方案1】:

    取决于什么

    // hexcode logic
    

    是,在 C# 中实现它并忘记编组可能会更好/更容易。

    如果您确实需要将字符串从 C++ 编组到 C#,那么本主题可能会对您有所帮助:How to marshall c++ char* to C# string using P/INVOKE(代码在此处)

    基本上只需从您的本机代码构建一个本机 DLL,如果您只需要一个字符串,您就可以从 C# 中调用它,而不是使用中间托管的 C++ 类。

    在内存管理方面,如果您可以将十六进制代码逻辑移动到 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
    }
    

    【讨论】:

    • 由于性能问题,我正在使用 c++ 本地代码,c++ 比 c# 快,并且文件易于在 c++ 中操作。由于我的代码的性能成本,我已经在 c# 中完成了它而不是移到 c++ 中。感谢回复
    • 封送处理可能会消除您从使用本机代码中可能获得的任何类型的性能优势。如果它确实需要高性能,为什么不将所有内容都迁移到 C++ 并取消托管代码?
    • 我将 c# 用于 gui,c++ 用于所有核心任务,我返回的字符串将用于(经过一些小的修改)来更新 gui 的进度。
    • 我会说只使用 pinvoke 而不是使用中间托管 C++ 类。 pinvoke 可能会更容易维护。
    • 我想追求性能,如果 pivoke 的性能比 c++ 包装类更好,我肯定会使用它
    猜你喜欢
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    相关资源
    最近更新 更多