[SOLVED] Correct way to pass ordinary String to WinAPI Unicode functions lazarus / 34-Windows ---------------------------------------------------------------------- flowCRANE · 2021-04-05 15:21 There are at least few ways to convert standard String variable and pass the result as a PWideChar to Windows API functions with postfix W. But I am looking for a solution that is, above all, correct, short and will work without any problems on modern Windows (especially Windows 10) and using the OBJFPC mode. So, in my program I store all strings as a standard String type. Now I need to pass two such strings to the ShellExecuteW function. These strings are to include the directory path and the file name. The function is to open a given directory in the file explorer and highlight the given file (setting lpOperation to explore). What is the correct way to pass such a strings to ShellExecuteW function? Thanks for any suggestions. ---------------------------------------------------------------------- ASerge · 2021-04-05 15:59 Quote from: furious programming on April 05, 2021, 05:21:00 pmThere are at least few ways to convert standard String variable and pass the result as a PWideChar to Windows API functions with postfix W. But I am looking for a solution that is, above all, correct, short and will work without any problems on modern Windows (especially Windows 10) and using the OBJFPC mode. So, in my program I store all strings as a standard String type. Now I need to pass two such strings to the ShellExecuteW function. These strings are to include the directory path and the file name. The function is to open a given directory in the file explorer and highlight the given file (setting lpOperation to explore). What is the correct way to pass such a strings to ShellExecuteW function? Use UnicodeString: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$MODE OBJFPC} uses Windows, ActiveX, ShellApi; procedure SelectFileInExplorer(const FileName: string);var  Params: UnicodeString;begin  Params := ' /select, ' + UTF8Decode(FileName);  ShellExecuteW(0, nil, 'explorer.exe', PWideChar(Params), nil, SW_SHOW);end; begin  SetMultiByteConversionCodePage(CP_UTF8); // Lazarus compatible  CoInitializeEx(nil, COINIT_APARTMENTTHREADED or COINIT_DISABLE_OLE1DDE);  SelectFileInExplorer('c:\Windows\system.ini');end. ---------------------------------------------------------------------- Aidex · 2021-04-05 16:10 I use this line ... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SysUtils.Ex ecuteProcess(UnicodeString('explorer.exe'),UnicodeString('/select,"'+f n+'"'),[]); ---------------------------------------------------------------------- Remy Lebeau · 2021-04-05 17:45 Quote from: Aidex on April 05, 2021, 06:10:04 pmI use this line ... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SysUtils.Ex ecuteProcess(UnicodeString('explorer.exe'),UnicodeString('/select,"'+f n+'"'),[]); Are the typecasts really necessary?  ExecuteProcess() is overloaded for RawByteString and UnicodeString, so if the type-casts are removed: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SysUtils.ExecuteProcess('explorer.exe', '/select,"'+fn+'"', []); Then, if String is a (UTF-8 encoded) AnsiString then the RawByteString overload will be called, otherwise String will be UnicodeString so the UnicodeString overload will be called instead. ---------------------------------------------------------------------- Aidex · 2021-04-05 18:00 The Unicode typecast is not necessary. I only use it because I saw in the Windows specific sourcecodes of FreePascal that there is always a typecast to Unicode done internally. Therefore I got used to use UnicodeString for paths and filenames if possible. At least under Windows it is no disadvantage, I think. Since in my other source codes the filename is already defined as UnicodeString, I did so. ---------------------------------------------------------------------- flowCRANE · 2021-04-05 18:51 To this day, I have used two ways to convert — one was: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PWideChar(UnicodeString(MyString)) and the other was: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PWideChar(UTF8ToUTF16(MyString)) Both worked, without any extra things. But I found it strange that the same conversion (regular String to PWideChar) could be solved in different ways. It seemed to me that they are not unambiguous. And now @ASerge gave another one, this time using the UTF8Decode function and UnicodeString as a temp variable (and other initialization functions, which is more complex that simple casting/converting). Actually all these ways are correct? Ultimately, I used ExecuteProcess because it allows me to skip the conversion (there is a version for RawByteString), and secondly, I can open the directory in the file explorer and additionally select the file, if it should be selected. The only problem is that I can't select multiple files this way — I tried various ways to use the /select parameter, but it didn't do anything. Anyone have an idea how to open a directory in the system file explorer and highlight a few files? Is such a thing possible? ---------------------------------------------------------------------- ASerge · 2021-04-05 18:59 Quote from: furious programming on April 05, 2021, 08:51:55 pmAnyone have an idea how to open a directory in the system file explorer and highlight a few files? Is such a thing possible? See SHOpenFolderAndSelectItems. ---------------------------------------------------------------------- flowCRANE · 2021-04-05 19:08 Ok, thanks for the reply. But in my project this is additional feature, so I will consider its implementation. Thank you all for help, problems solved.