createprocess

New to FreeBASIC? Post your questions here.
Post Reply
rsk
Posts: 35
Joined: Sep 15, 2007 16:07

createprocess

Post by rsk »

when using this function from "windows.bi", how can i change settings for example processor priority of the process and whether to give it focus or not?
(where in the library can i find the function?)
Mysoft
Posts: 836
Joined: Jul 28, 2005 13:56
Location: Brazil, Santa Catarina, Indaial (ouch!)
Contact:

Post by Mysoft »

CreateProcess have a lot of parameteres... better look into Win32 Programmer's Reference....

Code: Select all

BOOL CreateProcess(

    LPCTSTR lpApplicationName,	// pointer to name of executable module 
    LPTSTR lpCommandLine,	// pointer to command line string
    LPSECURITY_ATTRIBUTES lpProcessAttributes,	// pointer to process security attributes 
    LPSECURITY_ATTRIBUTES lpThreadAttributes,	// pointer to thread security attributes 
    BOOL bInheritHandles,	// handle inheritance flag 
    DWORD dwCreationFlags,	// creation flags 
    LPVOID lpEnvironment,	// pointer to new environment block 
    LPCTSTR lpCurrentDirectory,	// pointer to current directory name 
    LPSTARTUPINFO lpStartupInfo,	// pointer to STARTUPINFO 
    LPPROCESS_INFORMATION lpProcessInformation 	// pointer to PROCESS_INFORMATION  
   );
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Post by Zippy »

Where oh where..

Code: Select all

#include once "windows.bi"
'
dim as integer res
'
dim as PROCESS_INFORMATION pi
dim as STARTUPINFO si
    si.dwFlags=STARTF_USESHOWWINDOW  'this is required for:
    si.wShowWindow=SW_SHOWNOACTIVATE ' "or" these if multiple
    si.cb=len(si)
    
res=CreateProcess(_
        NULL,_ 'this param doesn't work consistently, use:
        "notepad.exe",_ 'complete path and quote as needed
        NULL,_
        NULL,_
        NULL,_
        NORMAL_PRIORITY_CLASS,_ 'set priority here, #6..
        NULL,_
        NULL,_
        @si,_
        @pi)
'
'
'CreateProcess Function:
'http://msdn2.microsoft.com/en-us/library/ms682425(VS.85).aspx
'                  
'SW_ constants
'http://msdn2.microsoft.com/en-us/library/ms633548.aspx

'Priority class constants:
'http://msdn2.microsoft.com/en-us/library/ms686219(VS.85).aspx
Or.. You can use SetPriorityClass() and ShowWindow() separately, after CreateProcess().

This still may not answer "where"?
rsk
Posts: 35
Joined: Sep 15, 2007 16:07

Post by rsk »

ha! thx m8s, this works perfect:) I think, with those references i can probly find whatever more info i need as well so thanks
rsk
Posts: 35
Joined: Sep 15, 2007 16:07

Post by rsk »

there is one thing i can't get to work with createprocess; how to call it if there is 'space' in path/name parameter?
zcbenz
Posts: 48
Joined: Jan 30, 2008 3:00

Post by zcbenz »

Use the shortname of path:

Code: Select all

#include once "windows.bi"
Dim as string path
Dim As String ShortPath = Space (256)
GetShortPathName(path,ShortPath,256)
print Trim (ShortPath)
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Post by Zippy »

I think it's advantageous to learn proper quoting..

This demo (bogus testing-paths) uses the 2nd param to CreateProcess to pass an executable path that contains a space, and a parameter containing a space.

Code: Select all

#include once "windows.bi"
'
dim as integer res
dim as string  tfn
'
dim as PROCESS_INFORMATION pi
dim as STARTUPINFO si
'    si.dwFlags=STARTF_USESHOWWINDOW
'    si.wShowWindow=SW_SHOWNOACTIVATE
    si.cb=Len(si)

tfn  = chr(34) & "c:\program files\notepad.exe" & chr(34) & " "
tfn += chr(34) & "c:\program files\test.txt"    & chr(34)

'or:

tfn  = """c:\program files\notepad.exe""" & " "
tfn += """c:\program files\test.txt"""
    
