Reputation: 473
I have the following code that works fine up to the last line in DskPrt1.txt assign letter=
. The file gets written as it should. In a dropdownlist, I choose a drive letter, send the chosen letter to FILE fp
, write it out and then direct diskpart to read it. This is the output
Volume 1 is the selected volume
the arguments specified for this command are not valid.
This tells me it is doing everything up until the second line in DskPrt.txt. I have copied and pasted the assign letter=X
into diskpart manually and it runs just fine. Why doesn't it work with my code?
Part 1
SendMessage(
(HWND) hWndDropMenu, // handle to destination window
CB_GETLBTEXT, // message to send
(WPARAM) wParam, // not used; must be zero
(LPARAM)tmpMsg // not used; must be zero
);
Part 2
FILE *fp;
fp = fopen("DskPrt1.txt", "wt");
char DskPrt11[] = "select volume 1";
char DskPrt12[] = "assign letter=";
fwrite (DskPrt11 , 1 , sizeof(DskPrt11) , fp ); //Line 1
fwrite("\n", sizeof(char), 1, fp); //New line
fwrite (DskPrt12 , 1 , sizeof(DskPrt12) , fp ); //Line 2
fwrite (tmpMsg , 1 , sizeof(tmpMsg) , fp ); //Letter
fclose(fp);
//Execute part 1 commands
std::wstring arrString[3] = {L"/C mkdir C:\\Users\\Andrew\\Desktop\\test",L"/C DISKPART /s C:\\Users\\Andrew\\Desktop\\DskPrt1.txt"};
LPWSTR cmd =L"C:\\Windows\\System32\\cmd.exe";
for(int i=0; i<2; i++)
{
STARTUPINFO info={sizeof(info)};
PROCESS_INFORMATION processInfo;
CreateProcessW(cmd, &arrString[i][0], NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &info, &processInfo);
::WaitForSingleObject(processInfo.hProcess, INFINITE);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}
Upvotes: 0
Views: 418
Reputation: 6846
Because of this:
fwrite (DskPrt11 , 1 , sizeof(DskPrt11) , fp );
By using sizeof(), you're including the terminating null. So your file ends up with at least two null bytes that you don't want. Use strlen() instead.
Upvotes: 1