C++实现修改磁盘san策略

    最近在公司遇到磁盘镜像盘写io,GetLastError返回错误码19,磁盘受到介质保护,diskpart查看了一下磁盘的san策略,发现策略为使共享磁盘脱机。现在要支持修改san策略为全部磁盘联机。


C++代码如下:

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

using namespace std;

int SharedDiskOperation(int cmd)
{
	HANDLE hChildStdinRd = NULL, hChildStdinWr = NULL, hChildStdoutWr = NULL, hChildStdoutRd = NULL;
	SECURITY_ATTRIBUTES saAttr;
	saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
	saAttr.bInheritHandle = TRUE;
	saAttr.lpSecurityDescriptor = NULL;

	if (!CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
	{
		cout << "Create stdout pipe failed when set shared disk online, error code:" << GetLastError() << endl;
		return 1;
	}
	SetHandleInformation(hChildStdoutRd, HANDLE_FLAG_INHERIT, 0);

	if (!CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
	{
		CloseHandle(hChildStdoutRd);
		CloseHandle(hChildStdoutWr);
		cout << "Create stdin pipe failed when set shared disk online, error code:" << GetLastError() << endl;
		return 2;
	}
	SetHandleInformation(hChildStdinWr, HANDLE_FLAG_INHERIT, 0);

	TCHAR szCmdline[] = TEXT("diskpart.exe");
	PROCESS_INFORMATION piProcInfo;
	STARTUPINFO siStartInfo;
	BOOL bFuncRetn = FALSE;

	ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
	ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
	siStartInfo.cb = sizeof(STARTUPINFO);
	siStartInfo.hStdError = hChildStdoutWr;
	siStartInfo.hStdOutput = hChildStdoutWr;       //控制台窗口
	siStartInfo.hStdInput = hChildStdinRd;         //键盘
	siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

	bFuncRetn = CreateProcess(NULL, szCmdline, NULL, NULL, TRUE, 0, NULL, NULL, &siStartInfo, &piProcInfo);

	if (bFuncRetn == 0)
	{
		CloseHandle(hChildStdinRd);
		CloseHandle(hChildStdinWr);
		CloseHandle(hChildStdoutWr);
		CloseHandle(hChildStdoutRd);
		cout << "Create process failed when set shared disk online, error code" << GetLastError() << endl;
		return 3;
	}
	else
	{
		CloseHandle(piProcInfo.hProcess);
		CloseHandle(piProcInfo.hThread);
	}

	DWORD dwWritten;
	if (cmd == 1)//san policy = onlineall
	{
		WriteFile(hChildStdinWr, "san policy = onlineall", sizeof("san policy = onlineall"), &dwWritten, NULL);
	}
	else if (cmd == 2)//san policy = offlineshared
	{
		WriteFile(hChildStdinWr, "san policy = offlineshared", sizeof("san policy = offlineshared"), &dwWritten, NULL);
	}
	else if (cmd == 3)//san policy = offlineall
	{
		WriteFile(hChildStdinWr, "san policy = offlineall", sizeof("san policy = offlineall"), &dwWritten, NULL);
	}
	else
	{
		cout << "undefined operation! " << endl;
	}

	WriteFile(hChildStdinWr, "exit", sizeof("exit"), &dwWritten, NULL);

	if (hChildStdinRd != NULL)
	{
		CloseHandle(hChildStdinRd);
	}
	if (hChildStdinWr != NULL)
	{
		CloseHandle(hChildStdinWr);
	}
	if (hChildStdoutWr != NULL)
	{
		CloseHandle(hChildStdoutWr);
	}
	if (hChildStdoutRd != NULL)
	{
		CloseHandle(hChildStdoutRd);
	}
	return 0;
}

int main()
{
	/*int num, retcode;
	cout << "Please input cmd: " << endl;
	cout << "1.全部联机 2.共享磁盘脱机 3.全部脱机" << endl;
	cin >> num;*/
	int retcode;
	retcode = SharedDiskOperation(1);
	if (retcode == 0)
		cout << "Successfully operate disk! " << endl;
	else
		cout << "Failed operate disk! " << endl;
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值