Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C中如何从托管C++ DLL(使用包装类)获取字符串_C#_C++_C_Unmanaged_Managed - Fatal编程技术网

C# C中如何从托管C++ DLL(使用包装类)获取字符串

C# C中如何从托管C++ DLL(使用包装类)获取字符串,c#,c++,c,unmanaged,managed,C#,C++,C,Unmanaged,Managed,我使用非托管dll完成了这项工作,但在使用托管dll时遇到了一些困难 我想把一个字符串传递给我的托管C++包装类,它处理它并返回修改的字符串 P> > C++的DLL的目的是返回文件的十六进制代码,然后修改它,在DLL中执行一些复杂的任务,将它作为字符串传递给它,对于这个扩展,我使用了托管C++ + DLL,而不是非托管的。 我的C++类如下: using namespace std; //main function - entry point of program __declspec(d

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

我想把一个字符串传递给我的托管C++包装类,它处理它并返回修改的字符串

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

我的C++包装器类如下: 包含C++文件的头文件在这里没有显示为代码AD/AB/P >

 using namespace System;

 namespace CWrapperHexValue {

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

};
}
我的包装器类如下所示:

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;
}
// 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;
     }
//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);
            }
        }
最后,我发送文件名的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;
}
// 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;
     }
//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转换方面没有太多经验

请告知我如何改进代码以实现更好的内存管理,因为我必须逐个传递大量系统文件,并根据需要获取它们的十六进制代码

// hexcode logic
是的,在C中实现它可能会更好/更容易,并且忘记封送处理

如果你真的需要把字符串从C++封到C,这个主题可能会对你有很大帮助:代码在这里

<>基本上只需从你的本机代码中生成一个本地DLL,你就可以从C调用它,而不是中间的托管C++类,如果你只需要一个字符串。 在内存管理方面,如果您可以将hexcode逻辑移到C中,那么就不需要封送字符串,并可能为您节省一些处理,但更重要的是,调试的难度会降低。也就是说,你为什么不能这样做

C


我使用C++原生代码是因为性能问题,C++比C快,而且文件在C++中很容易操作。我已经在C中做了,而不是因为我的代码的性能成本而移到C++。感谢您的回复封送处理可能会消除使用本机代码可能带来的任何性能好处。如果它真的需要性能,为什么不把所有东西移到C++上,去处理托管代码?我用C来实现GUI和C++的所有硬核任务,我将返回的字符串将在修改GUI的过程中经过一些修改后使用。我想只使用PNEKKE,而不是使用中间托管C++类。pPoCKE可能更容易维护。我想与性能一起,如果比C++包装器性能更好,我肯定会使用它。