C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 306,819 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,718 people online right now. Registration is fast and FREE... Join Now!




CreateProcess Exception

 

CreateProcess Exception

astudio66

14 Feb, 2009 - 12:46 AM
Post #1

New D.I.C Head
*

Joined: 14 Feb, 2009
Posts: 5

hello
I am new in c++ I have problem with CreateProcess when I use it in my project an exception stop my program
exception:
------------------------------------
Unhandled exception at 0x7c825352 in test.exe: 0xC0000005: Access violation writing location 0x00403658.
-------------------------------------

and then print in output window(in visual C++ IDE):
---------------------------------------------------
'test.exe': Loaded 'D:\WINDOWS\system32\ntdll.dll', No symbols loaded.
'test.exe': Loaded 'D:\WINDOWS\system32\kernel32.dll', No symbols loaded.
'test.exe': Loaded 'D:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcr80d.dll', No symbols loaded.
'test.exe': Loaded 'D:\WINDOWS\system32\msvcrt.dll', No symbols loaded.
----------------------------------------------------
any one can help me!
thanks

User is offlineProfile CardPM
+Quote Post


bsaunders

RE: CreateProcess Exception

14 Feb, 2009 - 02:38 AM
Post #2

D.I.C Addict
****

Joined: 18 Jan, 2009
Posts: 558



Thanked: 42 times
My Contributions
May you post your code with the call to CreateProcess?
User is offlineProfile CardPM
+Quote Post

astudio

RE: CreateProcess Exception

18 Feb, 2009 - 10:38 AM
Post #3

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 4

this is my code
CODE
void main( VOID )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory( &pi, sizeof(pi) );
    ZeroMemory( &si, sizeof(si) );
    si.cb = 10;//sizeof(si);
    
     //system("tracert -d www.yahoo.com");
    
    

    // Start the child process.
    if( !CreateProcess( NULL,   // No module name (use command line).
        TEXT("C:\\Documents and Settings\\AHMAD> tracert -d www.yahoo.com"), // 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.
    )
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles.
    getchar();
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}


*** MOD EDIT: Fixed code tags ***

This post has been edited by JackOfAllTrades: 2 Mar, 2009 - 05:59 AM
User is offlineProfile CardPM
+Quote Post

bsaunders

RE: CreateProcess Exception

18 Feb, 2009 - 02:38 PM
Post #4

D.I.C Addict
****

Joined: 18 Jan, 2009
Posts: 558



Thanked: 42 times
My Contributions
I think a size of 10 bytes for si is a bit small.

What happens if you changed cb to:

si.cb = sizeof(STARTUPINFO);?
User is offlineProfile CardPM
+Quote Post

astudio66

RE: CreateProcess Exception

2 Mar, 2009 - 03:04 AM
Post #5

New D.I.C Head
*

Joined: 14 Feb, 2009
Posts: 5

yes 10 is small I changed my code to this:
CODE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment (lib, "Ws2_32.lib" )
#pragma comment (lib, "Kernel32.lib" )
void main( VOID )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    int spi=sizeof(pi);
    int ssi= sizeof(si);
    //ZeroMemory( π,spi  );
    //ZeroMemory( &si, ssi);
    si.cb=sizeof(STARTUPINFO); //sizeof(si);
    
     //system("tracert -d www.yahoo.com");
    

     STARTUPINFO* ptr=&si;
     int b=0;
    // Start the child process.
     b=CreateProcess(L"tracert.exe -d www.yahoo.com"  ,   // No module name (use command line).
       NULL, // Command line.
        NULL,             // Process handle not inheritable.
        NULL,             // Thread handle not inheritable.
        true,            // Set handle inheritance to FALSE.
       NORMAL_PRIORITY_CLASS,                // No creation flags.
        NULL,             // Use parent's environment block.
        NULL,             // Use parent's starting directory.
        ptr,              // Pointer to STARTUPINFO structure.
        π );
    if( b==0)            // Pointer to PROCESS_INFORMATION structure.
    
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles.
    getchar();
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
[code]

in this case program run but GetLastError function return 2 and command dose not execute I dont khnow why ? blink.gif blink.gif
User is offlineProfile CardPM
+Quote Post

bsaunders

RE: CreateProcess Exception

2 Mar, 2009 - 03:37 AM
Post #6

D.I.C Addict
****

Joined: 18 Jan, 2009
Posts: 558



Thanked: 42 times
My Contributions
What happens if you move the module name and command line to the second argument instead of the first?

CODE
b=CreateProcess(NULL,
       L"tracert.exe -d www.yahoo.com", // Command line.
        NULL,             // Process handle not inheritable.
        NULL,             // Thread handle not inheritable.
        true,            // Set handle inheritance to FALSE.
       NORMAL_PRIORITY_CLASS,                // No creation flags.
        NULL,             // Use parent's environment block.
        NULL,             // Use parent's starting directory.
        ptr,              // Pointer to STARTUPINFO structure.
        π );

