0

У меня есть byte[] на стороне C# который мне нужно передать в CLI метод и там в него скопировать данные из void*

Делаю это так

C#

byte[] copy;
m_Logic.CopyToArray(out copy);
//There is filled `copy` that I can use

C++ (CLI)

    void Agent_CLI::CopyToArray([Out] array<unsigned char> ^% input)
    {
        std::vector<int> data = { 1, 2, 3, 4 };
        void * data_to_copy = data.data(); //TEST : my data to copy

        int data_size = (int)data.size();
        input = gcnew array<unsigned char>(data_size);
        memcpy(&input[0], data_to_copy, data_size * sizeof(unsigned char));
    }

но получаю вот такую ошибку

error C2664: 'void *memcpy(void *,const void *,::size_t)': cannot convert argument 1 from 'cli::interior_ptr' to 'void *'

что не так?

2

1 ответ 1

1

Спасибо @VladD в итоге получилось вот так

    void Agent_CLI::CopyToArray([Out] array<unsigned char> ^% input)
    {
        std::vector<unsigned char> data = { 1, 1, 1, 0 };
        void * data_to_copy = data.data(); 

        int data_size = (int)data.size();
        input = gcnew array<unsigned char>(data_size);

        pin_ptr<unsigned char> p = &input[0]; 
        unsigned char * cp = p;

        memcpy(cp, data_to_copy, data_size * sizeof(unsigned char));
    }

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.