fbpx

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

More
4 months 2 weeks ago #1 by Mathias Koch
2.0.0.2: Cannot cast type 'int' to 'object' was created by Mathias Koch
Hi,

I have the following line of code that gives me a compiler error:
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

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago - 4 months 2 weeks ago #2 by Chris Pyrgas
Replied by Chris Pyrgas on topic 2.0.0.2: Cannot cast type 'int' to 'object'
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

XSharp Development Team
chris(at)xsharp.eu
Last edit: 4 months 2 weeks ago by Chris Pyrgas.

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago #3 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic 2.0.0.2: Cannot cast type 'int' to 'object'
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:
nResult := PCALLNATIVE<INT>( ptrFunc, PSZ( pcVersion))
SELF:oOLEObject := OBJECT( _CAST, nResult)

Wolfgang

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago #4 by Mathias Koch
Replied by Mathias Koch on topic 2.0.0.2: Cannot cast type 'int' to 'object'
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?
	LOCAL i := 67 AS INT
	LOCAL o1 AS OBJECT
	LOCAL o2 AS OBJECT
        o1 := (OBJECT)i
	o2 := OBJECT(_CAST, i)

Mathias

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago - 4 months 2 weeks ago #5 by Chris Pyrgas
Replied by Chris Pyrgas on topic 2.0.0.2: Cannot cast type 'int' to 'object'
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?

	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

XSharp Development Team
chris(at)xsharp.eu
Last edit: 4 months 2 weeks ago by Chris Pyrgas.

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago #6 by Robert van der Hulst
Replied by Robert van der Hulst on topic 2.0.0.2: Cannot cast type 'int' to 'object'
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
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago #7 by Mathias Koch
Replied by Mathias Koch on topic 2.0.0.2: Cannot cast type 'int' to 'object'
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

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago - 4 months 2 weeks ago #8 by Robert van der Hulst
Replied by Robert van der Hulst on topic 2.0.0.2: Cannot cast type 'int' to 'object'
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 :
docs.microsoft.com/en-us/dotnet/api/syst...w=netframework-4.7.2

Robert

XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.
Last edit: 4 months 2 weeks ago by Robert van der Hulst.

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago #9 by Chris Pyrgas
Replied by Chris Pyrgas on topic 2.0.0.2: Cannot cast type 'int' to 'object'
...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

XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

More
4 months 2 weeks ago #10 by Mathias Koch
Replied by Mathias Koch on topic 2.0.0.2: Cannot cast type 'int' to 'object'
Thank you, I'll check that out!

Mathias

Please Log in or Create an account to join the conversation.