I have wrapped a C++ API in C#. This API fills the supplied memory location with a byte array that represents program settings. I need to get Bit 0 to determine its state. Here is the C++ API and usage documentation:
DECL_FOOAPIDLL DWORD WINAPI FOO_GetVal(
VOID *Val, //pointer to memory where the data will be stored by the function
DWORD Len, //length of Val in bytes
DWORD Id //identification number of the parameter
);
Here is my C# wrapper and call (what i assume to be correct):
[DllImport(FOO_API, CharSet = CharSet.Auto)]
static public extern uint FOO_GetVal(IntPtr val, uint len, uint id);
IntPtr Ptr = Marshal.AllocHGlobal(5);
uint hr = NativeWrapper.FOO_GetVal(Ptr, 5, 1181);
var byteArray = new byte[5];
Marshal.Copy(Ptr, byteArray, 0, 5);
Marshal.FreeHGlobal(Ptr);
How do i get bit 0?
I’ve tried (with no success):
bool b = GetBit(bytearray[0],0);
private bool GetBit(byte b, int bitnum)
{
return (b & (1 << nitnum)) != 0;
}
All my code was correct… the API vendor supplied me with the incorrect ID parameter (the 3rd value)… I appreciate all the comments and my new understanding of least to most significant bit explained by Guvante.