Jump to content

aehimself

Members
  • Content Count

    1030
  • Joined

  • Last visited

  • Days Won

    22

Posts posted by aehimself


  1. On 7/23/2020 at 3:47 AM, Donald Shimoda said:

    I want to detect the windows active app using a tray app. Is posible?

    GetTopWindow or GetActiveWindow, depending on your needs

     

    On 7/23/2020 at 3:47 AM, Donald Shimoda said:

    And how to detect if theres keyboard or mouse activity?

    function SecondsIdle: DWord;
    var
     liInfo: TLastInputInfo;
    begin
     liInfo.cbSize := SizeOf(TLastInputInfo) ;
     GetLastInputInfo(liInfo) ;
     Result := (GetTickCount - liInfo.dwTime) DIV 1000;
    end;

     

    • Like 1

  2. 2 minutes ago, Attila Kovacs said:

    Turn this to an advantage. It will pay off.

    Modularity is the key. And the beauty of inheritance makes it really-really easy.

    Since I started to chip my code to as small blocks as possible, separate classes of course; I realized that I started to move more and more of these one-class units to my Framework folder, out of an applications folder.

    And they simply all work together.

    • Like 1

  3. While I have no experience with C++ builder, nor FMX, but if the "ScrollBox" component is available, you can use that. Place one on your form, set the Align to alClient and put your components on the scrollbox instead of the form.


  4. When I made a parser like this I was working with indexes returned by String Helpers. Look for the first opener, look for the first closer from the opener. Make sure there are no quotes (or even number of quotes) in between. Make sure there are equal amount of openers and equal amount of closers in between.

    I'm not saying it was the fastest solution, but it worked 🙂 And it handled nested sections correctly, too.


  5. Yep, you have so much freedom you can easily do

    Var
     tb: TBytes;
     a: Integer;
    Begin
     SetLength(tb, 5);
     For a := 0 To 5 Do
      tb[a] := 1;
    End;

    or...

    Var
     pc: PChar;
    Begin
     GetMem(pc, 5 + 1);
     StrPCopy(pc, 'abcde', 5);
    End;

    or even:

    Var
     obj: TObject;
     proc: TProcedure;
    Begin
     proc := addr(obj);
     proc;
    End;

    Just because you have the possibility of doing something it doesn't mean you are supposed to or should.

     

    Btw, I thought this forum is a politic-free area. Please take those ideologies to Reddit.

    • Like 1

  6. 1 minute ago, PeterPanettone said:

    That's what I call "user-centric" programming. :classic_smile:

    In a more advanced project (especially after a UI change or refactoring) you'll quickly realize why separating the UI and data storage/handling extremely important.

    In your example, let's say a user doesn't like CheckListBox and wants you to change it to something else. Apart from the visual changes, you'll have to re-code your business logic as well.

     

    It is not that important in "personal use" applications, but lately I have a separate class for data storage everywhere. The UI only displays, validation, (de)serialization, everything is handled by the class itself.

    • Like 2

  7. I don't feel fine storing information in the UI. I'd rather create a separate TList<TMyValue> and add the values to it as I'm adding the checkboxes. This way the index of the CheckBox will be equal to the index of it's value in the TList.

     

    • Like 2

  8. 21 hours ago, PeterPanettone said:

    However, your employer has to pay for the time you need to write overloaded code. (Though I assume you're a fast writer).

    These overloaded methods are like 4 lines. I really would like to have an employer who pays me double for 4 extra lines per feature request.

    In a real world scenario, overloaded methods usually point from one to the other (or call the "real" one with proper adjustments) so we can argue that this was for demonstration only, but in real life - multiple overloaded methods will not consume even 10 minutes of your time if done smartly.


  9. 2 hours ago, David Heffernan said:

    Nothing can save you at this point.

    Agreed. A destructor might be called even when the object creation is incomplete... so it must have proper "defenses" in place.

    Let's just think on the standard way of local object handling...

    myobject := TMyObject.Create;
    Try
     myobject.DoStuff;
    Finally
     myobject.Free;
    End;

    What happens if an exception is raised in the destructor? Uncaught exceptions and 99% of the times memory leaks.

    4 hours ago, Kas Ob. said:

    i am here talking about an imaginary raised exception in the destructor (which is wrong, yet it might happen)

    No, it might not. If it does, it's a bug. If it's an external component, report it to the developer. If it is yours, fix it.

    3 hours ago, Mahdi Safsafi said:

    As you can see, this class is freeing its instance before freeing its internal data.

    I might be fresh in the area, but what is the purpose of this? I would simply call it a bad code as up until now, I never really had the need to put inherited anywhere else than the very last line of a custom object destructor.


  10. 2 hours ago, dummzeuch said:

    You mean it's a bad thing that, instead of fixing the bug in 4-5 applications and compile them, you now have to fix the bug only once and still compile 4-5 applications?

    No; that was not my intention to mean that. Obviously fixing at one place is more convenient and time effective, no one doubts that. All I wanted to say was that I don't really have a dependency (or rather - uses) list, which clearly says which application is using the particular unit I am working in.

    I think I'll write a recursive crawler to discover any custom units in the uses clauses of all my applications. As a result, I can enter a unit name and see which applications I have to rebuild. An other way is to distribute each unit as a separate library and my auto updater will take care of the updates of said files. This, however, increases the number of files my application is using and the attack surface of course. Even the beginner Hacker Henry will know to check the exports of MyEncryptor.dll and launch a brute force attack on encrypted data.

    And however I don't deal with credit card / bank account numbers or personal data; I like to prepare for the worst.

     

    Anyhow, we are sliding off the original topic. This is just one thing to consider with the cherished "Write once, use anywhere" method - which is THE WAY to go.


  11. On 7/11/2020 at 8:54 AM, Rollo62 said:

    A clever design concept is a good pay-off.

    I started to see this at my biggest project. I broke it in the smallest pieces possible (random number generator, pseudorandom generator, encrypter, (de)serializer, hell I have a separate class for the application itself). Whenever I start a new project, I realize that most of these modules are now in my "Common" folder. It just started to become logical not to write a code again (or copy-and-paste) if I already wrote it once.

    The only bad thing in this is that if I make a bugfix in one of the units I have to recompile 4-5 applications now instead of one 🙂


  12. For not that important applications I'm using MessageDlg, but for more advanced ones I always use my own "message dialog". Fully customizable, with a short (and if needed) an extended version of the message which is visible if the user clicks the down arrow. It uses the system's icons, but follows themeing correctly. I included a class function "Confirm" which returns a boolean and displays the dialog in "confirm" mode (question icon, yes and no buttons visible) and a .Error class procedure, which takes an exception and dumps as much information as possible in the extended text. I'm also planning to add a "Report" button to errors, which would send me an e-mail with the information collected; I was just too lazy to to do so until now (and I did not look for a component. Worst case, I'll implement my own lightweight SMTP client, idk yet).

    Based on this logic I also created a multi-input form, where my application can request multiple inputs validated (number only, can be null, predefined values from a combobox, multi-line text, etc.

    The problem is, this input form became so versatile and useful, most of my applications are heavily depending on it now.

     

    So yeah, I completely support @haentschman in this. Just write your own, and you will have a truly cross-platform solution satisfying all your needs.


  13. 5 hours ago, Lars Fosdal said:

    There are numerous Unicode white space chars of varying width, so adjusting the distance "by character" is possible.

    In my opinion, just because there's a possibility to do something doesn't mean you should use it 🙂

     

    2 hours ago, Rollo62 said:

    Don't mess with youself too much,
    if Microsoft shows not any attempt to make these controls practically more usable since the last 20+ years, why should we do ?

     

    2 hours ago, Sherlock said:

    And in the end, what is the effort worth? Just to say: "In my product 1+1 is still 2 but it looks better than the others".

     

    I do understand and agree with you on this. But it's not for the others, it's because of my own self.

    I just don't want to produce code I don't like or as "time-resistant" as I can think of. Whilst it's a good thing it's a curse as well... I can easily waste hours on a single "Hello World" application this way.

    • Like 1

  14. No insult taken. My attention may have slipped - I did not see the static part and for that I apologize.

    For the record, my intention was not to argue: I just had to refactor too many lines of code (written 10-15 years ago) which broke or misbehaved on specific modern systems. The days-weeks wasted there taught me to proudly waste hours when I write fresh code which will still work the same way everywhere in the foreseeable future.

     

    Do I still have "hacks" like this in production? Hell yes, I do. Do I hate myself for it when I see it? That's a question for a different topic 🙂

    • Like 2

  15. @rcaspers I had no idea you can "override" default TField types. Would have made my life a lot easier a couple of years ago!

     

    @Max Terentiev You can easily do the conversion by TEncoding.Convert, GetBytes and GetString. However, the easiest solution would be to upgrade the component you are using to one, which actually supports Unicode. If they are all TDataSet descendants, you should face no issues.

    If upgrading is not an option, I'd create a TFieldHelper with an .AsUnicodeString property.

    Getter would read the fields value as a byte array, and return it as a String with TEnoding.Unicode.GetString(fieldbytearray);

    Setter would accept a string, convert it to a byte array with TEncoding.Unicode.GetBytes and strore this instead of the string.


  16. 10 hours ago, Remy Lebeau said:

    GetMem() operates on bytes, but s.Length is expressed in characters, not bytes, and SizeOf(Char) is 2 bytes in D2009+, so you need to double the allocation, otherwise you will have a buffer overflow on StrPCopy(), corrupting surrounding memory, which can lead to all kinds of problems, including leaks of other things that were allocated dynamically, if you corrupt the pointers to them:

    That was exactly my purpose 🙂 Don't worry, this is only for testing and satisfying my curiosity.

    10 hours ago, Remy Lebeau said:

    It is likely that one of the pointers you are corrupting may be the one used by 'pc' or 's' itself!

    I guess this will be the cause of the "leak". Today, I attempted to check the addresses of pc and s, and I can not seem to trigger a memory leak, nor access violation even with StrPCopy(pc, s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s + s);

    maybe today I have more free continuous unused memory than the last time...

    It makes sense, though. If by accident I managed to corrupt the pointer itself, the release will not happen at the right place (or maybe not happen at all?). I just cannot seem to find what the 3rd leaked allocation might be.

     

    Anyway, memory corruption is no job for an allocation detection tool. I just found it strange that sometimes even these were triggered.


  17. We are getting close to the next release so time is not that much to spare, but I managed to run my test. DeLeaker shows about the same results with 1, 2 and 5 copies of "s".

    I am no expert with memory leak detection in Delphi, but can someone explain this to me?

     

    On 6/25/2020 at 10:18 PM, aehimself said:

     

    
    Var
     pc: PChar;
     s: String;
    begin
     s := 'Hello, world!';
     GetMem(pc, s.Length + 1);
     Try
      StrPCopy(pc, s);
     Finally
      FreeMem(pc);
     End;
    end;

     

    No leaks, however (on a Unicode Delphi) this used up twice as much space as it was reserved. Fine, it's not a leak (as it is properly released), it's corruption. But why StrPCopy(pc, s + s); already pops up as leak in this case?

×