Search code examples
delphihandlenegative-number

Delphi Handle in negative


I am working on a project in delphi XE5. Different Popup menus are created at run time using same function. Names are given each time to pop up using "Handle" from Tcomponent class.

popupname := 'XYZ' + IntToStr(handle);

On some system I get "Handle" value as negative, when I try to give name with "-" sign to a component i get error message "XYZ-5645 not a valid component name"

Can you please suggest me a way out?


Solution

  • Instead of IntToStr, which accepts a signed integer, you can treat the handle as a pointer, and so represent the numeric value as hex:

    popupname := Format('XYZ%p', [Pointer(Handle)]);
    

    This makes sense because a handle in Windows is, as defined in the header files, an untyped pointer, void*.

    As an added benefit your code will now be correct on both 32 and 64 bit platforms.

    Thinking outside the box, perhaps the component does not need a name at all. If so, remove this code, leave it unmanned and thus side-step your problem.