Passing bytes by reference from C# into C++/CLI wrapper to call the InterlockedOr8 function

空扰寡人 提交于 2021-02-07 10:54:32

问题


I am trying to pass bytes by reference from C# such that I can call the atomic operation InterlockedOr8 on them. I have the following static library in C++:

Bitwise.h:

char EightBitOr(char volatile* destination, char value);

Bitwise.cpp:

#include "Bitwise.h"

char EightBitOr(char volatile* destination, char value)
{
    return InterlockedOr8(destination, value);
}

I am calling it from this C++/CLI wrapper DLL:

Wrapper.h:

#include "..\Bitwise\Bitwise.h"

using namespace System;

namespace Wrapper {
    public ref class Bitwise
    {
    public:
        static char InterlockedEightBitOr(volatile char* destination, char value);
    };
}

Wrapper.cpp:

#include "Wrapper.h"

char Wrapper::Bitwise::InterlockedEightBitOr(volatile char* destination, char value)
{
    return EightBitOr(destination, value);
}

I am trying to call the wrapper from a .NET 5.0 (Core) C# Console Application as follows:

var map = new byte[268435456];
Bitwise.InterlockedEightBitOr(ref map[0], (byte)1);

But I get the following error:

CS0570 'Bitwise.InterlockedEightBitOr(sbyte*, sbyte)' is not supported by the language

I also noticed the wrapper has translated the method signature into sbyte Bitwise.InterlockedEightBitOr(sbyte* destination, sbyte value); whereas I'd much rather pass unsigned bytes and I am not sure how to change this behavior. Anyways, I was wondering if anyone knew if there is any way I can pass bytes by reference into this CLI wrapper? What changes would I need to make?

来源:https://stackoverflow.com/questions/65998398/passing-bytes-by-reference-from-c-sharp-into-c-cli-wrapper-to-call-the-interlo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!