CreateProcess argument escape characters

C

Chance Hopkins

I'm using CreateProcess to launch an email and it's going pretty well.

The only problem im having is that my arguments are causing errors.

My string looks something like this:

(this function calls the proper api below)

CreateProcess("tmail.exe",
"-to \"" + UserEmail + "\" " +
"-subject \"" + subject + "\" " +
"-body \"" + body + "\"");


if the body string has a " or - in it I seem to get some errors. I'm
assuming I should escape them. I tried using \" and \- but that doesn't seem
to be working right.

I read here from this page about the xp shell the following

http://www.microsoft.com/resources/.../all/proddocs/en-us/ntcmds_shelloverview.mspx

You can use most characters as variable values, including white space. If
you use the special characters <, >, |, &, or ^, you must precede them with
the escape character (^) or quotation marks. If you use quotation marks,
they are included as part of the value because everything following the
equal sign is taken as the value.

So I tried ^'s and that didn't work either.

Anyone know what the reserved characters are and what the escape character
or sequence is?

Thanks for any help.


----------------------------

[DllImport("Coredll.dll")]
public extern static IntPtr CreateProcess(
string imageName,
string cmdLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
Int32 boolInheritHandles,
Int32 dwCreationFlags,
IntPtr lpEnvironment,
IntPtr lpszCurrentDir,
IntPtr si,
//byte [] si,
ProcessInfo pi
);
 
P

Peter Foot [MVP]

In order to make your code more portable (on some devices the email app was
pmail.exe, on most others it's tmail.exe) you could instead call
createprocess with a mailto: url link. You'll need to encode this with
appropriate escape characters, and you can make use of the System.Uri class
to help do this for you.

Peter
 
C

Chance Hopkins

Peter Foot said:
In order to make your code more portable (on some devices the email app
was pmail.exe, on most others it's tmail.exe)

thanks for the good tips, the code is actually like this:

string MailProg = "tmail.exe";

if(System.IO.File.Exists("\\Windows\\pmail.exe"))
MailProg = "pmail.exe";

I snipped it a bit for brevity. I'm always feeling like I paste WAY TOO MUCH
code.

you could instead call
createprocess with a mailto: url link. You'll need to encode this with
appropriate escape characters, and you can make use of the System.Uri
class to help do this for you.

I tried this but ended up manually don't a bunch of .Replace("","") cause it
doesn't work with the Uri class for some reason. You are right on with the
concept though. This got is working for me. I'm still debugging a bit and
looking for some character. I have about 2% of them not working, but the
rest do. I can't get any to work if I do a straight:

return new System.Uri(val).ToString();

from a function. I'll try to get something logical together tomorrow and
post it if I figure anything out.

Thanks for the help.

Peter

--
Peter Foot
Windows Embedded MVP
www.inthehand.com | www.peterfoot.net | www.opennetcf.org

Chance Hopkins said:
I'm using CreateProcess to launch an email and it's going pretty well.

The only problem im having is that my arguments are causing errors.

My string looks something like this:

(this function calls the proper api below)

CreateProcess("tmail.exe",
"-to \"" + UserEmail + "\" " +
"-subject \"" + subject + "\" " +
"-body \"" + body + "\"");


if the body string has a " or - in it I seem to get some errors. I'm
assuming I should escape them. I tried using \" and \- but that doesn't
seem to be working right.

I read here from this page about the xp shell the following

http://www.microsoft.com/resources/.../all/proddocs/en-us/ntcmds_shelloverview.mspx

You can use most characters as variable values, including white space. If
you use the special characters <, >, |, &, or ^, you must precede them
with the escape character (^) or quotation marks. If you use quotation
marks, they are included as part of the value because everything
following the equal sign is taken as the value.

So I tried ^'s and that didn't work either.

Anyone know what the reserved characters are and what the escape
character or sequence is?

Thanks for any help.


----------------------------

[DllImport("Coredll.dll")]
public extern static IntPtr CreateProcess(
string imageName,
string cmdLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
Int32 boolInheritHandles,
Int32 dwCreationFlags,
IntPtr lpEnvironment,
IntPtr lpszCurrentDir,
IntPtr si,
//byte [] si,
ProcessInfo pi
);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top