Exception aobut Process.Start function

  • Thread starter 无头è€å°¸
  • Start date
Æ

无头è€å°¸

I have written a program A with C#. In Main function, I will run a new
executable program B with Process.Start. The code like this

Process myProcess = new Process();
myProcess.StartInfo = ......
.......
Process.Start();

In particular case, Process.Start throw a Win32Exception with access denied.
There's a dialog showed by OS. The stack is

System.ComonentModel.Win32Exception: Access denied
System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo
startInfo)
System.Diagnostics.Process.Start()

Why this exception occured? The user has the principle to access destination
executable program. I'm puzzled. Thanks everyone who can give me clew.

Here is code in Program C with C++

TCHAR szExeFullPath[MAX_PATH] = {0};
DWORD dwExeNameLen = MAX_PATH * sizeof(TCHAR);
HKEY hKey = NULL;
LONG lRet = -1;

// open Regedit key
lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_T("SOFTWARE\\ProgramC"),
0,
KEY_QUERY_VALUE,
&hKey
);

if ( ERROR_SUCCESS == lRet )
{
// Retrieves the type and data for the specified value name
// associated with an open registry key
lRet = RegQueryValueEx(hKey, _T("AppName"), 0, NULL, (LPBYTE)szExeFullPath,
&dwExeNameLen);

if ( ERROR_SUCCESS != lRet )
{
hr = E_FAIL;
}

// return true when the executable file path is exist
// close registry key
RegCloseKey(hKey);
}
else
{
hr = E_FAIL;
}

_tcscat_s(szExeFullPath, MAX_PATH, _T(" /h"));

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);

ZeroMemory(&pi, sizeof(pi));

if ( CreateProcess(NULL, // No module name (use command line).
szExeFullPath, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi) // Pointer to PROCESS_INFORMATION
structure.
)
{
//delete this;
CloseHandle(pi.hProcess);
}
else
{
hr = E_FAIL;
}

The C++ code has any problem? I don't know. Maybe there are, who can tell me?

Thank's again!
 
P

Phil Wilson

You should maybe post the full C# example.

Anyway, the Process Start defaults to UseShellExecute = True, and that's not
the direct equivalent to a CreateProcess call, and I notice that your error
is a shell error. Try UseShellExecute false.
 
A

Alvin Bruney [ASP.NET MVP]

I think it's because the application does not have access to the registry
keys. You'll need to provide the permissions to the process or at the
assembly level.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------



Phil Wilson said:
You should maybe post the full C# example.

Anyway, the Process Start defaults to UseShellExecute = True, and that's
not the direct equivalent to a CreateProcess call, and I notice that your
error is a shell error. Try UseShellExecute false.
--
Phil Wilson
[MVP Windows Installer]

???? said:
I have written a program A with C#. In Main function, I will run a new
executable program B with Process.Start. The code like this

Process myProcess = new Process();
myProcess.StartInfo = ......
......
Process.Start();

In particular case, Process.Start throw a Win32Exception with access
denied.
There's a dialog showed by OS. The stack is

System.ComonentModel.Win32Exception: Access denied
System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo
startInfo)
System.Diagnostics.Process.Start()

Why this exception occured? The user has the principle to access
destination
executable program. I'm puzzled. Thanks everyone who can give me clew.

Here is code in Program C with C++

TCHAR szExeFullPath[MAX_PATH] = {0};
DWORD dwExeNameLen = MAX_PATH * sizeof(TCHAR);
HKEY hKey = NULL;
LONG lRet = -1;

// open Regedit key
lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_T("SOFTWARE\\ProgramC"),
0,
KEY_QUERY_VALUE,
&hKey
);

if ( ERROR_SUCCESS == lRet )
{
// Retrieves the type and data for the specified value name
// associated with an open registry key
lRet = RegQueryValueEx(hKey, _T("AppName"), 0, NULL,
(LPBYTE)szExeFullPath,
&dwExeNameLen);

if ( ERROR_SUCCESS != lRet )
{
hr = E_FAIL;
}

// return true when the executable file path is exist
// close registry key
RegCloseKey(hKey);
}
else
{
hr = E_FAIL;
}

_tcscat_s(szExeFullPath, MAX_PATH, _T(" /h"));

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);

ZeroMemory(&pi, sizeof(pi));

if ( CreateProcess(NULL, // No module name (use command line).
szExeFullPath, // Command line.
NULL, // Process handle not
inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to
FALSE.
0, // No creation flags.
NULL, // Use parent's environment
block.
NULL, // Use parent's starting
directory.
&si, // Pointer to STARTUPINFO
structure.
&pi) // Pointer to PROCESS_INFORMATION
structure.
)
{
//delete this;
CloseHandle(pi.hProcess);
}
else
{
hr = E_FAIL;
}

The C++ code has any problem? I don't know. Maybe there are, who can tell
me?

Thank's again!
 

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