开发者

Challenging Runtime Error when calling Native Code from Managed Code

开发者 https://www.devze.com 2022-12-13 10:28 出处:网络
First_Layer I have a win32 dll written in VC++6 service pack 6. Let\'s call this dll as FirstLayer. I do not have access to FirstLayer\'s source code but I need to call it from managed

First_Layer

I have a win32 dll written in VC++6 service pack 6. Let's call this dll as FirstLayer. I do not have access to FirstLayer's source code but I need to call it from managed code. The problem is that FirstLayer makes heavy use of std::vector and std::string and there is no way of marshaling these types into a C# application directly. The code for this layer below illustrates an example of what can be found in this dll.

Second_Layer

The solution that I can think of is to first create another win32 dll written in VC++6 service pack 6. Let's call this dll as "SecondLayer". SecondLayer acts as a wrapper for FirstLayer which basically converts STL types into custom written non STL class types.

Third_Layer

I also created a VC++2005 class library as a wrapper for SecondLayer. This wrapper does all the dirty work of converting the unmanaged SecondLayer into managed code. Let's call this layer as "ThirdLayer". The code for this layer as shown below is simplified to demonstrate the error so it does not do the above mentioned conversion.

Fourth_Layer

To top it all, I created a C#2005 console application to call ThirdLayer. Let's call this C# console application as "FourthLayer".

Call Sequence Summary

FourthLayer(C#2005) -> ThirdLayer(VC++2005) -> SecondLayer(VC++6) -> FirstLayer(VC++6)

The Runtime Error

The code below compile/build without errors but I get the following runtime error:

Unhandled Exception: System.AccessViolationException: Attempted开发者_运维知识库 to read or write protected memory. This is often an indication that other memory is corrupt. at SecondLayer.PassDataBackToCaller(SecondLayer ,StdVectorWrapper* ) at Sample.ThirdLayer.PassDataBackToCaller() in c:\project\on going projects\test\sample\thirdlayer\thirdlayer.cpp:line 22 at FourthLayer.Program.Main(String[] args) in C:\Project\On Going Projects\test\Sample\FourthLayer\Program.cs:line 14*

This error does not necessary appear when the FourthLayer application is executed on different operating system. For example, for Windows XP, there are no error but for other OS like Vista and Windows 7, the error will appear.

I do not understand what is causing this. Any ideas? How can I go about modifying the code to correct this?

// Fourth_Layer (C#2005 console application)

class FourthLayer
{
    static void Main(string[] args)
    {
        ThirdLayer thirdLayer = new ThirdLayer();
        thirdLayer.PassDataBackToCaller();
    }
}

// Third_Layer (VC++2005 class library)

public ref class ThirdLayer
{
    private:
        SecondLayer *_secondLayer;

    public:
        ThirdLayer();
        ~ThirdLayer();
        void PassDataBackToCaller();
};

ThirdLayer::ThirdLayer()
{
    _secondLayer = new SecondLayer();
}

ThirdLayer::~ThirdLayer()
{
    delete _secondLayer;
}

void ThirdLayer::PassDataBackToCaller()
{ 
    StdVectorWrapper v;
    _secondLayer->PassDataBackToCaller(v);

    for (int i=0; i<v.GetSize(); i++)
    {
        StdStringWrapper s = v.GetNext();
        std::cout << s.CStr() << std::endl;
    }
}

// Second_Layer - Main Class (VC++6 win32 dll)

class SECOND_LAYER_API SecondLayer
{
    private:
        FirstLayer *_firstLayer;

    public:
        SecondLayer();
        ~SecondLayer();
        void PassDataBackToCaller(StdVectorWrapper &toCaller);

    private:
        void ConvertToStdVectorWrapper(
            const std::vector<std::string> &in, StdVectorWrapper &out);
};

SecondLayer::SecondLayer() : _firstLayer(new FirstLayer())
{
}

SecondLayer::~SecondLayer()
{
    delete _firstLayer;
}

void SecondLayer::PassDataBackToCaller(StdVectorWrapper &toCaller)
{ 
    std::vector<std::string> v;
    _firstLayer->PassDataBackToCaller(v);
    ConvertToStdVectorWrapper(v, toCaller);
}

void SecondLayer::ConvertToStdVectorWrapper(
    const std::vector<std::string> &in, StdVectorWrapper &out)
{
    for (std::vector<std::string>::const_iterator it=in.begin(); it!=in.end(); ++it)
    {
        StdStringWrapper s((*it).c_str());
        out.Add(s);
    }
}

// Second_Layer - StdVectorWrapper Class (VC++6 win32 dll)

class SECOND_LAYER_API StdVectorWrapper
{
    private:
        std::vector<StdStringWrapper> _items;
        int index;  

    public: 
        StdVectorWrapper();
        void Add(const StdStringWrapper& item);
        int GetSize() const;  
        StdStringWrapper& GetNext(); 
};

StdVectorWrapper::StdVectorWrapper()
{
    index = 0;
}

void StdVectorWrapper::Add(const StdStringWrapper &item)
{
    _items.insert(_items.end(),item);
}

int StdVectorWrapper::GetSize() const
{
    return _items.size();
}

StdStringWrapper& StdVectorWrapper::GetNext()
{
    return _items[index++];
}

// Second_Layer - StdStringWrapper Class (VC++6 win32 dll)

class SECOND_LAYER_API StdStringWrapper
{
    private:
        std::string _s;

    public:  
        StdStringWrapper();
        StdStringWrapper(const char *s);
        void Append(const char *s);
        const char* CStr() const;  
};

StdStringWrapper::StdStringWrapper()
{
}

StdStringWrapper::StdStringWrapper(const char *s)
{
    _s.append(s);
}

void StdStringWrapper::Append(const char *s)
{
    _s.append(s);
}

const char* StdStringWrapper::CStr() const
{
    return _s.c_str();
}

// First_Layer (VC++6 win32 dll)

class FIRST_LAYER_API FirstLayer
{
    public:
        void PassDataBackToCaller(std::vector<std::string> &toCaller);
};

void FirstLayer::PassDataBackToCaller(std::vector<std::string> &toCaller)
{
    std::string a, b;
    a.append("Test string 1"); 
    b.append("Test string 2");
    toCaller.insert(toCaller.begin(),a);
    toCaller.insert(toCaller.begin(),b);
}


I found the solution. Bascially, there are two problems with it.

Problem One (Between FirstLayer and SecondLayer)

By default, the following setting of VC++6 is Multithreaded. This setting have to be changed to Multithreaded Dll for both the FirstLayer and SecondLayer. Both of which must be re-compiled with this new setting for it to work.

Project->Settings->C/C++ Tab->Category: Code Generation->Use run-time library->Multithreaded Dll

Problem Two (Between SecondLayer and ThirdLayer)

The StdStringWrapper and StdVectorWrapper class which I wrote do not implement deep copy. So all I need to do is to add the following to the StdStringWrapper and StdVectorWrapper class to implement deep copy.

  • Copy Constructor
  • Assignment Operator
  • Deconstructor

Edit: Alternative Solution for Problem Two

An even better solution would be to use clone_ptr for all the elements contained in std::vector as well as for std::vector itself. This eliminates the need for the copy constructor, assignment operator and deconstructor. So inside the StdVectorWrapper class, you would declare it as

clone_ptr< std::vector< clone_ptr< StdStringWrapper > > > _items;

0

精彩评论

暂无评论...
验证码 换一张
取 消