DWORD(@ptrVal) VS DWORD(_CAST, ptrVal)

Public support forum for peer to peer support with related to the Visual Objects and Vulcan.NET products
Post Reply
Jamal
Posts: 314
Joined: Mon Jul 03, 2017 7:02 pm

DWORD(@ptrVal) VS DWORD(_CAST, ptrVal)

Post by Jamal »

I had an access violation resulting from using DWORD(ptrVal) because I did not cast the ptrVal to DWORD using _CAST.
While the CLVO group I saw using the address of ptrVal like DWORD(@ptrVal) which also works. So, is it the same as DWORD(_CAST, ptrVal) ?
My quick test below shows the same result. Is one better than the other?

Code: Select all

function start()
LOCAL y as ptr	
			
		y := 0x00000004     
		
		? "y: ", y 
		
		? "dword(@y)", dword(@y)
		? "dword(_cast, y)", dword(_cast, y)   
		
		wait  		
		
return nil
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

DWORD(@ptrVal) VS DWORD(_CAST, ptrVal)

Post by robert »

Jamal,

Code: Select all

? DWORD(ptrVal)
is using the ptrVal and tells the compiler that it is a pointer to a location where a dword is stored. That would be the same as

Code: Select all

LOCAL pDword AS DWORD PTR
pDword:= y
? pDword[1]
Of course this will crash when y has the value 0x00000004 , because that is usually an invalid memory location.
All the other examples will simply take the 32 bits from the PTR and will tell the compiler to use them as a DWORD.

I am not sure where you need this, but this kind of casting is considered "dangerous" and will only work in a 32 bit environment because the size of a PTR is the same as the size of a DWORD. In a 64 bits environment (if you would compile this in X# with AnyCPU) this will not be allowed.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Jamal
Posts: 314
Joined: Mon Jul 03, 2017 7:02 pm

DWORD(@ptrVal) VS DWORD(_CAST, ptrVal)

Post by Jamal »

Robert,

For this I am strictly using VO and I am checking the success or failure of the win32 function ShellExecute() which returns a PTR result.

Of course, I would not use ShellExecute() function with X# or other .NET languages since there are better functions.

Thanks for the info.

Jamal
Post Reply