how can I run an external program like ( word , notepad ,... )In C ?
and how can I set a certain size of the window of the external program like ( 300 * 300 ) ?

Recommended Answers

All 7 Replies

win32 api function CreateProcess() will let you do it, and I think it will even let you specify the screen size in the startupInfo parameter.

Try it with function system() which takes string. All written as parameter to this function is like you type in cmd.
For example, system("notepad.exe") should run notepad.

system() will not let you specify the screen size.

thanks all

Ancient Dragon
can you put the code for run notepad or word ?
because i tried many time but does not work ( i use Visual Studio )

Whether you can specify the new window's size and location will depend on the program you are trying to launch. Some program, apparently like Notepad.exe, do not honor such requests. In this example I created a Windows Forms program. The code below will change its initial x and y upper-left coordinated but the window size does not change.

#include <Windows.h>
#include <iostream>
using std::cout;

int main(int argc, char* argv[])
{
    STARTUPINFO info;
    PROCESS_INFORMATION pinfo;
    memset(&info,0,sizeof(info));
    memset(&pinfo,0,sizeof(pinfo));
    info.cb = sizeof(STARTUPINFO);
    info.dwX = 150;
    info.dwY = 150;
    info.dwXSize = 300;
    info.dwYSize = 600;
    info.dwFlags = STARTF_USEPOSITION|STARTF_USESIZE;

    BOOL rval = CreateProcess("C:\\dvlp\\addressbooki.exe",0,0,0,
        0,0,NULL,NULL,&info,&pinfo);
    if( rval == FALSE)
    {
        char buf[255] = {0};
        DWORD dwError = GetLastError();
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,dwError,0,buf,sizeof(buf),0);
        cout << buf << '\n';
        return 1;
    }
    cout << "All ok\n";
   // The next line is optional -- only needed if this program
   // needs to wait until the spawned program has finished initializing
   // itself.
    WaitForInputIdle(pinfo.hProcess,INFINITE);



	return 0;
}

Whether you can specify the new window's size and location will depend on the program you are trying to launch. Some program, apparently like Notepad.exe, do not honor such requests. In this example I created a Windows Forms program. The code below will change its initial x and y upper-left coordinated but the window size does not change.

#include <Windows.h>
#include <iostream>
using std::cout;

int main(int argc, char* argv[])
{
    STARTUPINFO info;
    PROCESS_INFORMATION pinfo;
    memset(&info,0,sizeof(info));
    memset(&pinfo,0,sizeof(pinfo));
    info.cb = sizeof(STARTUPINFO);
    info.dwX = 150;
    info.dwY = 150;
    info.dwXSize = 300;
    info.dwYSize = 600;
    info.dwFlags = STARTF_USEPOSITION|STARTF_USESIZE;

    BOOL rval = CreateProcess("C:\\dvlp\\addressbooki.exe",0,0,0,
        0,0,NULL,NULL,&info,&pinfo);
    if( rval == FALSE)
    {
        char buf[255] = {0};
        DWORD dwError = GetLastError();
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,dwError,0,buf,sizeof(buf),0);
        cout << buf << '\n';
        return 1;
    }
    cout << "All ok\n";
   // The next line is optional -- only needed if this program
   // needs to wait until the spawned program has finished initializing
   // itself.
    WaitForInputIdle(pinfo.hProcess,INFINITE);



	return 0;
}

He asked for C, not for C++

commented: good catch :) +36

Sorry about that -- replace cout with printf(). The code for CreateProcess() is the same in both C and C++.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.