1

I have two questions.They both are concerning a void in C++,which I am trying to translate in C#.

C++ code

void Func_X_2(LPBYTE stream, DWORD key, BYTE keyByte)
{
stream[0] ^= (stream[0] + LOBYTE(LOWORD(key)) + keyByte);
stream[1] ^= (stream[1] + HIBYTE(LOWORD(key)) + keyByte);
stream[2] ^= (stream[2] + LOBYTE(HIWORD(key)) + keyByte);
stream[3] ^= (stream[3] + HIBYTE(HIWORD(key)) + keyByte);
stream[4] ^= (stream[4] + LOBYTE(LOWORD(key)) + keyByte);
stream[5] ^= (stream[5] + HIBYTE(LOWORD(key)) + keyByte);
stream[6] ^= (stream[6] + LOBYTE(HIWORD(key)) + keyByte);
stream[7] ^= (stream[7] + HIBYTE(HIWORD(key)) + keyByte);
}

First question:

DWORD is UInt32,BYTE is byte,but what is LPBYTE? How to use it as an array?

Second question:

How to use LOBYTE,HIBYTE,LOWORD,HIWORD in C#?

EDIT

This is how the function is being called: C++ code

Func_X_2((LPBYTE)keyArray, dwArgs[14], keyByte);

keyArray is a DWORD(UInt32),dwArgs is an array of dword.KeyByte is a byte.

Thanks in advance.

3 Answers 3

2

LPBYTE is a pointer to a byte array. The equivalent in C# would be a variable of type byte[]. So you could translate your function like so:

public static void Func_X_2(byte[] stream, int key, byte keyByte)
{
    stream[0] ^= (byte)(stream[0] + BitConverter.GetBytes(LoWord(key))[0]);
    stream[1] ^= (byte)(stream[1] + BitConverter.GetBytes(LoWord(key))[1]);
    stream[2] ^= (byte)(stream[2] + BitConverter.GetBytes(HiWord(key))[0]);
    stream[3] ^= (byte)(stream[3] + BitConverter.GetBytes(HiWord(key))[1]);
    stream[4] ^= (byte)(stream[4] + BitConverter.GetBytes(LoWord(key))[0]);
    stream[5] ^= (byte)(stream[5] + BitConverter.GetBytes(LoWord(key))[1]);
    stream[6] ^= (byte)(stream[6] + BitConverter.GetBytes(HiWord(key))[0]);
    stream[7] ^= (byte)(stream[7] + BitConverter.GetBytes(HiWord(key))[1]);
}

public static int LoWord(int dwValue)
{
    return (dwValue & 0xFFFF);
}

public static int HiWord(int dwValue)
{
    return (dwValue >> 16) & 0xFFFF;
}
Sign up to request clarification or add additional context in comments.

1 Comment

How to call the function Func_X_2 if keyarray2 is an array[2] of dword?
1

LPBYTE stands for Long Pointer to Byte, so it's effectively a Byte array.

If you have an uint32, u (have to be careful shifting signed quantities):

LOWORD(u) =  (u & 0xFFFF);
HIWORD(u) =  (u >> 16);

assumes only bottom 16 bits set (ie. top 16 bits zero):

LOBYTE(b) = (b & 0xFF);
HIBYTE(b) = (b >> 8);

Comments

1

[...] what is LPBYTE? How to use it as an array?

It is a pointer to BYTE: a typedef, usually for unsigned char. You use it as you would use an unsigned char* to point to the first element of an array of unsigned characters. It is defined in windef.h:

typedef unsigned char       BYTE;    
typedef BYTE far            *LPBYTE;

How to use LOBYTE,HIBYTE,LOWORD,HIWORD in C#?

These are macros to fetch parts of a WORD. They are very easy to implement (as bit-fiddling operatios). These are also defined in windef.h. You can simply take the definitions out and paste it into custom C# functions:

#define LOWORD(l)           ((WORD)((DWORD_PTR)(l) & 0xffff))
#define HIWORD(l)           ((WORD)((DWORD_PTR)(l) >> 16))
#define LOBYTE(w)           ((BYTE)((DWORD_PTR)(w) & 0xff))
#define HIBYTE(w)           ((BYTE)((DWORD_PTR)(w) >> 8))

You may want to look at this SO post also for bit manipulation in C#.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.