Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 476121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:27:27+00:00 2026-05-13T00:27:27+00:00

First_Layer I have a win32 dll written in VC++6 service pack 6. Let’s call

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:27:27+00:00Added an answer on May 13, 2026 at 12:27 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First let me say that I really feel directionless on this question. I am
I have two layers. The first layer is Image control. The source of it
First, let's get the security considerations out of the way. I'm using simple authentication
First off, I am using Windows XP. I have multiple hard drives and it
First of all, I know how to build a Java application. But I have
First off, there's a bit of background to this issue available on my blog:
First off, I'm working on an app that's written such that some of your
I have a big solution with lots of projects, using VS2008 SP1, and at
Okay, so at first when i run my win32 program the menu works fine,
I have the same problem as described in the posts listed below. That is,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.