Jump to content

Remy Lebeau

Members
  • Content Count

    3000
  • Joined

  • Last visited

  • Days Won

    135

Posts posted by Remy Lebeau


  1. 2 hours ago, Rick Malik said:

    Stack Overflow [closed] my question because they thought I was looking for books, software, or tutorials....

    Because, that is essentially what you did ask for - a tutorial on how to use the OCX control.

    2 hours ago, Rick Malik said:

    I just wanna know what to do with BadWordFound so I can replace the stupid thing in my TMemo when it finds it.

    THAT is what you should have asked for instead.  That would have made for a better StackOverflow question, and less likely to be closed. Also, since you clearly know what you are looking for, you should have explained/shown what you had already tried that didn't work for you.

    Quote

    There's plenty of VB and pascal examples, but I only speak c/c++. 

    Then you should have included one of those examples in your StackOverflow question and asked how to accomplish the same thing in C++.  I can't find any documentation or examples of the OCX control, in ANY programing language.  Can you provide a source?


  2. 24 minutes ago, Rick Malik said:

    But aren't there 11 nums? 0 thru 10 = 11

    No. There are only 10 integers declared in the array (values 1..10). Valid indexes into the array are only 0..9, inclusive.


  3. 4 hours ago, kokoslolos said:

    And now as I'm writing this post, it just hit me that in my approach, if the FormName (custom property) is changed at design time, this cannot update my action's custom properties 😄 ! Need a bit of rethinking there.

    I would suggest making the Action hold a pointer to the desired Form, and simply read the Form's current Name whenever needed.  And if the Form's Name changes, then have the Form notify any Action that is pointing at it so they can use the new Name as needed. Easy to do if Actions register themselves with the Form so it can maintain a list of pointers to the Actions.


  4. 2 hours ago, alogrep said:

    The first error is ignored, no warning, no message.

    That code is performing floating-point division. And as Lejos mentioned, floating-point exceptions behave differently in Delphi 12 than they did in earlier versions.

     

    https://dalijap.blogspot.com/2023/09/coming-in-delphi-12-disabled-floating.html


    This is also documented by Embarcadero:
     

    https://docwiki.embarcadero.com/RADStudio/Athens/en/What's_New#Disabling_Floating-Point_Exceptions_on_All_Platforms

     

    Quote

    The second error is caught, but it does not show the call stack

    If you want to see a Call Stack at runtime, you have to display it yourself. You can catch the Exception (or use the TApplication(Events).OnException event) and then read the Exception.StackTrace property. However, this requires you to install a 3rd party exception logging library, such as MadExcept or EurekaLog, to populate the trace details. Embarcadero does not provide that functionality. In which case, you may as well use that library's own logging capabilities.


  5. Are you using C++ or Delphi? Integer division or floating-point division? Can you provide an example that is not working for you? 


  6. 1 hour ago, bobD said:

    Because a TFDConnection connects very quickly, but takes a long time to fail, and it's annoying to sit and wait <g>. Gethostbyname fails a lot quicker.

    Even if you could lookup the database server's IP quickly, there is no guarantee that the database engine is actually running/responding, so you still have to wait for the full connection before you can do anything with the database, and still handle any failure that may occur.  So, may as well just do that by itself, and deal with the slowness of reporting a failure (you should probably file a ticket about that).


  7. 2 hours ago, bobD said:

    Program is a client/server design can that use either a tcp-ip connection to my home network database, or a local connection to an onboard copy. It should default to network if at home, and default to local if I'm on the road. It can also do things like demand additional authorization, etc.

    Without having explicit knowledge of your home network setup, there is simply no way for your app to detect whether you are connected to your home network or not.  You could try looking for a connected LAN adapter, but what if you are in a hotel that offers LAN access? You could try looking for a connected Wifi adapter, but most public networks are Wifi.  You would need your Wifi's SSID to differentiate between a home Wifi and a public Wifi.

     

    I think you are making the issue harder then it needs to be.  I would suggest taking out the guesswork completely and simply just provide two options in your program's configuration:

     

    - connect to a tcp hostname

     

    - local copy only

     

    And then make it easy to switch between the two configurations as needed.  Perhaps wrap them in profiles and then switch the active profile when you leave home and then come home.

     

    Another option would be to just skip the local copy altogether and just always connect to your home database server even when you're on the road.  Setup port forwarding on your home router and then register a static DynDNS hostname so you can easily find your router's WAN ip/port from public networks (and for added security, setup a VPN server on your home network and then connect to the VPN when on a public network).  This way, you always have access to your database server and can connect to it via its IP or hostname on your private LAN.

    2 hours ago, bobD said:

    Here's what I've come up with so far:  (actually, modified from code I found that you posted some 17 years ago)

    If you really want to go down this route, then you need to assign a unique static hostname to your database server, and then you can use gethostbyname() (or better, getaddrinfo()) to lookup that hostname whenever you connect to the database.  No need to validate the IP prefix.  If you connect to the database by hostname instead of by IP, then you don't even need to perform the lookup yourself, as most TCP libraries/database drivers will handle this task for you.  But either way, if the hostname fails to resolve, then you are likely not connected to your home network.  Done.


  8. 3 hours ago, bobD said:

    Basically, program needs to act differently if at home or away--so how do I know?

    Ask the user. The OS only knows whether there is a network connection or not. It doesn't care where the network is located or what it's connected to. That's up to the hardware to deal with.

    Quote

    Is there another way to get the name of the local network LAN that works both wired (desktop machines) and wifi?

    No. And as far as sockets are concerned, there is no difference whatsoever whether you are connected to a wired LAN, or to a Wifi, or a cellular provider, etc.  The socket API works the same way. The difference is in how the hardware routes the traffic.

    Quote

    I'd like to try using winsock's gethostbyname but I haven't found a good use example.

    Likely because this is not what that API is intended for.

    Quote

    Or is there a better way?

    What EXACTLY are you trying to accomplish in the first place?


  9. Running a process as an (impersonated) admin user, and running a process in an elevated state, are two different things.  Being an admin user does not imply automatic elevation, but an elevation prompt does require an admin user.

     

    In any case, perhaps have a look at the CreateProcessWithLogonElevatedW() and CreateProcessWithTokenElevatedW() functions provided in the Elevate DLL of this old CodeProject article: Vista UAC: The Definitive Guide (I think the site is down undergoing a redesign at the moment, though. Maybe you can find another copy of the DLL elsewhere).

     


  10. 2 hours ago, davornik said:

    I have tried to catch OnPaint message, something like this, but no success 😞

    You are catching the FORM'S paint event, not the BUTTON'S paint event.  Every window receives its own painting messages (WM_PAINT, WM_DRAWITEM, etc).

    1 hour ago, davornik said:

    Finally, made it work 🙂

    Your code can be simplified a little. If you use the button's WindowProc property, you won't need to call GetWindowLongPtr() directly (and even then, SetWindowSubclass() would have been a better choice). Also, since your DrawColoredTxt() function is completely erasing the button and drawing it yourself, there is no point in calling the default paint handler at all.

     

    Try this:

    ...
        procedure DrawColoredTxt(aBtn: TButton; aCaption: string);
      private
        FOriginalButtonProc: TWndMethod;
        procedure ButtonWndProc(var Message: TMessage);
    ...
    
    procedure TForm1.DrawColoredTxt(aBtn: TButton; aCaption: string);
    begin
      ...
    end;
    
    procedure TForm1.ButtonWndProc(var Message: TMessage);
    var
      PS: TPaintStruct;
    begin
      case Message.Msg of
        WM_PAINT: begin
          BeginPaint(Button2.Handle, PS);
          try
            FOriginalButtonProc(Message);
            DrawColoredTxt(Button2, 'Admin');
          finally
            EndPaint(Button2.Handle, PS);
          end;
        end;
      // Forward all other messages to original handler
      else
        FOriginalButtonProc(Message);
      end;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      FOriginalButtonProc := Button2.WindowProc;
      Button2.WindowProc := ButtonWndProc;
      Button2.Repaint;
    end;
    
    procedure TForm1.Button3Click(Sender: TObject);
    begin
      if Assigned(FOriginalButtonProc) then
      begin
        Button2.WindowProc := FOriginalButtonProc;
        FOriginalButtonProc := nil;
      end;
      Button2.Caption := 'Admin';
      Button2.Repaint;
    end;

    But, that being said, since you are drawing the entire button anyway, you may as well just use the BS_OWNERDRAW style and handle the WM_DRAWITEM message, as explained earlier in this discussion thread.

    • Like 1

  11. 4 hours ago, davornik said:

    I have tried to do drawtext over text on TButton and it works partially.

    ...

    I get changed font color like this:

    ...

    But when I move the mouse into Button2 everything falls apart 😞

    That is because you are drawing outside of a painting event.  As soon as the button gets repainted for any reason, your drawing is lost.  This is why you must owner-draw the button so that any custom drawing can be persisted across multiple paint events.


  12. 1 hour ago, Mark- said:

    I was using the OnError callback. Call connect, onError is called but, "LastError" was zero.

    Have you tried calling WSAGetLastError() directly?  At the point where OnError is called, the last socket error code might not have been overwritten yet.

    1 hour ago, Mark- said:

    Switched to catching it as an exception try Connect except end; LastError is still zero.

    The error code is stored in the raised ESocketException in its ErrorCode property.

    1 hour ago, Mark- said:

    Looked in the source at

    
    procedure TCustomWSocket.BindSocket;
    ...
    if WSocket_Synchronized_bind(HSocket, PSockAddrIn(@LocalSockName)^, SockNamelen) <> 0 then begin
    

    and the correct error code 10049 (WSAEADDRNOTAVAIL) is present but, it is never assigned to LastError, that I could see.

    Sounds like a bug that should be reported to the ICS author.

    • Like 1

  13. 3 hours ago, JohnLM said:

    I will figure out a way to fix this the code (above) to handle a source file copied to the clipboard to process when copying whole source codes from the Delphi IDE that could potentially be hundreds of lines.

    First off, you don't need your fix13and10str() function, as the RTL has its own AdjustLineBreaks() function:

    Quote

    AdjustLineBreaks adjusts all line breaks in the given string to the indicated style.

     

    When Style is tlbsCRLF, the function changes all CR characters not followed by LF and all LF characters not preceded by CR into CR/LF pairs. When Style is tlbsLF, the function changes all CR/LF pairs and CR characters not followed by LF to LF characters.

    For example:

    uses
      ..., System.SysUtils;
      
    procedure TForm1.btnProcessClick(Sender: TObject);
    var
      s: string;
    begin
      s := AdjustLineBreaks(Clipboard.AsText, tlbsCRLF);
      // or, just let it use the platform default style...
      // s := AdjustLineBreaks(Clipboard.AsText);
      m1.Lines.Add(s);
    end;

    Alternatively, you can assign the clipboard text as-is to a TStrings.Text property and let it parse the line breaks for you:

     

    https://docwiki.embarcadero.com/Libraries/en/System.Classes.TStrings.Text

    Quote

    When setting Text, the value will be parsed and separated into substrings whenever the LineBreak value is encountered. For backward compatibility, on POSIX, if the LineBreak separator is LF, then LF, CR, or CRLF are treated as separators. On Windows if the LineBreak separator is CRLF, then LF, CR, or CRLF are treated as separators.

    For example:

    procedure TForm1.btnProcessClick(Sender: TObject);
    var
      sl: TStringList;
    begin
      sl := TStringList.Create;
      try
        // sl.LineBreak is set to System.sLineBreak by default...
        // sl.LineBreak := sLineBreak;
        sl.Text := Clipboard.AsText;
        m1.Lines.AddStrings(sl);
      finally
        sl.Free;
      end;
    end;

    Or simpler (if you don't mind the entire TMemo content being replaced):

    procedure TForm1.btnProcessClick(Sender: TObject);
    begin
      m1.Lines.Text := Clipboard.AsText;
    end;
    3 hours ago, JohnLM said:

    For the record, I do not use notepad++ and I am not saying it is bad or anything.  I just never found uses for it up to this point.

    One handy use-case is changing line breaks in text.  You can copy the code from the IDE, paste it into Notepad++ (or just open the original file directly in Notepad++), specify a new type of line feed (bare-CR, bare-LF, and CRLF are supported), copy the new text back to the clipboard, and paste it into your app.

    • Thanks 1

  14. 22 minutes ago, GabrielMoraru said:

    How do you deal with Android? It does not support blocking messages...

    When displaying a popup message, you have to use the non-modal version, and break up your code logic so it can be continued by the popup's asynchronous callback when the popup is closed.

    • Like 1

  15. 10 hours ago, Keesver said:

    As a last resort you can define your own TDataset type with a public 'FDatasource' member and then cast the TDataset to this type. Design wise this is a clear 'hack' but it will do the job

    That approach is dependent on the layout of the original class, and could break between updates, so use with caution.

     

    It is also not a last resort, either.  A safer approach would be to use RTTI to get the offset of the desired class member, and then use pointer arithmetic to access the member, eg:

    uses
      ..., System.Rtti;
    
    private
      DataSourcesOffset: Integer;
    
    ...
     
    procedure TForm1.Form1Create(Sender: TObject);
    var
      Ctx: TRttiContext;
    begin
      DataSourcesOffset := Ctx.GetType(TClientDataSet).GetField('FDataSources').Offset;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    type
      PDataSourceList = ^TList<TDataSource>;
    begin
      var ds := PDataSourceList(PByte(ClientDataSet1) + DataSourcesOffset)^;
      for var src in ds do
        ShowMessage(src.Name);
    end;

     


  16. 17 minutes ago, Anders Melander said:

    In short it involves declaring a placeholder property on the top level class registering a dedicated property editor for this property which in turn redirects to the actual sub-property and then enumerates the delegates/events of that object to create TNestedProperty property editors for each of them.

    That sounds like a lot of extra work for very little gain.  I don't think it is very good design to expose design-time access to sub-property events.  The sub-properties are part of the main component.  All events should be part of the main component, as well.  But that is just my opinion.


  17. 1 hour ago, Rolf Fankhauser said:

    I run it several times and got different results for the  invalid indexes. Index 7 always returned 14880996, the other results seem to be random :

    That's like the very definition of Undefined Behavior.  Accessing the array out of bounds is reaching into surrounding memory, and the content of that memory is random/indeterminate on each new run.  It could be random bytes left over from a previous run/process.  It could be bytes belonging to other local variables on the stack.  You just don't know.

     

    myArray[2] = 7
    myArray[4] = 9
    myArray[7] = out of bounds, UB!

    myArray[8] = out of bounds, UB!
    myArray[9] = out of bounds, UB!

×