Re: [fpc-pascal] GetSaveFileNameA limited to 100 characters in default name
>MAX_PATH itself seems to be an alias from system unit where it is defined accordingly to the operating system (260 on windows, 4096 on Linux, 1024 on BSD, Solaris and Darwin). >It's unlikely, but this way you can end up with the corruption of the byte right after the memory block DefaultFileName is pointing to . This got me thinking. my default filename is just a recommended default. but TFileName.lpstrFile is also what returns the filename to use, and the user could use the save-as dialog to add a directory, and change the name of the file to something else..etc. so the file name could be significantly larger than the default. So I think I'm best off setting TFileName.nMaxFile := Max_Path; then TFileName.lpstrFile will always be able to hold anything the operating system can support. James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] GetSaveFileNameA limited to 100 characters in default name
I haven't seen your reply. Le 18/06/2021 à 23:50, James Richters via fpc-pascal a écrit : So now I have: TFileName.nMaxFile:= Length(DefaultFileName)+1; TFileName.lpstrFile:=Pchar(DefaultFileName); I need the +1 for the #0 at the end of the Pchar, and now it works fine, and I can have strings as long as they need to be. I think that this way you tell GetSaveFileNameA that it can write Length(DefaultFileName)+1 chars in Pchar(DefaultFileName), but you have only allocated Length(DefaultFileName) chars to DefaultFileName . It's unlikely, but this way you can end up with the corruption of the byte right after the memory block DefaultFileName is pointing to ... ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] GetSaveFileNameA limited to 100 characters in default name
I'm not familiar with GetSaveFileNameA, but usually for the size of a FileName buffer I use the constant MAX_PATH which is defined in unit SysUtils. MAX_PATH itself seems to be an alias from system unit where it is defined accordingly to the operating system (260 on windows, 4096 on Linux, 1024 on BSD, Solaris and Darwin). Did you have a look at Microsoft's documentation for GetSaveFileNameA and the OPENFILENAMEA structure it takes as parameter ? https://docs.microsoft.com/en-us/windows/win32/api/commdlg/nf-commdlg-getsavefilenamea https://docs.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-openfilenamea Particularly, you need to put in |nMaxFile |the size of the buffer pointed to by lpstrFile. The folowing code works with a 120 characters long filename : program test_GetSaveFileName; uses SysUtils, CommDlg; const DefaultFilename= 'Test'; var Buffer: array[0..MAX_PATH] of Char; //size MAX_PATH + 1 for terminating null char ofn: TOPENFILENAMEA; begin StrPLCopy( Buffer, DefaultFilename, MAX_PATH); ofn.lStructSize:= sizeof(ofn); ofn.hInstance:= 0; ofn.lpstrFilter:= nil; ofn.lpstrCustomFilter:= nil; ofn.nMaxCustFilter:= 0; ofn.nFilterIndex:= 0; ofn.lpstrFile:= Buffer; ofn.nMaxFile:= Sizeof( Buffer); ofn.lpstrFileTitle:= nil; ofn.nMaxFileTitle:= 0; ofn.lpstrInitialDir:= nil; ofn.lpstrTitle:= 'test GetSaveFileNameA'; ofn.Flags:= 0; ofn.nFileOffset:= 0; ofn.nFileExtension:= 0; ofn.lpstrDefExt:= nil; ofn.lCustData:= 0; ofn.lpfnHook:= nil; ofn.lpTemplateName:= nil; ofn.pvReserved:= nil; ofn.dwReserved:= 0; ofn.FlagsEx:= 0; if GetSaveFileNameA( @ofn) then WriteLn( ofn.lpstrFile); end. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] GetSaveFileNameA limited to 100 characters in default name
I'm converting my AnsiString to a Pchar with TFileName.lpstrFile:=Pchar(DefaultFileName); But I didn't understand the meaning of TFileName.nMaxFile:=100; I thought it was the maximum number of files to display or something.. not the maximum number of characters in the file name. which it appears to be. So now I have: TFileName.nMaxFile:= Length(DefaultFileName)+1; TFileName.lpstrFile:=Pchar(DefaultFileName); I need the +1 for the #0 at the end of the Pchar, and now it works fine, and I can have strings as long as they need to be. Thank you for the help! James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] GetSaveFileNameA limited to 100 characters in default name
The Windows API doesn't understand what an 'AnsiString' is and cannot resize it dynamically like FreePascal would do as it is used. You need to use something like buf: array[0..512] of char; TFileName.nMaxFile := sizeof(buf); But then you should also handle the possibility that the dir is greater than 512 and allocate a bigger buffer as needed. So maybe dynamically allocating the space would be better. -- Alexander Grotewohl https://dcclost.com From: fpc-pascal on behalf of James Richters via fpc-pascal Sent: Friday, June 18, 2021 1:56 PM To: 'FPC-Pascal users discussions' Cc: James Richters Subject: [fpc-pascal] GetSaveFileNameA limited to 100 characters in default name I’m using GetSaveFileNameA() in a windows console program to obtain a save-as directory and file name, I am passing sending it a default file name with: TFileName.lpstrFile:=Pchar(DefaultFileName); DefaultFileName is an AnsiString that contains a full path and filename. This all works fine as long as DefaultFileName is 100 characters or less, if it’s 101 or more, then GetSaveFileNameA() never opens a dialog box and just returns False; Does anyone know where the 100 character limit is coming from? TFileName.lpstrFile is a PChar, and DefaultFileName is an AnsiString, Neither of which have a length limit that I know of… I am doing a Writeln if Both DefaultFileName and TFileName.lpstrFile and they both match, nothing is being truncated. James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
[fpc-pascal] GetSaveFileNameA limited to 100 characters in default name
I'm using GetSaveFileNameA() in a windows console program to obtain a save-as directory and file name, I am passing sending it a default file name with: TFileName.lpstrFile:=Pchar(DefaultFileName); DefaultFileName is an AnsiString that contains a full path and filename. This all works fine as long as DefaultFileName is 100 characters or less, if it's 101 or more, then GetSaveFileNameA() never opens a dialog box and just returns False; Does anyone know where the 100 character limit is coming from? TFileName.lpstrFile is a PChar, and DefaultFileName is an AnsiString, Neither of which have a length limit that I know of. I am doing a Writeln if Both DefaultFileName and TFileName.lpstrFile and they both match, nothing is being truncated. James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Will the size of an executable depend on the uses clause
Ooops... little overflow bug My computer has reached 33 days of uptime today and with var t, d, h, m, s: Integer; you switch to the negative side ... I got a display like : -8:-4:-52 Replacing with QWord solved the problem (I imagine DWord would just allow for something like 66 days) var t, d, h, m, s: QWord; I found this bug because incidentally I had the idea to have a look at the Ubuntu version of uptime which is installed on my system with Windows Subsystem for Linux. This uptime doesn't match the value of GetTickCount64, you just get the uptime since you clicked on the Ubuntu icon in Windows ... Here is the modified code : program uptime; function GetTickCount64: QWord; stdcall; external 'kernel32.dll'; function _2d( _i: Integer): String; begin Str( _i, _2d); if Length(_2d) < 2 then _2d:= '0'+_2d; end; function FormatUpTime( _tc: QWord): String; var t, d, h, m, s: QWord; sd: String; begin t:= _tc div 1000; s:= t mod 60; m:= (t div 60) mod 60; h:= (t div 3600) mod 24; d:= (t div 86400); FormatUpTime:= _2d(h)+':'+_2d( m)+':'+_2d( s); if 0 = d then exit; Str( d, sd); FormatUpTime:= sd+' '+FormatUpTime; end; begin WriteLn( FormatUpTime( GetTickCount64)); end. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to check if a network is available?
Le 18/06/2021 à 17:07, Bo Berglund via fpc-pascal a écrit : I will need to check this in my connector class, the address to look for has to be a config item in my application so it can be modified if need be without rebuilding the app. Have to figure out how to: - Retrieve the output of the TProcess execution There are many ways to cook TProcess ... I think you'll find what you need at : https://wiki.freepascal.org/Executing_External_Programs - Parse the output for the expected network address. My idea would be to store the output in a Stringlist as shown in the examples, then with IndexOf you can locate the position of lines like "IPv4 Route Table" and "Persistent Routes:" and make a more detailed parsing between the two. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to check if a network is available?
On Fri, 18 Jun 2021 16:39:52 +0200, Jean SUZINEAU via fpc-pascal wrote: >I've never used open vpn, may be the 10.117 is defined in your >configuration file ? > >For the vpns I've used, the ip adressed were fixed. The tunnel IP address is defined server side and the route is pushed to the client on connection. A common VPN address in all tutorials starts with 10.8.0 but this is too common so I have set ours as 10.117.xx > >( for route, you can type too something like "route print 10.117.*" ) > C:\>route print 10.117.* === Interface List 9...c4 65 16 9d 7e 8e ..Intel(R) Ethernet Connection (7) I219-LM 16...98 fc 84 ed a3 ea ..Realtek USB GbE Family Controller 7...00 ff 2d dd 8d be ..TAP-Windows Adapter V9 21...00 ff 94 a3 1f d4 ..TAP-Windows Adapter V9 #2 14...d0 c6 37 ea 58 3f ..Intel(R) Wireless-AC 9560 160MHz 26...d0 c6 37 ea 58 40 ..Microsoft Wi-Fi Direct Virtual Adapter #3 11...d2 c6 37 ea 58 3f ..Microsoft Wi-Fi Direct Virtual Adapter #4 10...00 50 56 c0 00 01 ..VMware Virtual Ethernet Adapter for VMnet1 13...00 50 56 c0 00 08 ..VMware Virtual Ethernet Adapter for VMnet8 15...d0 c6 37 ea 58 43 ..Bluetooth Device (Personal Area Network) 1...Software Loopback Interface 1 === IPv4 Route Table === Active Routes: Network DestinationNetmask Gateway Interface Metric 10.117.3.0255.255.255.0 On-link10.117.3.2281 10.117.3.2 255.255.255.255 On-link10.117.3.2281 10.117.3.255 255.255.255.255 On-link10.117.3.2281 === Persistent Routes: None IPv6 Route Table === Active Routes: None Persistent Routes: None I will need to check this in my connector class, the address to look for has to be a config item in my application so it can be modified if need be without rebuilding the app. Have to figure out how to: - Retrieve the output of the TProcess execution - Parse the output for the expected network address. -- Bo Berglund Developer in Sweden ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to check if a network is available?
I've never used open vpn, may be the 10.117 is defined in your configuration file ? For the vpns I've used, the ip adressed were fixed. ( for route, you can type too something like "route print 10.117.*" ) ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to check if a network is available?
On Fri, 18 Jun 2021 15:00:18 +0200, Jean SUZINEAU via fpc-pascal wrote: >May be you can get some information with the "route" command . > >If you run "route print" in cmd command prompt, you can get information >on the different networks available. > >https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/route_ws2008 > >(I couldn't find the doc for Windows 10, but "route print" works on >Windows 10 Pro) > >May be you can run the command "route print 0.0.0.0 " with TProcess and >parse the output ? > On command line (grep is available from a delphi): C:\>route print | grep 10.117 File STDIN: 10.117.3.0255.255.255.0 On-link10.117.3.2281 10.117.3.2 255.255.255.255 On-link10.117.3.2281 10.117.3.255 255.255.255.255 On-link10.117.3.2281 224.0.0.0240.0.0.0 On-link10.117.3.2281 255.255.255.255 255.255.255.255 On-link10.117.3.2281 If I know beforehand which will be the IP address of the VPN network I could use this... -- Bo Berglund Developer in Sweden ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to check if a network is available?
Yep, that's the golden rule in networking. The only way that you know that a bi-directional path is open is an end-to-end ping. Even then, you only know that it's open at the time the ping completed. If you are using TCP then you can always enable keepalive packets that effectively do the same thing while the TCP connection is open. The IPsec Internet Key Exchange Protocol also has the same capability for Dead Peer Detection (DPD) which works between the two end points of a VPN tunnel. On 18/06/2021 13:34, James Richters via fpc-pascal wrote: Do a Ping to an address on the network to see if it's connected? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal I would like to know how I can check if a remote network is available, i.e. if the VPN system has succeeded to connect the remote network. I need this in a class that connects an OpenVPN tunnel on demand and takes it down after use. Unfortunately openvpn-gui does not have an API call to do this... It provides an API for connect, disconnect, reconnect etc but not for returning the state of a connection for example. https://github.com/OpenVPN/openvpn-gui#send-commands-to-a-running-instance-of-openvpn-gui Any suggestions for Windows? I just want to know if a call to connect succeeded. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to check if a network is available?
May be you can get some information with the "route" command . If you run "route print" in cmd command prompt, you can get information on the different networks available. https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/route_ws2008 (I couldn't find the doc for Windows 10, but "route print" works on Windows 10 Pro) May be you can run the command "route print 0.0.0.0 " with TProcess and parse the output ? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How to check if a network is available?
Do a Ping to an address on the network to see if it's connected? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
[fpc-pascal] How to check if a network is available?
I would like to know how I can check if a remote network is available, i.e. if the VPN system has succeeded to connect the remote network. I need this in a class that connects an OpenVPN tunnel on demand and takes it down after use. Unfortunately openvpn-gui does not have an API call to do this... It provides an API for connect, disconnect, reconnect etc but not for returning the state of a connection for example. https://github.com/OpenVPN/openvpn-gui#send-commands-to-a-running-instance-of-openvpn-gui Any suggestions for Windows? I just want to know if a call to connect succeeded. -- Bo Berglund Developer in Sweden ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal