How to pass a string variable to a function waiting for PChar?

I have this code:

ShellExecute(Handle, 'open', 'C:\Users\user\Desktop\sample\menu\WTSHELP\start.html', nil, nil, sw_Show); 

How to replace the literal in the third argument with a string variable? If I use the code as shown below, it does not compile.

 var dir: string; dir := 'C:\Users\user\Desktop\sample\menu\WTSHELP\start.html'; ShellExecute(Handle, 'open', dir, nil, nil, sw_Show); 
+6
source share
2 answers

I assume dir is of type string . Then

 ShellExecute(Handle, 'open', PChar(dir), nil, nil, SW_SHOWNORMAL); 

must work. In fact, the compiler tells you this; he says something like

 [DCC Error] Unit1.pas(27): E2010 Incompatible types: 'string' and 'PWideChar' 

(Also note that you usually use SW_SHOWNORMAL when you call ShellExecute .)

+8
source

ShellExecute is a Windows API. So you need to pass in the type of PChar .

If I correctly assume that your dir variable is a string, you can specify the string as PChar and call ShellExecute as follows:

 ShellExecute(Handle,'open', PChar(dir) ,nil,nil,sw_Show); 
+6
source

All Articles