Re: [DUG] Variable in String
Well, setting aside the obfuscation of burying the real work of the routine as a side effect of code that exists merely to provide the scaffolding required to make that call, this code actually contains an error: setlength(temp, 100); //has to be big enough first setlength(temp, getEnvironmentVariable(PChar('USERNAME'), PChar(temp), length(temp))); Since GetEnvironmentVariable() works with a plain null terminated buffer, the length of the buffer that you indicate in the call to it needs to reflect the expected presence of that terminator in the buffer. But the null terminator on a String type variable is implicit, and not part of the payload and so not reflected in the length. If the USERNAME were exactly 100 characters in length, your call to GetEnvironmentVariable() would fail, even though the buffer you are passing is actually the right size. i.e. you should actually call getEnvironmentVariable(PChar('USERNAME'), PChar(temp), length(temp) + 1) This to my mind perfectly illustrates the problem: treating one type (a string) as if it were another in a situation where the differences are actually important. Passing a String value to an external routine that simple READS from a null terminated buffer is much more straightforward and less error prone (imho – due to being reminded of what you are working with by the very nature of what you are working with) than trying to pass a pointer which is actually an offset into a complex String type to a function that expects to modify a plain null terminated buffer. When reading from a string, the payload can be safely naively regarded as a null terminated buffer by a function that is unaware of the greater complexity of the type of value it is actually being passed. But when writing to a null terminated buffer, there be dragons when that buffer isn’t just a null terminated buffer but actually the payload of a far more complex type. If an external routine expected a pointer to an unsigned 32 bit value I doubt you would consider it sensible to contrive to pass a pointer to an signed 16-bit variable, even if you always got the desired results (at least, up to now... ). J From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of John Bird Sent: Wednesday, 29 June 2011 13:19 To: NZ Borland Developers Group - Delphi List Subject: Re: [DUG] Variable in String Jolyon I imagine you have an opinion on doing this your way rather than like this example below – I would be interested in hearing your reasoning. A similar example – this is the sort of code I have been using setlength(temp, 100); //has to be big enough first setlength(temp, getEnvironmentVariable(PChar('USERNAME'), PChar(temp), length(temp))); John From: Jolyon Smith <mailto:jsm...@deltics.co.nz> Sent: Monday, June 27, 2011 10:07 AM To: 'NZ Borland Developers Group - Delphi List' <mailto:delphi@delphi.org.nz> Subject: Re: [DUG] Variable in String Sorry Bob, I meant to include an example of your code tweaked to use “raw” a char array with the Windows API routine. Here it is (this version displays results rather than storing in a variable, but you get the idea J ) : var dir: array of Char; s: String; begin s := ‘’; SetLength(dir, MAX_PATH + 1); if Succeeded(SHGetFolderPath(0, CSIDL_Program_Files, 0, 0, @dir[0])) then s := PChar(dir); ShowMessageFmt(s + ' (%d chars)', [Length(s)]); // Outcome: s has both the right length *and* is null terminated correctly end; From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Monday, 27 June 2011 08:58 To: 'NZ Borland Developers Group - Delphi List' Subject: Re: [DUG] Variable in String Hi Jolyon I was wondering if my problem is a conflict between how I derive the value of the variable (PAnsiChar). Here is the code for doing that. SetLength(sDir, MAX_PATH); ZeroMemory(@sDir[1], MAX_PATH); if Succeeded(SHGetFolderPath(0, CSIDL_Program_Files, 0, 0, PAnsiChar(sDir))) then FW_Path := sDir; Bob From: Jolyon Smith <mailto:jsm...@deltics.co.nz> Sent: Wednesday, June 22, 2011 3:49 PM To: 'NZ Borland Developers Group - Delphi List' <mailto:delphi@delphi.org.nz> Subject: Re: [DUG] Variable in String Don’t quote FW_Path element of the program path – you need to quote the entire path AND program file name when/if any part of the path or the filename itself does – or may – contain spaces: e.g. “path a\sub a\sub b\prog.exe” not “path a”\sub\prog.exe So in your case, this should do the trick: FW_Path := X; DXF := openDialog1.FileName; ProgramName := ‘”’ + FW_Path + '\FWTools2.4.7\bin\ogr2ogr” "-f" "PostgreSQL" PG:"
Re: [DUG] Variable in String
Jolyon I imagine you have an opinion on doing this your way rather than like this example below – I would be interested in hearing your reasoning. A similar example – this is the sort of code I have been using setlength(temp, 100); //has to be big enough first setlength(temp, getEnvironmentVariable(PChar('USERNAME'), PChar(temp), length(temp))); John From: Jolyon Smith Sent: Monday, June 27, 2011 10:07 AM To: 'NZ Borland Developers Group - Delphi List' Subject: Re: [DUG] Variable in String Sorry Bob, I meant to include an example of your code tweaked to use “raw” a char array with the Windows API routine. Here it is (this version displays results rather than storing in a variable, but you get the idea J ) : var dir: array of Char; s: String; begin s := ‘’; SetLength(dir, MAX_PATH + 1); if Succeeded(SHGetFolderPath(0, CSIDL_Program_Files, 0, 0, @dir[0])) then s := PChar(dir); ShowMessageFmt(s + ' (%d chars)', [Length(s)]); // Outcome: s has both the right length *and* is null terminated correctly end; From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Monday, 27 June 2011 08:58 To: 'NZ Borland Developers Group - Delphi List' Subject: Re: [DUG] Variable in String Hi Jolyon I was wondering if my problem is a conflict between how I derive the value of the variable (PAnsiChar). Here is the code for doing that. SetLength(sDir, MAX_PATH); ZeroMemory(@sDir[1], MAX_PATH); if Succeeded(SHGetFolderPath(0, CSIDL_Program_Files, 0, 0, PAnsiChar(sDir))) then FW_Path := sDir; Bob From: Jolyon Smith Sent: Wednesday, June 22, 2011 3:49 PM To: 'NZ Borland Developers Group - Delphi List' Subject: Re: [DUG] Variable in String Don’t quote FW_Path element of the program path – you need to quote the entire path AND program file name when/if any part of the path or the filename itself does – or may – contain spaces: e.g. “path a\sub a\sub b\prog.exe” not “path a”\sub\prog.exe So in your case, this should do the trick: FW_Path := X; DXF := openDialog1.FileName; ProgramName := ‘”’ + FW_Path + '\FWTools2.4.7\bin\ogr2ogr” "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); hth From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Thursday, 23 June 2011 10:30 To: DUG Subject: [DUG] Variable in String Hi I’m having trouble with using a variable in a string path. When I use the variable FW_Path := ‘C:\Program Files (x86)’ with two single quotes, the following works well and ShowMessage(ProgramName); displayed the full path . When I reference FW_Path to a variable X I get an error returned “Can Not run” The variable X is returned as C:\Program Files (x86) without quotes. I attempted Quote String and got the following ‘C:\Program Files (x86) with one single quote. Both cases return the same error - and in both cases ShowMessage(ProgramName); displayed none of the path after C:\Program Files (x86). Help would be appreciated. Bob FW_Path := QuoteStr(X); DXF := openDialog1.FileName; ProgramName :=FW_Path+'\FWTools2.4.7\bin\ogr2ogr "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe
Re: [DUG] Variable in String
Hi Jolyon Works perfectly. Thanks a lot for all your help. Bob From: Jolyon Smith Sent: Sunday, June 26, 2011 3:07 PM To: 'NZ Borland Developers Group - Delphi List' Subject: Re: [DUG] Variable in String Sorry Bob, I meant to include an example of your code tweaked to use “raw” a char array with the Windows API routine. Here it is (this version displays results rather than storing in a variable, but you get the idea J ) : var dir: array of Char; s: String; begin s := ‘’; SetLength(dir, MAX_PATH + 1); if Succeeded(SHGetFolderPath(0, CSIDL_Program_Files, 0, 0, @dir[0])) then s := PChar(dir); ShowMessageFmt(s + ' (%d chars)', [Length(s)]); // Outcome: s has both the right length *and* is null terminated correctly end; From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Monday, 27 June 2011 08:58 To: 'NZ Borland Developers Group - Delphi List' Subject: Re: [DUG] Variable in String Hi Jolyon I was wondering if my problem is a conflict between how I derive the value of the variable (PAnsiChar). Here is the code for doing that. SetLength(sDir, MAX_PATH); ZeroMemory(@sDir[1], MAX_PATH); if Succeeded(SHGetFolderPath(0, CSIDL_Program_Files, 0, 0, PAnsiChar(sDir))) then FW_Path := sDir; Bob From: Jolyon Smith Sent: Wednesday, June 22, 2011 3:49 PM To: 'NZ Borland Developers Group - Delphi List' Subject: Re: [DUG] Variable in String Don’t quote FW_Path element of the program path – you need to quote the entire path AND program file name when/if any part of the path or the filename itself does – or may – contain spaces: e.g. “path a\sub a\sub b\prog.exe” not “path a”\sub\prog.exe So in your case, this should do the trick: FW_Path := X; DXF := openDialog1.FileName; ProgramName := ‘”’ + FW_Path + '\FWTools2.4.7\bin\ogr2ogr” "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); hth From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Thursday, 23 June 2011 10:30 To: DUG Subject: [DUG] Variable in String Hi I’m having trouble with using a variable in a string path. When I use the variable FW_Path := ‘C:\Program Files (x86)’ with two single quotes, the following works well and ShowMessage(ProgramName); displayed the full path . When I reference FW_Path to a variable X I get an error returned “Can Not run” The variable X is returned as C:\Program Files (x86) without quotes. I attempted Quote String and got the following ‘C:\Program Files (x86) with one single quote. Both cases return the same error - and in both cases ShowMessage(ProgramName); displayed none of the path after C:\Program Files (x86). Help would be appreciated. Bob FW_Path := QuoteStr(X); DXF := openDialog1.FileName; ProgramName :=FW_Path+'\FWTools2.4.7\bin\ogr2ogr "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe
Re: [DUG] Variable in String
Sorry Bob, I meant to include an example of your code tweaked to use “raw” a char array with the Windows API routine. Here it is (this version displays results rather than storing in a variable, but you get the idea J ) : var dir: array of Char; s: String; begin s := ‘’; SetLength(dir, MAX_PATH + 1); if Succeeded(SHGetFolderPath(0, CSIDL_Program_Files, 0, 0, @dir[0])) then s := PChar(dir); ShowMessageFmt(s + ' (%d chars)', [Length(s)]); // Outcome: s has both the right length *and* is null terminated correctly end; From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Monday, 27 June 2011 08:58 To: 'NZ Borland Developers Group - Delphi List' Subject: Re: [DUG] Variable in String Hi Jolyon I was wondering if my problem is a conflict between how I derive the value of the variable (PAnsiChar). Here is the code for doing that. SetLength(sDir, MAX_PATH); ZeroMemory(@sDir[1], MAX_PATH); if Succeeded(SHGetFolderPath(0, CSIDL_Program_Files, 0, 0, PAnsiChar(sDir))) then FW_Path := sDir; Bob From: Jolyon Smith <mailto:jsm...@deltics.co.nz> Sent: Wednesday, June 22, 2011 3:49 PM To: 'NZ Borland Developers Group - Delphi List' <mailto:delphi@delphi.org.nz> Subject: Re: [DUG] Variable in String Don’t quote FW_Path element of the program path – you need to quote the entire path AND program file name when/if any part of the path or the filename itself does – or may – contain spaces: e.g. “path a\sub a\sub b\prog.exe” not “path a”\sub\prog.exe So in your case, this should do the trick: FW_Path := X; DXF := openDialog1.FileName; ProgramName := ‘”’ + FW_Path + '\FWTools2.4.7\bin\ogr2ogr” "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); hth From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Thursday, 23 June 2011 10:30 To: DUG Subject: [DUG] Variable in String Hi I’m having trouble with using a variable in a string path. When I use the variable FW_Path := ‘C:\Program Files (x86)’ with two single quotes, the following works well and ShowMessage(ProgramName); displayed the full path . When I reference FW_Path to a variable X I get an error returned “Can Not run” The variable X is returned as C:\Program Files (x86) without quotes. I attempted Quote String and got the following ‘C:\Program Files (x86) with one single quote. Both cases return the same error - and in both cases ShowMessage(ProgramName); displayed none of the path after C:\Program Files (x86). Help would be appreciated. Bob FW_Path := QuoteStr(X); DXF := openDialog1.FileName; ProgramName :=FW_Path+'\FWTools2.4.7\bin\ogr2ogr "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); _ ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe
Re: [DUG] Variable in String
Aha! J Quite possibly... SetLength(sDir, MAX_PATH); ZeroMemory(@sDir[1], MAX_PATH); if Succeeded(SHGetFolderPath(0, CSIDL_Program_Files, 0, 0, PAnsiChar(sDir))) then FW_Path := sDir; Yes, you are setting the string length to MAX_PATH, so any string data with a length of LESS than MAX_PATH will contain null chars after the actual string value as part of the string value data itself! If you inspect the value of sDir it will appear to be correct, because the code displaying the string contents will almost certainly be treating the first NULL it finds as the string terminator, irrespective of the string length you have set, but when you try to combine that string with other strings using the Delphi string handling functions, the explicit LENGTH of the string will be used, not the null terminator: So, building on my previous example: s := ‘hat’#0#0; s := s + ‘ and gloves’; // At this point “s” contains: ‘hat’#0#0’ and gloves” To fix your code you have at least two options: 1) After getting the contents of sDir, call SetLength() again and set the actual length of the string 2) Use a char array in the windows API call and exploit the RTL support for auto conversion of PChars to and from strings I myself would go with the latter. The Windows API is designed to work with “raw” pointers to char arrays – co-ercing functions that *modify* buffer contents to accept Delphi strings makes me nervous (functions that just accept string data without modifying it are a different matter, and I have no trouble simply casting a string to a PChar in those circumstances). J Also, I am guessing from your use of PANSIChar that you are using Delphi 2007 or earlier – this code will fail if/when you move to Delphi 2009 as the SHGetFolderPath() routine in those later versions will expect/require a PWIDEChar param. The easiest way to deal with this is to simply use PChar instead – in D2007< PChar = PANSIChar and in D2009+ PChar = PWideChar. But, to further protect yourself, the ZeroMemory() call should use MAX_PATH * SizeOf(Char) to indicate the number of zero bytes to write (Char can change size depending on Delphi versions). Having said that, I suspect (but am not 100% sure) that the ZeroMemory() call is redundant in this case. Alternatively you could explicitly use the ANSI version of SHGetFolderPath (SHGetFolderPathA) with a PANSIChar, but this is likely to get even messier with the use of String in the Delphi Unicode compatibility arena, since a “String” cannot be typecast as a PANSIChar in D2009+. I also think you may need to +1 on MAX_PATH to allow for the null terminator that the Windows API is most likely to include in any return value. Whew – that’s quite a start to a Monday morning. J hth Jolyon From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Monday, 27 June 2011 08:58 To: 'NZ Borland Developers Group - Delphi List' Subject: Re: [DUG] Variable in String Hi Jolyon I was wondering if my problem is a conflict between how I derive the value of the variable (PAnsiChar). Here is the code for doing that. ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe
Re: [DUG] Variable in String
There isn’t really enough information to go on here to say for sure what your problem is. “ProgramName” doesn’t “see” anything. What you use ProgramName with is more relevant (i.e. what some other function “sees” ProgramName as containing. If the value of FW_Path contains a null character and if the function you pass ProgramName to is dealing with null terminated strings, then it will consider the value of the string to be terminated by that null char, regardless of the actual length of the string. Demonstration: var s: String; msg: String; begin s := 'hat'#0; // < the null char is part of the string (in addition to the null char marking the END of the string msg := 'That''s a nice ' + s + ' you are wearing'; ShowMessage('Length of message = ' + IntToStr(Length(msg)) + ' chars'); ShowMessage(msg); end; From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Monday, 27 June 2011 05:56 To: 'NZ Borland Developers Group - Delphi List' Subject: Re: [DUG] Variable in String Hi Jolyon Double quoting the path doesn’t seem to work.ProgramName := ‘”’ + FW_Path + '\FWTools2.4.7\bin\ogr2ogr” "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; All that ProgramName sees is the string variable FW_Path. Even with the +, all of the remaining string is ignored. Bob From: Jolyon Smith <mailto:jsm...@deltics.co.nz> Sent: Wednesday, June 22, 2011 3:49 PM To: 'NZ Borland Developers Group - Delphi List' <mailto:delphi@delphi.org.nz> Subject: Re: [DUG] Variable in String Don’t quote FW_Path element of the program path – you need to quote the entire path AND program file name when/if any part of the path or the filename itself does – or may – contain spaces: e.g. “path a\sub a\sub b\prog.exe” not “path a”\sub\prog.exe So in your case, this should do the trick: FW_Path := X; DXF := openDialog1.FileName; ProgramName := ‘”’ + FW_Path + '\FWTools2.4.7\bin\ogr2ogr” "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); hth From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Thursday, 23 June 2011 10:30 To: DUG Subject: [DUG] Variable in String Hi I’m having trouble with using a variable in a string path. When I use the variable FW_Path := ‘C:\Program Files (x86)’ with two single quotes, the following works well and ShowMessage(ProgramName); displayed the full path . When I reference FW_Path to a variable X I get an error returned “Can Not run” The variable X is returned as C:\Program Files (x86) without quotes. I attempted Quote String and got the following ‘C:\Program Files (x86) with one single quote. Both cases return the same error - and in both cases ShowMessage(ProgramName); displayed none of the path after C:\Program Files (x86). Help would be appreciated. Bob FW_Path := QuoteStr(X); DXF := openDialog1.FileName; ProgramName :=FW_Path+'\FWTools2.4.7\bin\ogr2ogr "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); _ ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe
Re: [DUG] Variable in String
Hi Jolyon I was wondering if my problem is a conflict between how I derive the value of the variable (PAnsiChar). Here is the code for doing that. SetLength(sDir, MAX_PATH); ZeroMemory(@sDir[1], MAX_PATH); if Succeeded(SHGetFolderPath(0, CSIDL_Program_Files, 0, 0, PAnsiChar(sDir))) then FW_Path := sDir; Bob From: Jolyon Smith Sent: Wednesday, June 22, 2011 3:49 PM To: 'NZ Borland Developers Group - Delphi List' Subject: Re: [DUG] Variable in String Don’t quote FW_Path element of the program path – you need to quote the entire path AND program file name when/if any part of the path or the filename itself does – or may – contain spaces: e.g. “path a\sub a\sub b\prog.exe” not “path a”\sub\prog.exe So in your case, this should do the trick: FW_Path := X; DXF := openDialog1.FileName; ProgramName := ‘”’ + FW_Path + '\FWTools2.4.7\bin\ogr2ogr” "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); hth From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Thursday, 23 June 2011 10:30 To: DUG Subject: [DUG] Variable in String Hi I’m having trouble with using a variable in a string path. When I use the variable FW_Path := ‘C:\Program Files (x86)’ with two single quotes, the following works well and ShowMessage(ProgramName); displayed the full path . When I reference FW_Path to a variable X I get an error returned “Can Not run” The variable X is returned as C:\Program Files (x86) without quotes. I attempted Quote String and got the following ‘C:\Program Files (x86) with one single quote. Both cases return the same error - and in both cases ShowMessage(ProgramName); displayed none of the path after C:\Program Files (x86). Help would be appreciated. Bob FW_Path := QuoteStr(X); DXF := openDialog1.FileName; ProgramName :=FW_Path+'\FWTools2.4.7\bin\ogr2ogr "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe
Re: [DUG] Variable in String
Hi Jolyon Double quoting the path doesn’t seem to work.ProgramName := ‘”’ + FW_Path + '\FWTools2.4.7\bin\ogr2ogr” "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; All that ProgramName sees is the string variable FW_Path. Even with the +, all of the remaining string is ignored. Bob From: Jolyon Smith Sent: Wednesday, June 22, 2011 3:49 PM To: 'NZ Borland Developers Group - Delphi List' Subject: Re: [DUG] Variable in String Don’t quote FW_Path element of the program path – you need to quote the entire path AND program file name when/if any part of the path or the filename itself does – or may – contain spaces: e.g. “path a\sub a\sub b\prog.exe” not “path a”\sub\prog.exe So in your case, this should do the trick: FW_Path := X; DXF := openDialog1.FileName; ProgramName := ‘”’ + FW_Path + '\FWTools2.4.7\bin\ogr2ogr” "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); hth From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Thursday, 23 June 2011 10:30 To: DUG Subject: [DUG] Variable in String Hi I’m having trouble with using a variable in a string path. When I use the variable FW_Path := ‘C:\Program Files (x86)’ with two single quotes, the following works well and ShowMessage(ProgramName); displayed the full path . When I reference FW_Path to a variable X I get an error returned “Can Not run” The variable X is returned as C:\Program Files (x86) without quotes. I attempted Quote String and got the following ‘C:\Program Files (x86) with one single quote. Both cases return the same error - and in both cases ShowMessage(ProgramName); displayed none of the path after C:\Program Files (x86). Help would be appreciated. Bob FW_Path := QuoteStr(X); DXF := openDialog1.FileName; ProgramName :=FW_Path+'\FWTools2.4.7\bin\ogr2ogr "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe
Re: [DUG] Variable in String
The easiest way to test this is whatever string you come up with copy it to the clipboard and paste in into the RUN prompt. If it doesn't run there, it won't run within your application. Don't use the command prompt (for testing), as it treats spaces differently. How are you actually executing the generated command prompt? I have a unit that creates processes. I pass the program name as a parameter and then a stringlist for all other parameters. The method then double quotes the program name and then each of the parameters as the command line is generated. Perhaps something like this would help clean up the code On Thu, Jun 23, 2011 at 8:30 AM, Bob Pawley wrote: > Hi > > I’m having trouble with using a variable in a string path. > > When I use the variable FW_Path := ‘C:\Program Files (x86)’ with two single > quotes, the following works well and ShowMessage(ProgramName); displayed the > full path . > > When I reference FW_Path to a variable X I get an error returned “Can Not > run” The variable X is returned as C:\Program Files (x86) without > quotes. > > I attempted Quote String and got the following ‘C:\Program Files (x86) with > one single quote. > > Both cases return the same error - and in both cases > ShowMessage(ProgramName); displayed none of the path after C:\Program Files > (x86). > > Help would be appreciated. > > Bob > >FW_Path := QuoteStr(X); > DXF := openDialog1.FileName; > ProgramName :=FW_Path+'\FWTools2.4.7\bin\ogr2ogr "-f" "PostgreSQL" > PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" > -nln Import_Process'; > ShowMessage(ProgramName); > > ___ > NZ Borland Developers Group - Delphi mailing list > Post: delphi@delphi.org.nz > Admin: http://delphi.org.nz/mailman/listinfo/delphi > Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: > unsubscribe > ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe
Re: [DUG] Variable in String
Don’t quote FW_Path element of the program path – you need to quote the entire path AND program file name when/if any part of the path or the filename itself does – or may – contain spaces: e.g. “path a\sub a\sub b\prog.exe” not “path a”\sub\prog.exe So in your case, this should do the trick: FW_Path := X; DXF := openDialog1.FileName; ProgramName := ‘”’ + FW_Path + '\FWTools2.4.7\bin\ogr2ogr” "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); hth From: delphi-boun...@delphi.org.nz [mailto:delphi-boun...@delphi.org.nz] On Behalf Of Bob Pawley Sent: Thursday, 23 June 2011 10:30 To: DUG Subject: [DUG] Variable in String Hi I’m having trouble with using a variable in a string path. When I use the variable FW_Path := ‘C:\Program Files (x86)’ with two single quotes, the following works well and ShowMessage(ProgramName); displayed the full path . When I reference FW_Path to a variable X I get an error returned “Can Not run” The variable X is returned as C:\Program Files (x86) without quotes. I attempted Quote String and got the following ‘C:\Program Files (x86) with one single quote. Both cases return the same error - and in both cases ShowMessage(ProgramName); displayed none of the path after C:\Program Files (x86). Help would be appreciated. Bob FW_Path := QuoteStr(X); DXF := openDialog1.FileName; ProgramName :=FW_Path+'\FWTools2.4.7\bin\ogr2ogr "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; ShowMessage(ProgramName); ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe
Re: [DUG] Variable in String
Quotestr puts single quotes around the variable. You need double quotes. Ie. " On 23/06/2011 8:33 AM, "Bob Pawley" wrote: > Hi > > I’m having trouble with using a variable in a string path. > > When I use the variable FW_Path := ‘C:\Program Files (x86)’ with two single quotes, the following works well and ShowMessage(ProgramName); displayed the full path . > > When I reference FW_Path to a variable X I get an error returned “Can Not run” The variable X is returned as C:\Program Files (x86) without quotes. > > I attempted Quote String and got the following ‘C:\Program Files (x86) with one single quote. > > Both cases return the same error - and in both cases ShowMessage(ProgramName); displayed none of the path after C:\Program Files (x86). > > Help would be appreciated. > > Bob > > FW_Path := QuoteStr(X); > DXF := openDialog1.FileName; > ProgramName :=FW_Path+'\FWTools2.4.7\bin\ogr2ogr "-f" "PostgreSQL" PG:"host=192 user=postgres dbname=E5R password=" "'+ DXF +'" -nln Import_Process'; > > ShowMessage(ProgramName); ___ NZ Borland Developers Group - Delphi mailing list Post: delphi@delphi.org.nz Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to delphi-requ...@delphi.org.nz with Subject: unsubscribe