-1

My main program is written in C++/CLI (managed). The API for some of my hardware is contained in a .C file. From my main program I call the main() for the unmanaged c code which creates an array and works with the hardware. Once completed it disconnects from the hardware, frees the memory, and returns to the C++/CLI program.

What would be a good way to access (copy) that array from the unmanaged c code to the managed c++?

Matt
  • 11
  • 1

1 Answers1

-2

See How to: Pin Pointers and Arrays; sample code is

#include <vector>
#include <algorithm>

#include <msclr/all.h>

using namespace System;

int main(array<System::String ^> ^args)
{
    constexpr size_t count = 100;

    std::vector<int> unmanged_ints;
    for (auto i = 0; i < count; i++)
        unmanged_ints.push_back(i);

    auto managed_ints = gcnew cli::array<int>(count);
    cli::pin_ptr<int> pManaged_ = &managed_ints[0];
    int* pManaged = pManaged_;

    std::copy(unmanged_ints.cbegin(), unmanged_ints.cend(), pManaged);

    return 0;
}
Ðаn
  • 10,934
  • 11
  • 59
  • 95
  • Will I still be able to pass the pinned array to the function written in C? The link reads like it pins it as an object. – Matt Dec 09 '16 at 14:55
  • So I changed my strategy a bit and decided to have the array be part of the managed code and pass it to the main() for the unmanaged c. I will need to pin the array to keep the GC from moving it, correct? – Matt Dec 13 '16 at 13:42