fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

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

2.0.0.2: Cannot cast type 'int' to 'object' 9 months 1 week ago #6049

  Kromi's Avatar Topic Author Kromi Offline Posts: 32
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.

2.0.0.2: Cannot cast type 'int' to 'object' 9 months 1 week ago #6051

  Chris's Avatar Chris Offline Posts: 1137
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

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

Last edit: by Chris.

2.0.0.2: Cannot cast type 'int' to 'object' 9 months 1 week ago #6052

  wriedmann's Avatar wriedmann Offline Posts: 1545
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

2.0.0.2: Cannot cast type 'int' to 'object' 9 months 1 week ago #6056

  Kromi's Avatar Topic Author Kromi Offline Posts: 32
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.

2.0.0.2: Cannot cast type 'int' to 'object' 9 months 1 week ago #6063

  Chris's Avatar Chris Offline Posts: 1137
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

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

Last edit: by Chris.

2.0.0.2: Cannot cast type 'int' to 'object' 9 months 1 week ago #6066

  robert's Avatar robert Offline Posts: 969
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.

2.0.0.2: Cannot cast type 'int' to 'object' 9 months 1 week ago #6078

  Kromi's Avatar Topic Author Kromi Offline Posts: 32
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.

2.0.0.2: Cannot cast type 'int' to 'object' 9 months 1 week ago #6082

  robert's Avatar robert Offline Posts: 969
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.

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

Last edit: by robert.

2.0.0.2: Cannot cast type 'int' to 'object' 9 months 1 week ago #6083

  Chris's Avatar Chris Offline Posts: 1137
...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.

2.0.0.2: Cannot cast type 'int' to 'object' 9 months 1 week ago #6084

  Kromi's Avatar Topic Author Kromi Offline Posts: 32
Thank you, I'll check that out!

Mathias

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

  • Page:
  • 1