PTSTR pszEnvironmentName = NULL;
PTSTR pszEnvironmentValue = NULL;
DWORD dwValueLength = 0;
pszEnvironmentName = (PTSTR)malloc(MAX_PATH * sizeof(TCHAR));
GetDlgItemText(hDlg, IDD_EDIT_NAME, pszEnvironmentName, MAX_PATH);
pszEnvironmentValue = (PTSTR)malloc(1024 * sizeof(TCHAR) * 3);
GetDlgItemText(hDlg, IDD_EDIT_VALUE, pszEnvironmentValue,
1024 * sizeof(TCHAR) * 3);
if (SetEnvironmentVariable(pszEnvironmentName, pszEnvironmentValue))
{
MessageBox(hDlg, TEXT("修改成功!"), TEXT("成功"), MB_OK);
}
else
{
MessageBox(hDlg, TEXT("修改失敗!"), TEXT("失敗"), MB_OK);
}
//釋放分配的內存
free(pszEnvironmentValue);
free(pszEnvironmentName);
參考:http://www.cnblogs.com/runliuv/p/3489427.html