0

I need to convert int value to LPBYTE. when I look at the definitions is shows like this. I'm not sure what is far word says.

typedef unsigned char       BYTE;
typedef BYTE far            *LPBYTE;
  1. What is the meaning of 'far'
  2. How to convert int value to LPBYTE

Edit

foo(LPBYTE x){
}

int main()
{
 int y = koo();
 foo(y); // how to cast here
 return 0;
}

Actual code

 int iVal = 0;
 LONG res = RegQueryValueEx(hKey, L"UseSystemSeparators", NULL, &lpType, (LPBYTE)iVal, &size);
Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147
  • [Here](http://stackoverflow.com/questions/3869830/near-and-far-pointers) is an explanation – martin Mar 17 '15 at 07:26
  • @Joachim Pileborg: int to a pointer – Nayana Adassuriya Mar 17 '15 at 07:28
  • Can you please clarify your question, is it about the `far` keyword? Or is it about "converting" to a pointer? If the latter, do you want to convert the `int` to a pointer, or do you want to get a pointer to the `int`? – Some programmer dude Mar 17 '15 at 07:28
  • Just to clarify, you have an `int` variable that contains an *address*? Or do you want to get the address of the `int` variable? – Some programmer dude Mar 17 '15 at 07:28
  • @Joachim Pileborg: edit the question, than for the comments – Nayana Adassuriya Mar 17 '15 at 07:31
  • 2
    The *far* keyword dates back to the 16-bit version of Windows, it declared the pointer to be a 32-bit pointer. Not relevant anymore today. When you want to cast *int* to LPBYTE then you haven't written enough 64-bit code, time to get cracking at it. – Hans Passant Mar 17 '15 at 07:37
  • Agree with Hans. You might have chosen a type which is too small. `INT_PTR` is a Windows type that's int-like but large enough to hold a pointer. – MSalters Mar 17 '15 at 08:10

2 Answers2

2

If I understand you correctly, you want the address of the variable, and then convert that address to LPBYTE.

Then you need to use the address-of operator & on the variable to get a pointer to the variable, and cast that pointer:

foo(reinterpret_cast<LPBYTE>(&y));

If the variable actually hold an address, then you first of all have to be very careful because it's not guaranteed that int can hold a memory address (i.e. a pointer). Think for example on a 64-bit system where pointers are 64 bits, but int is usually still a 32 bit type.

Use instead intptr_t which is guaranteed to be big enough to hold either an int or a pointer.

Then you should do e.g.

intptr_t y = ...;

foo(reinterpret_cast<LPBYTE>(y));

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • No I need to convert the value of the int variable to LPBYTE. because Int hold a resource handler – Nayana Adassuriya Mar 17 '15 at 07:35
  • shows a warning with reinterpret_cast `warning C4312: 'reinterpret_cast': conversion from 'int' to 'LPBYTE' of greater size` – Nayana Adassuriya Mar 17 '15 at 07:41
  • @NayanaAdassuriya You get the warning because `int` is smaller than a pointer. You should look at the code where you assign the variable, are you *really* assigning an address and storing it in the variable? You should update the question to show some *actual* code instead. – Some programmer dude Mar 17 '15 at 09:56
  • @NayanaAdassuriya The second-to-last argument is a pointer to the variable that receives the data, so it's the *first* part of my answer that you should read! Otherwise, since it's zero, it's equal to passing a `NULL` pointer, which [according to the official reference](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724911%28v=vs.85%29.aspx) is telling the function to not read the data. – Some programmer dude Mar 17 '15 at 10:29
  • @NayanaAdassuriya Also you should be careful, do you *know* that the data stored in the registry key is equal or smaller than the size of an `int`? Otherwise you might write beyond the limits of it and end up with [*undefined behavior*](http://en.wikipedia.org/wiki/Undefined_behavior). – Some programmer dude Mar 17 '15 at 10:33
0

I'm not sure what is far word says.

far is a 32-bit pointer which consists of 16-bit 'selector' that helps in determining the start address of the memory region, and a 16-bit offset into the memory region.

You can cast your variable like this:

int main()
{
 int y = koo();
 foo(static_cast<LPBYTE>(&y)); 
 return 0;
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331