Native exception with CreateProcess()

A

ashah

Hi everyone,

I am using CreateProcess() in my application and every once in a while
get a native exception while doing so. It isn't very frequent, but
I'm still wondering what might be causing it. I'm including the code
below - any suggestions are appreciated!

Thanks,
Amol

public class LaunchApp
{
public class ProcessInfo
{
public Int32 hProcess;
public Int32 hThread;
public Int32 ProcessID;
public Int32 ThreadID;
}

// CreateProcess PInvoke API
[DllImport("CoreDll.DLL", SetLastError=true)]
private static extern int CreateProcess(
String imageName,
String cmdLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
Int32 boolInheritHandles,
Int32 dwCreationFlags,
IntPtr lpEnvironment,
IntPtr lpszCurrentDir,
byte [] si,
ProcessInfo pi );

public bool CreateProcess( String ExeName, String CmdLine,
ProcessInfo pi )
{
if ( pi == null )
pi = new ProcessInfo();

byte [] si = new byte[128];

return CreateProcess(ExeName, CmdLine, IntPtr.Zero, IntPtr.Zero,
0, 0, IntPtr.Zero, IntPtr.Zero, si, pi) != 0;
}

public AfariaApp()
{
}

/// <summary>
/// Runs the application as a seperate process
/// </summary>
public void Connect(string channelSet)
{
// This is actually read in from a config file
string appPath = "\Program Files\app.exe"; // some path

this.CreateProcess(appPath, "ChannelSet=" + channelSet, null);
}
}
 
P

Peter Foot [MVP]

Comparing your P/Invoke to the one we use in our OpenNETCF libraries we pass
an IntPtr.Zero for the si argument:-

[DllImport("coredll", EntryPoint="CreateProcess", SetLastError=true)]

private extern static bool CreateProcess(string pszImageName, string
pszCmdLine, IntPtr psaProcess, IntPtr psaThread, int fInheritHandles, int
fdwCreate, IntPtr pvEnvironment, IntPtr pszCurDir, IntPtr psiStartInfo,
ProcessInfo pi);

The only arguments ever used are the ImageName, CmdLine and pi, the rest are
always passed IntPtr.Zero or 0 depending on their defined types. You may
wish to checkout the OpenNETCF.Diagnostics.Process class in the Smart Device
Framework (www.opennetcf.org/smartdeviceframework.asp).

Also does the error always occur when calling your specific application or
with any application?



Peter
 
A

ashah

Peter,

Thanks for the information. No, the error doesn't always occur -
actually it occurs very rarely (maybe a few times out of every 100
tries), which is why it has been hard to debug. I will take a look at
the OpenNETCF stuff and modify to match what is in there. I wasn't
sure if it had anything to do with the way the strings were being
passed - whether there was something special that needed to be done.
If you have other suggestions, let me know.

Thanks,
Amol

Peter Foot said:
Comparing your P/Invoke to the one we use in our OpenNETCF libraries we pass
an IntPtr.Zero for the si argument:-

[DllImport("coredll", EntryPoint="CreateProcess", SetLastError=true)]

private extern static bool CreateProcess(string pszImageName, string
pszCmdLine, IntPtr psaProcess, IntPtr psaThread, int fInheritHandles, int
fdwCreate, IntPtr pvEnvironment, IntPtr pszCurDir, IntPtr psiStartInfo,
ProcessInfo pi);

The only arguments ever used are the ImageName, CmdLine and pi, the rest are
always passed IntPtr.Zero or 0 depending on their defined types. You may
wish to checkout the OpenNETCF.Diagnostics.Process class in the Smart Device
Framework (www.opennetcf.org/smartdeviceframework.asp).

Also does the error always occur when calling your specific application or
with any application?



Peter


--
Peter Foot
Windows Embedded MVP
OpenNETCF.org Senior Advisor
www.inthehand.com | www.opennetcf.org



ashah said:
Hi everyone,

I am using CreateProcess() in my application and every once in a while
get a native exception while doing so. It isn't very frequent, but
I'm still wondering what might be causing it. I'm including the code
below - any suggestions are appreciated!

Thanks,
Amol

public class LaunchApp
{
public class ProcessInfo
{
public Int32 hProcess;
public Int32 hThread;
public Int32 ProcessID;
public Int32 ThreadID;
}

// CreateProcess PInvoke API
[DllImport("CoreDll.DLL", SetLastError=true)]
private static extern int CreateProcess(
String imageName,
String cmdLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
Int32 boolInheritHandles,
Int32 dwCreationFlags,
IntPtr lpEnvironment,
IntPtr lpszCurrentDir,
byte [] si,
ProcessInfo pi );

public bool CreateProcess( String ExeName, String CmdLine,
ProcessInfo pi )
{
if ( pi == null )
pi = new ProcessInfo();

byte [] si = new byte[128];

return CreateProcess(ExeName, CmdLine, IntPtr.Zero, IntPtr.Zero,
0, 0, IntPtr.Zero, IntPtr.Zero, si, pi) != 0;
}

public AfariaApp()
{
}

/// <summary>
/// Runs the application as a seperate process
/// </summary>
public void Connect(string channelSet)
{
// This is actually read in from a config file
string appPath = "\Program Files\app.exe"; // some path

this.CreateProcess(appPath, "ChannelSet=" + channelSet, null);
}
}
 

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