Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a string variable to a function that expects a PChar?

I have this code:

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

How can I replace the literal in the third argument with a string variable? If I use code like below it doesn't compile.

var
  dir: string;

dir := 'C:\Users\user\Desktop\sample\menu\WTSHELP\start.html';
ShellExecute(Handle, 'open', dir, nil, nil, sw_Show);
like image 778
user1884060 Avatar asked Apr 02 '13 21:04

user1884060


2 Answers

I assume that dir is of type string. Then

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

should work. Indeed, the compiler tells you this; it says something like

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

(Also notice that you normally use SW_SHOWNORMAL when you call ShellExecute.)

like image 96
Andreas Rejbrand Avatar answered Sep 23 '22 12:09

Andreas Rejbrand


ShellExecute is a Windows API. Thus, you need to pass the PChar type to it.

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

ShellExecute(Handle,'open', PChar(dir) ,nil,nil,sw_Show);
like image 7
Nick Hodges Avatar answered Sep 20 '22 12:09

Nick Hodges