User is offlineProfile CardPM
+Quote Post

horace

RE: CreateProcess Exception

2 Mar, 2009 - 06:22 AM
Post #7

D.I.C Lover
Group Icon

Joined: 25 Oct, 2006
Posts: 1,578



Thanked: 188 times
Dream Kudos: 50
My Contributions
try
CODE

    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    int spi=sizeof(pi);
    int ssi= sizeof(si);
    ZeroMemory( &si, sizeof(si) );
    si.cb=sizeof(STARTUPINFO); //sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
     int b=0;
    // Start the child process.
     b=CreateProcess("tracert.exe ",
        "-d www.yahoo.com"  ,     // Command line.
        NULL,             // Process handle not inheritable.
        NULL,             // Thread handle not inheritable.
        0,            // Set handle inheritance to FALSE.
       NORMAL_PRIORITY_CLASS,                // No creation flags.
        NULL,             // Use parent's environment block.
        NULL,             // Use parent's starting directory.
        &si,              // Pointer to STARTUPINFO structure.
        &pi );


User is offlineProfile CardPM
+Quote Post

astudio66

RE: CreateProcess Exception

5 Mar, 2009 - 07:20 AM
Post #8

New D.I.C Head
*

Joined: 14 Feb, 2009
Posts: 5

thanX for your helps but I cant fix that this statement (CreateProcess)is very complex mad.gif
User is offlineProfile CardPM
+Quote Post

horace

RE: CreateProcess Exception

5 Mar, 2009 - 09:41 AM
Post #9

D.I.C Lover
Group Icon

Joined: 25 Oct, 2006
Posts: 1,578



Thanked: 188 times
Dream Kudos: 50
My Contributions
QUOTE(astudio66 @ 5 Mar, 2009 - 02:20 PM) *

thanX for your helps but I cant fix that this statement (CreateProcess)is very complex mad.gif

say you have a program tracert.cpp - build it
CODE

#include <iostream.h>
#include <string.h>

//----------------------------------------------------------------------------//
// function main with parameters                                              //
//   argc - number of elements in array argv                                  //
//   argv - an array of pointers to strings                                   //
int main(int argc, char *argv[])
{
    cout << "\ntracert - number of parameters in command line is " << argc;
    for (int index = 0; index < argc; index++)
       {
       cout << "\n parameter " << index << " is " << argv[index];
       }
    cout << endl;
    return 0;
}


and you run
CODE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment (lib, "Ws2_32.lib" )
#pragma comment (lib, "Kernel32.lib" )
int main( VOID )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    int spi=sizeof(pi);
    int ssi= sizeof(si);
    ZeroMemory( &si, sizeof(si) );
    si.cb=sizeof(STARTUPINFO); //sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
     int b=0;
     printf( "\nCalling CreateProcess \n" );
    // Start the child process.
     b=CreateProcess("tracert.exe ",
        "-d www.yahoo.com"  ,     // Command line.
        NULL,             // Process handle not inheritable.
        NULL,             // Thread handle not inheritable.
        0,            // Set handle inheritance to FALSE.
       NORMAL_PRIORITY_CLASS,                // No creation flags.
        NULL,             // Use parent's environment block.
        NULL,             // Use parent's starting directory.
        &si,              // Pointer to STARTUPINFO structure.
        &pi );
    if( b==0)            // Pointer to PROCESS_INFORMATION structure.

    {
        printf( "CreateProcess failed %d (%ld).\n", b, GetLastError() );

    }
    printf( "\nCreateProcess OK\n" );

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );
    printf( "\nChild process completed\n" );

    // Close process and thread handles.
    getchar();
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
}

and the output is
CODE


tracert - number of parameters in command line is 2
parameter 0 is -d
parameter 1 is www.yahoo.com

Calling CreateProcess

CreateProcess OK

Child process completed


User is offlineProfile CardPM
+Quote Post

jeff666

RE: CreateProcess Exception

5 Mar, 2009 - 09:57 AM
Post #10

D.I.C Head
**

Joined: 30 Dec, 2008
Posts: 167



Thanked: 6 times
My Contributions
Why don't you copy-paste MSDN samples ?!!!
There are hundred ones !
User is offlineProfile CardPM
+Quote Post

astudio66

RE: CreateProcess Exception

7 Mar, 2009 - 01:06 AM
Post #11

New D.I.C Head
*

Joined: 14 Feb, 2009
Posts: 5

yes I did it!(copy-paste)
I think this code have a problem in pointers. I am java programmer and in java we have pointers like in c++
thanX for your helps
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/20/09 10:21PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month