Diskpart does not handle the script correctly when executed from CreateProcess ()

diskpart "myScript.txt":

select disk 1 convert dynamic noerr select disk 2 convert dynamic noerr create volume stripe disk=1,2 noerr assign letter=X noerr 

.
,

When launched from the command line: diskpart /s myScript.txt works as expected .

However , when launched using win api CreateProcess() both conversion commands work, but when they fall into create volume , it displays:

 "The arguments you specified for this command are not valid" 

. ,

Now, to make things more interesting:
If the script is run again from CreateProcess () the 2nd time (assuming the disks are now converted, and this gives the correct error for comamnds conversions), when it falls into create volume , it works.

Does it make me think that something is doing with disks and / or executables?

Any point in the right direction is evaluated because it is very confusing. Thank you

 STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); ZeroMemory(&pi, sizeof(pi)); si.cb = sizeof(si); strncpy( command, "diskpart.exe /s myScript.txt", (sizeof(command) - 1) ); CreateProcess( "c:\\WINDOWS\\system32\\diskpart.exe", command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi ) ); 

end of original question ________________________________________________________

EDIT:
Updates and additional information:

  • Added a delay of 15-20 seconds before creating the volume command, anyway, the same error message was received.

  • Also split the work into two scripts with two calls to CreateProcess (). In the second script, just calling "create a volume" and assign it, it hung for a while, and then returned with "this command cannot be completed at this time" .. or something in effect.

  • One more note: on the first script, putting them in a dynamic one, it worked about two times slower than when working from the command line.

Perhaps you just need to run it all twice (with errors in the second run), as it worked

EDIT2
2 scripts now work or work when I tried it again. Not sure why this didn't work the first time.

+6
source share
3 answers

Since your script is running a second time, it seems that the most likely reason is related to synchronization - volumes are not ready by the time the create volume command is executed.

Based on this assumption:

You can add the detail disk command before the create volume command to see the status of the disk. This will tell you about the current state of the disk. Also select the first drive to show its details if drive 2 does not show you anything interesting. The information you get from this is likely to be useful.

As for the actual solution to the problem, there may be a problem with the delay when using drives online and offline. For instance:

 select disk 1 convert dynamic select disk 2 convert dynamic select disk 1 offline disk select disk 2 offline disk select disk 1 online disk select disk 2 online disk create volume stripe disk=1,2 assign letter=X 
+3
source

Here is an excerpt from what http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx says about the first parameter:

LpApplicationName may be NULL. In this case, the module name should be the first token, limited to a space in the lpCommandLine string.

If the executable is a 16-bit application, the value of lpApplicationName must be NULL, and the line pointed to by lpCommandLine must indicate the executable, as well as its arguments.

 given the above, Suggest: (first, assure command[] buffer is plenty large enough for the following) (may have to add the path for the 'myStript.txt) (may be necessary to add some wait time before calling CreateProcess() to assure the prior commands have completed. strncpy( command, "c:\\Windows\\System32\\diskpart.exe /s myScript.txt", (sizeof(command) - 1) ); CreateProcess( NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi ) ); 
+2
source

As there may be a synchronization problem, you need to make sure that you correctly expect the diskpart command to finish before continuing with the parent program.

I always use WaitForSingleObject after CreateProcess when I want to simulate cmd.exe processing:

 CreateProcess(NULL, cmdline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); WaitForSingleObject(pi.hProcess, INFINITE); 

This sequence is closer to the good old system ...

+2
source

Source: https://habr.com/ru/post/980778/


All Articles