I want to manually set address of Pointer to value stored in string variable. I have:
addr : String;
ptr : Pointer;
then:
addr:='005F5770';
How to assign it to the ptr?
Like this:
ptr := Pointer($005F5770);
You don't need a string variable since the address is a literal that is known at compile time.
In fact you can make this a constant since the value is known at compile time:
const
ptr = Pointer($005F5770);
Of course, if the value isn't a literal and really does start life as a string with hexadecimal representation then you first need to convert to an integer:
ptr := Pointer(StrToUInt64('$' + S));
Convert it to a UInt64 so that your code is immune to 32 bit pointer truncation when compiled for 64 bit.
Prepend the string hexadecimal number with $ or 0xand use the standard StrToInt():
ptr := Pointer(StrToInt('$'+addr));
If your pointer values are large and targeting a 64 bit compiler, consider using StrToInt64()
Note that a typecast from integer to a pointer is needed.
Related
Suppose I have the following struct –
struct MyStruct {
var value1: UInt16
var value2: UInt16
}
And I use this struct somewhere in my code like so -
var s = MyStruct(value1: UInt16(0), value2: UInt16(0))
I know that the struct will require 32-bits of storage for the two 16-bit integers –
What I am not certain about is whether swift is allocating two additional 64-bit pointers for each value in addition to one 64-bit pointer for the variable s.
Does this mean total storage requirement for the above code would result in the following?
MyStruct.value1 - 16-bits
MyStruct.value1 ptr - 64-bits
MyStruct.value2 - 16-bits
MyStruct.value2 ptr - 64-bits
s ptr - 64-bits
–––––––––––––––––––––––––––––
Total - 224-bits
Can someone please clarify?
MyStruct is 4 bytes because sizeof(UInt16) is 2 bytes. To test this for any given type, use sizeof. sizeof return the memory in bytes.
let size = sizeof(MyStruct) //4
If you want to get the size of a given instance you can use sizeOfValue.
var s = MyStruct(value1: UInt16(0), value2: UInt16(0))
let sSize = sizeofValue(s) //4
I believe the size of the pointer will depend on the architecture/compiler which is 64-bits on most computers and many newer phones but older ones might be 32 bit.
I don't think there is a way to actually get a pointer to MyStruct.value1, correct me if i'm wrong (i'm trying &s.value1.
Pointers
Structs in Swift are created and passed around on the stack, that's why they have value semantics instead of reference semantics.
When a struct is created in a function, it is stored on the stack so it's memory is freed up at the end of the function. It's reference is just an offset from the Stack Pointer or Frame Pointer.
It'll be four bytes on the stack.
Just try it in a XCode Playground:
The answer is 4 bytes.
I would like to build a color picker. I have tried this code
invoke GetDC,NULL
mov esi,eax
invoke GetPixel,esi,400,400
invoke lstrcpy,string ,eax
invoke SetDlgItemText,hWin,textbox1,string
invoke ReleaseDC,NULL,esi
but it returns P»© and things like that. how do I get it to return things like 00F0F0F0h
The GetPixel function returns a COLORREF value (which is really just a DWORD that specifies the RGB value of the pixel).
But your code treats the return value of GetPixel as if it were a string, passing it to lstrcpy. That won't work. lstrcpy is going to interpret it as if it were a pointer to a string somewhere in memory.
And that explains why you're getting nonsense like P»©, because the GetPixel function doesn't really return a memory address and the memory at that address doesn't really contain a valid nul-terminated string.
If you want to display a formatted numeric value as a string, you need to use a printf-style function to accomplish this. The Win32 API variant is called wsprintf.
You are not trying to format a string but a number, you need to pass the correct flags and specifier to wsprintf. What *printf does is format whatever you pass to it, and it will convert to a string according to your format specifier and put that string into the address passed to lpOut.
The %s specifier is for formatting strings. Lets say I wanted to display the return value of GetPixel as an 8 digit hex number with 0x in front of the number.
.data
szFmt db "%#08x", 0
.data?
Buf db 12 dup (?)
.code
invoke GetDC, NULL
invoke GetPixel, eax, 200, 200
invoke wsprintf, offset Buf, offset szFmt, eax
invoke MessageBox, NULL, offset Buf, NULL, MB_OK
Instead of calling MessageBox, you can do:
invoke SetDlgItemText, hWin, textbox1, offset Buf
Try that and see what MessageBox displays
http://msdn.microsoft.com/en-us/library/windows/desktop/ms647550(v=vs.85).aspx
Sorry if this question is really simple, but I tried all that I know and coudn't figure it out.
I'm trying to make a simple procedure which takes a string and a Count from the console and print the string number of times specified by the Count.
Everything is fine, but when I mov the Count to eax for a loop, the value get's messed up and I end up with an infinite loop of print.
I tried to change the Count to DWORD with atodw, but didn't work.
here's the code :
PrintString PROTO :DWORD, :DWORD
.data
String db 100 DUP(0)
Count db 10 DUP(0)
.code
start:
;1- get user input
invoke StdIn, addr String, 99
invoke StdIn, addr Count, 10
;2- Remove the CRLF from count
invoke StripLF, addr Count
;3- Convert the count to DWORD
invoke atodw, addr InputCount
mov Counter, eax
;4- Call the Printer function
invoke Printer, addr String, addr Count
Printer PROC StringToPrint:DWORD, count:DWORD
mov eax,count ;;;;;; This is the problem I think
Looppp:
push eax
invoke StdOut, StringToPrint
pop eax
dec eax
jnz Looppp
ret
Printer endp
You’re passing addr Count – the address of the string – as the second argument to Printer. But it expects an integer, so you want to pass Counter instead.
Since you’re using a language without type checking, adopting a naming convention such as Hungarian notation for your identifiers could help you see and avoid this kind of problem. With the variables here named strCount and dwCount, for example, it would be more obvious that you were using the wrong one.
As an aside, eax must eventually reach zero so your printing loop won’t be infinite – just rather longer than you intended…
I need to combine two 32-bit values to create a 64-bit value. I'm looking for something analogous to MAKEWORD and MAKELONG. I can easily define my own macro or function, but if the API already provides one, I'd prefer to use that.
I cannot find any in the Windows API. However, I do know that you work mostly (or, at least, a lot) with Delphi, so here is a quick Delphi function:
function MAKELONGLONG(A, B: cardinal): UInt64; inline;
begin
PCardinal(#result)^ := A;
PCardinal(cardinal(#result) + sizeof(cardinal))^ := B;
end;
Even faster:
function MAKELONGLONG(A, B: cardinal): UInt64;
asm
end;
Explanation: In the normal register calling convention, the first two arguments (if cardinal-sized) are stored in EAX and EDX, respetively. A (cardinal-sized) result is stored in EAX. Now, a 64-bit result is stored in EAX (less significant bits, low address) and EDX (more significant bits, high address); hence we need to move A to EAX and B to EDX, but they are already there!
Personally I prefer C-macros
#define MAKE_i64(hi, lo) ( (LONGLONG(DWORD(hi) & 0xffffffff) << 32 ) | LONGLONG(DWORD(lo) & 0xffffffff) )
I am trying to convert the following code from msvc to gcc
#define ltolower(ch) CharLower((LPSTR)(UCHAR)(ch))
char * aStr;
* aStr = (char)ltolower(*aStr);
This code is giving a compiler error: cast from ‘CHAR*’ to ‘char’ loses precision
My understanding is that tolower(int) from c wouldn't convert the whole string.
Thanks.
Your cast in CharLower is raising that error. Before doing that, you need to set the high order byte of the pointer passed to CharLower equals to ZERO.
From MSDN reference on the function:
If the operand is a character string,
the function returns a pointer to the
converted string. Because the string
is converted in place, the return
value is equal to lpsz.
If the operand is a single character,
the return value is a 32-bit value
whose high-order word is zero, and
low-order word contains the converted
character.
Something like this might work:
#define ltolower(ch) CharLower(0x00ff & ch)
If you are using a C++ compiler, you might also need a CAST operator:
#define ltolower(ch) CharLower((LPTSTR)(0x00ff & ch))
Haven't tested it though...