2.0.0.2: Cannot cast type 'int' to 'object'

This forum is meant for questions and discussions about the X# language and tools
Post Reply
Kromi
Posts: 45
Joined: Wed Jan 13, 2016 8:31 am

2.0.0.2: Cannot cast type 'int' to 'object'

Post 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
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

2.0.0.2: Cannot cast type 'int' to 'object'

Post 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
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

2.0.0.2: Cannot cast type 'int' to 'object'

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Kromi
Posts: 45
Joined: Wed Jan 13, 2016 8:31 am

2.0.0.2: Cannot cast type 'int' to 'object'

Post 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
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

2.0.0.2: Cannot cast type 'int' to 'object'

Post 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
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

2.0.0.2: Cannot cast type 'int' to 'object'

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
Kromi
Posts: 45
Joined: Wed Jan 13, 2016 8:31 am

2.0.0.2: Cannot cast type 'int' to 'object'

Post 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
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

2.0.0.2: Cannot cast type 'int' to 'object'

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

2.0.0.2: Cannot cast type 'int' to 'object'

Post 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
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
Kromi
Posts: 45
Joined: Wed Jan 13, 2016 8:31 am

2.0.0.2: Cannot cast type 'int' to 'object'

Post by Kromi »

Thank you, I'll check that out!

Mathias
Post Reply