Page 1 of 1
2.0.0.2: Cannot cast type 'int' to 'object'
Posted: Mon Sep 03, 2018 11:17 am
by Kromi
Hi,
I have the following line of code that gives me a compiler error:
Code: Select all
SELF:oOLEObject := OBJECT( _CAST, PCALLNATIVE<INT>( ptrFunc, PSZ( pcVersion)))
=> Cannot cast type 'int' to 'object'
Is this because PCallNative is not supported yet or is there another problem?
Mathias
2.0.0.2: Cannot cast type 'int' to 'object'
Posted: Mon Sep 03, 2018 11:24 am
by Chris
Hi Mathias,
PCallNative() already works fine, it's only CCallNatve() that is not implemented yet. But in your sample, this function returns INT (the type specified in the generic argument), this cannot be cast to an object as in VO. You will need to use a different approach in .Net, can you please show us the complete relevant code and also mention which OLE item you are using?
Chris
2.0.0.2: Cannot cast type 'int' to 'object'
Posted: Mon Sep 03, 2018 11:26 am
by wriedmann
Hi Mathias,
it must be something other because PCallNative() is supported and works in some of my migrated VO applications.
Maybe there is a problem with the cast. Do you have tried to decouple the line like this:
Code: Select all
nResult := PCALLNATIVE<INT>( ptrFunc, PSZ( pcVersion))
SELF:oOLEObject := OBJECT( _CAST, nResult)
Wolfgang
2.0.0.2: Cannot cast type 'int' to 'object'
Posted: Mon Sep 03, 2018 1:00 pm
by Kromi
Casting an int to an object should not be a problem, I guess.
Check this code. First assignment compiles, the second not. Aren't they equivalent?
Code: Select all
LOCAL i := 67 AS INT
LOCAL o1 AS OBJECT
LOCAL o2 AS OBJECT
o1 := (OBJECT)i
o2 := OBJECT(_CAST, i)
Mathias
2.0.0.2: Cannot cast type 'int' to 'object'
Posted: Mon Sep 03, 2018 1:44 pm
by Chris
Hi Mathias,
Kromi wrote:Casting an int to an object should not be a problem, I guess.
Check this code. First assignment compiles, the second not. Aren't they equivalent?
Code: Select all
LOCAL i := 67 AS INT
LOCAL o1 AS OBJECT
LOCAL o2 AS OBJECT
o1 := (OBJECT)i
o2 := OBJECT(_CAST, i)
In this specific code sample, it would had been safe to do such a cast, but this is code written specifically for .Net (X# or vulcan), it's not code that would compile in VO. When there is such a cast in VO, it is almost always used to convert an int, holding a pointer to an object to a var holding a reference to that object itself (like in your original code with the OLE object). This did work in VO (Win32 apps in general), but it is completely not allowed in .Net, it will never work like that.
For this reason, we intentionally disallowed that specific syntax (OBJECT(_CAST, i)) in order to trap such problems at compile time and not at runtime later. If that was code that was compiling without errors in Vulcan, then this was a problem, because if this code was executed, you would get a runtime exception. In X#, you get a warning (error actually) about this at compile time, so you discover it first, before the customer!
Chris
2.0.0.2: Cannot cast type 'int' to 'object'
Posted: Mon Sep 03, 2018 1:59 pm
by robert
Mathias,
In VO the OBJECT(_CAST) syntax tells the compiler that the memory location 67 is a reference to an object.
Apart from the fact that (in AnyCPU) a memory address does not always fit in an int, this is obviously wrong.
The syntax (OBJECT) i or OBJECT(i) tells the compiler to store the int value in an object.
In general you should look carefully at all _CAST keywords in your code.
_CAST tells the compiler that you know what you are doing. And quite often this has been used by people that did not know what they were doing....
Robert
2.0.0.2: Cannot cast type 'int' to 'object'
Posted: Tue Sep 04, 2018 6:35 am
by Kromi
Hi Robert,
thank you for explaining that. Now that old code makes sense to me and I think the one who wrote it knew what he did - in VO. But the code obviously doesn't work since it was ported to Vulcan.NET.
Mathias
2.0.0.2: Cannot cast type 'int' to 'object'
Posted: Tue Sep 04, 2018 11:14 am
by robert
If you really need to use an integer to represent an object then there is another trick in .Net.
You can use the GCHandle class to lock an object in memory and get its handle.
var gch := GCHandle.Alloc(oMyObject)
You can then call GCHandle.ToIntPtr(gch) to convert the handle to an IntPtr (4 bytes or 8 bytes for x86 or x64).
On the receiving side you can then use this IntPtr to get the GcHandle back and then access the object
var gch := GCHandle.FromIntPtr(param)
var omyObject := (MyObject)gch.Target
If you do this then please do not forget to call the Free() method on the handle. See for an example :
https://docs.microsoft.com/en-us/dotnet ... work-4.7.2
Robert
2.0.0.2: Cannot cast type 'int' to 'object'
Posted: Tue Sep 04, 2018 11:18 am
by Chris
...and for a sample on how to do this, you can have a look in the Vulcan SDK, in RichTextEdit.prg, search for "GCHandle".
Chris
2.0.0.2: Cannot cast type 'int' to 'object'
Posted: Tue Sep 04, 2018 11:23 am
by Kromi
Thank you, I'll check that out!
Mathias