Recent

Author Topic: [SOLVED] How to typecast a function result  (Read 1136 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
[SOLVED] How to typecast a function result
« on: February 24, 2021, 12:31:52 pm »
I have been using an API of a commercial software product to code some helpful bespoke behaviour. It has gone well, overall. But, I have hit a snag.

One of the functions seems to magically (in my view) give a different TYPE of result depending on what paramater the programmer sends to the function. The documentation for the function states : "Retrieves information about the object, either through the buffer or the return value. If through the return value, you may need to cast it to the appropriate type in order to correctly interpret/understand it."

In my use case, the result is not passed to a buffer, so the information about the object that I see is passed through the return value. The return value I need is of type LPWSTR. But the function returns INT64. It seems to suggest that somehow I typecast the result "to correctly interpret/understand it", but I have zero clue how to typecast an INT64 to LPWSTR when the result type needed for successful compilation is INT64.

Can anyone steer me here? I have read several of the typecast wiki articles but I'm sorry to say they are not helping me understand this issue.

Thanks
« Last Edit: February 24, 2021, 08:31:15 pm by Gizmo »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: How to typecast a function result
« Reply #1 on: February 24, 2021, 01:31:36 pm »
Have you referred to the (flipping? :-) manual?

https://www.freepascal.org/docs-html/current/ref/refse83.html#x145-16700012.4

However you might also need to be aware of the ptrint and ptruint types, which by definition are defined to have the same size as a pointer on the target platform.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14363
  • Sensorship about opinions does not belong here.
Re: How to typecast a function result
« Reply #2 on: February 24, 2021, 02:03:23 pm »
You need to dereference the pointer. (which is the function result)
Let's assume p:
p^ which is the same as * p in C like languages.
« Last Edit: February 24, 2021, 02:07:37 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: How to typecast a function result
« Reply #3 on: February 24, 2021, 06:13:14 pm »
I'm really sorry but this still makes no sense to me.

Given the following example :

Code: Pascal  [Select][+][-]
  1. var
  2.   EvdDescript : Int64;
  3. ...
  4. EvdDescript := SomeAPIFunction(hDevice, 10, nil);
  5.  

how does one dereference that? hDevice is the handle to the device, 10 is the property control type I want (which means "give me the text data") and nil is the nil buffer which is not required when passing 10. EvdDescript is the Int64 return value. I do not understand, at all, how the apparant LPWSTR gets added to the mix here and I access it?

balazsszekely

  • Guest
Re: How to typecast a function result
« Reply #4 on: February 24, 2021, 07:01:11 pm »
@Gizmo
Quote
One of the functions seems to magically (in my view) give a different TYPE of result depending on what paramater the programmer sends to the function.
It's similar to the overload modifier in pascal, no mystery here in my opinion.

Quote
In my use case, the result is not passed to a buffer, so the information about the object that I see is passed through the return value. The return value I need is of type LPWSTR. But the function returns INT64. It seems to suggest that somehow I typecast the result "to correctly interpret/understand it", but I have zero clue how to typecast an INT64 to LPWSTR when the result type needed for successful compilation is INT64.

Can anyone steer me here? I have read several of the typecast wiki articles but I'm sorry to say they are not helping me understand this issue.
LPWSTR pascal equivalent is PWideChar/PChar. Similar to the typecast in SendMessage/PostMessage apis, where you can typcast a string to an lparam and back, you can try something like this:
Code: Pascal  [Select][+][-]
  1. var
  2.   EvdDescript : Int64;
  3.   ApiResult: String;    
  4. begin
  5.   EvdDescript := SomeAPIFunction(hDevice, 10, nil);
  6.   if EvdDescript > 0 then
  7.     ApiResult := String(PWideChar(EvDescript)); //or PChar(EvDescript)
  8. end;

PS: Lparam is PtrInt on 32 bit and Int64 on 64 bit.
« Last Edit: February 24, 2021, 08:22:33 pm by GetMem »

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: How to typecast a function result
« Reply #5 on: February 24, 2021, 08:31:05 pm »
You've saved me once again! Thanks so much - this worked straight away.

Now I need to run off and try to actually understand what's going on and how\why that has worked. It's a new concept to me I am afraid.

balazsszekely

  • Guest
Re: [SOLVED] How to typecast a function result
« Reply #6 on: February 24, 2021, 09:17:26 pm »
@Gizmo
Quote
Now I need to run off and try to actually understand what's going on and how\why that has worked. It's a new concept to me I am afraid.
The result returned by the api(int64) points to the address of another variable, in our case a LPWSTR. With the PWideChar(EvDescript) typecast, you're telling the compiler that the variable is a WideSring. The second typecast converts the WideString to string. With FPC 3.0.0+ you can probably skip the last step, the conversion should be done automatically.

 

TinyPortal © 2005-2018