res=CreateProcess(_
        NULL,_ 
        tfn,_  
        NULL,_
        NULL,_
        NULL,_
        NORMAL_PRIORITY_CLASS,_
        NULL,_
        NULL,_
        @si,_
        @pi)

'if res=1 then end
'print "CreateProcess Failed.."
sleep
end
Note that CreateProcess in this case identifies/delineates the executable as everything before the first bare space (unquoted space), and the params to the exe as.. everything after the bare space.

There's a trick" if you wish to use both the 1st and 2nd CreateProcess params to pass the exectuable name/path and exe params, respectively (again, bogus testing-paths):

Code: Select all

#include once "windows.bi"
'
dim as integer res
dim as string  tfn
'
dim as PROCESS_INFORMATION pi
dim as STARTUPINFO si
'    si.dwFlags=STARTF_USESHOWWINDOW
'    si.wShowWindow=SW_SHOWNOACTIVATE
    si.cb=Len(si)

tfn = " " & """c:\program files\test.txt"""
    
res=CreateProcess(_
        "c:\program files\printcomm.exe",_
        tfn,_
        NULL,_
        NULL,_
        NULL,_
        NORMAL_PRIORITY_CLASS,_
        NULL,_
        NULL,_
        @si,_
        @pi)

'if res=1 then end
'print "CreateProcess Failed.."
sleep
end
Yes, you must prefix that 2nd param with a bare space.

Also: when using the 1st param to CreateProcess you must use an absolute path, CP will not check the path for your exe. Whereas if you use the 2nd param only, CP will check the path/shell for relative refs (i.e., "notepad.exe").
Nothoro
Posts: 6
Joined: Apr 13, 2014 7:36

Re: This is a way late to the gate reply, I found this post very helpful, thank you.

Post by Nothoro »

Zippy wrote:Where oh where..

Code: Select all

#include once "windows.bi"
'
dim as integer res
'
dim as PROCESS_INFORMATION pi
dim as STARTUPINFO si
    si.dwFlags=STARTF_USESHOWWINDOW  'this is required for:
    si.wShowWindow=SW_SHOWNOACTIVATE ' "or" these if multiple
    si.cb=len(si)
    
res=CreateProcess(_
        NULL,_ 'this param doesn't work consistently, use:
        "notepad.exe",_ 'complete path and quote as needed
        NULL,_
        NULL,_
        NULL,_
        NORMAL_PRIORITY_CLASS,_ 'set priority here, #6..
        NULL,_
        NULL,_
        @si,_
        @pi)
'
'
'CreateProcess Function:
'http://msdn2.microsoft.com/en-us/library/ms682425(VS.85).aspx
'                  
'SW_ constants
'http://msdn2.microsoft.com/en-us/library/ms633548.aspx

'Priority class constants:
'http://msdn2.microsoft.com/en-us/library/ms686219(VS.85).aspx
Or.. You can use SetPriorityClass() and ShowWindow() separately, after CreateProcess().

This still may not answer "where"?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: createprocess

Post by MrSwiss »

This is a way late to the gate reply, I found this post very helpful, thank you.
Why, then do it, in the first place?

Btw. overwriting/renaming a threads title is: 'bad news' ! (related to forum-search)
Apart from the fact, that 'bumping' old threads, is discouraged (plse. stop doing it).
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: createprocess

Post by counting_pine »

Nothoro, let me rephrase what MrSwiss has posted.

Welcome to the forum. Within reasaon, any questions you have about FreeBASIC, or any FB code you want to contribute are welcome.

It's kind of you to thank people in threads where you've been helped, but if a thread is old, it is probably worth leaving it alone unless you have a new question that needs an answer.

On the art of posting replies:
- firstly, forum posts are different from emails. When replying in an existing thread, the Subject area isn't a prominent area of the post. So it doesn't serve as a good "subject line" for what you want to say, or as a place to communicate anything, and it's generally best left as it is ("Re: <thread title>").
- secondly, it's not generally helpful to just Quote an existing post verbatim and not add anything to it.
If you want to refer to a specific part of the post, just quote that part. Or if you just want to post in a more conversational style, it may be better to be more concise, and not quote anything.

For a case like this, what you put in the subject line would have worked better as the main content of the post.
But again, a simple Thank You on its own is not worth posting when a thread's been inactive for so long, and it also carries less meaning, since you're not well-known on the forum at this stage.
Post Reply