从托管C#代码调用c ++函数时的内存访问错误

时间:2014-11-24 20:56:02

标签: c# c++ access-violation

我有一个托管的C#程序,它在一个无人的C ++ DLL中调用一个函数。我得到一个关于记忆的错误。我已经不知疲倦地调试但似乎无法找到解决方案。请告诉我如何将正确的数据格式从C#传递给被调用的C ++函数。

这就是我所拥有的:

错误:

" at Frame_Processor.ProcessFrames(Frame_Processor* , SByte* , SByte* , Int32 , SByte* ) "   
     ***-- this is the  unmanged C++ function that is called.***

" at Frame_Processor_Wrapped.ProcessFrames(String framesPath, String datasetPath, Int32 frameCount, String keyName)" 
    ***-- this is the manged function that called it.***

似乎参数的格式是问题。为SByte * /串。请指教。

新编辑:

C#(托管代码)

static void Main(string[] args)
        {
            //test
            string id = "abcd";
            string in = "C:\\input\\";
            string out = "C:\\output";
            int count = 10;

            Frame_Processor_Wrapped fp = new Frame_Processor_Wrapped();
            fp.ProcessFrames(in, out, count, id);
        }

C ++ / CLI代码(使用Marshaling) - AKA Frame_Processor_Wrapped

 void Frame_Processor_Wrapped::ProcessFrames(System::String^ in, System::String^ out, int count, System::String^ id)
        {               
            StringConversion ^convert = gcnew StringConversion();

            //convert System.String to char*
            char* in_char= convert->myStringToChar(in);             
            char* out_char= convert->myStringToChar(out);
            char* id_char= convert->myStringToChar(id);

            frameProcessor->ProcessFrames(in_char, out_char, count, id_char);                               
        }

*

/* Conversion Class*/   
StringConversion::StringConversion() {}

        char* StringConversion::myStringToChar(System::String^ str)     {
            System::IntPtr ptr = Marshal::StringToHGlobalAnsi(str);         return
     static_cast<char*>(ptr.ToPointer());   }

*

C ++ DLL(无人值守代码)

void Frame_Processor::ProcessFrames(char* in, char* out, int count, char* id)
    {
        //use the parameters here for something (w,x,y,z varaibles)
        w = out;
        x = in;
        y = count;
        z = id;

        //do something
        RunProcessFrames();
    }

我收到有关内存访问的运行时错误。

0 个答案:

没有答案