vmwinst.c

来自「一个类似windows」· C语言 代码 · 共 1,250 行 · 第 1/3 页

C
1,250
字号
    return FALSE;
  }
  Size = SizeofResource(hAppInstance, VmwareInfResource);
  if (0 == Size)
  {
    return FALSE;
  }
  VmwareInfMem = LoadResource(hAppInstance, VmwareInfResource);
  if (NULL == VmwareInfMem)
  {
    return FALSE;
  }
  VmwareInfLocked = LockResource(VmwareInfMem);
  if (NULL == VmwareInfLocked)
  {
    return FALSE;
  }

  BufferSize = GetTempPathW(sizeof(TempPath) / sizeof(TempPath[0]), TempPath);
  if (0 == BufferSize || sizeof(TempPath) / sizeof(TempPath[0]) < BufferSize)
  {
    return FALSE;
  }
  if (0 == GetTempFileNameW(TempPath, L"vmx", 0, TempFileName))
  {
    return FALSE;
  }

  TempFile = CreateFileW(TempFileName, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL, NULL);
  if (INVALID_HANDLE_VALUE == TempFile)
  {
    DeleteFile(TempFileName);
    return FALSE;
  }
  if (! WriteFile(TempFile, VmwareInfLocked, Size, &Written, NULL) ||
      Written != Size)
  {
    CloseHandle(TempFile);
    DeleteFile(TempFileName);
    return FALSE;
  }
  CloseHandle(TempFile);

  wcscpy(CmdLine, L"DefaultInstall 128 ");
  wcscat(CmdLine, TempFileName);
  InstallHinfSectionW(NULL, NULL, CmdLine, 0);

  DeleteFile(TempFileName);

  return TRUE;
}

/* GUI */

void
InitPropSheetPage(PROPSHEETPAGE *psp, WORD idDlg, DWORD Flags, DLGPROC DlgProc)
{
  ZeroMemory(psp, sizeof(PROPSHEETPAGE));
  psp->dwSize = sizeof(PROPSHEETPAGE);
  psp->dwFlags = PSP_DEFAULT | Flags;
  psp->hInstance = hAppInstance;
  psp->pszTemplate = MAKEINTRESOURCE(idDlg);
  psp->pfnDlgProc = DlgProc;
}

/* Property page dialog callback */
INT_PTR CALLBACK
PageWelcomeProc(
  HWND hwndDlg,
  UINT uMsg,
  WPARAM wParam,
  LPARAM lParam
)
{
  LPNMHDR pnmh = (LPNMHDR)lParam;
  switch(uMsg)
  {
    case WM_NOTIFY:
    {
      HWND hwndControl;

      /* Center the wizard window */
      hwndControl = GetParent(hwndDlg);
      CenterWindow (hwndControl);

      switch(pnmh->code)
      {
        case PSN_SETACTIVE:
        {
          PropSheet_SetWizButtons(GetParent(hwndDlg), PSWIZB_NEXT);
          break;
        }
        case PSN_WIZNEXT:
        {
          if(DriverFilesFound)
          {
            if(!AddVmwareRegistryEntries())
            {
              WCHAR Msg[1024];
              LoadString(hAppInstance, IDS_FAILEDTOADDREGENTRIES, Msg, sizeof(Msg) / sizeof(WCHAR));
              MessageBox(GetParent(hwndDlg), Msg, NULL, MB_ICONWARNING);
              SetWindowLong(hwndDlg, DWL_MSGRESULT, IDD_WELCOMEPAGE);
              return TRUE;
            }
            if(!EnableVmwareDriver(TRUE, TRUE, TRUE))
            {

              WCHAR Msg[1024];
              LoadString(hAppInstance, IDS_FAILEDTOACTIVATEDRIVER, Msg, sizeof(Msg) / sizeof(WCHAR));
              MessageBox(GetParent(hwndDlg), Msg, NULL, MB_ICONWARNING);
              SetWindowLong(hwndDlg, DWL_MSGRESULT, IDD_WELCOMEPAGE);
              return TRUE;
            }
            SetWindowLong(hwndDlg, DWL_MSGRESULT, IDD_CONFIG);
            return TRUE;
          }
          break;
        }
      }
      break;
    }
  }
  return FALSE;
}

/* Property page dialog callback */
INT_PTR CALLBACK
PageInsertDiscProc(
  HWND hwndDlg,
  UINT uMsg,
  WPARAM wParam,
  LPARAM lParam
)
{
  switch(uMsg)
  {
    case WM_NOTIFY:
    {
      LPNMHDR pnmh = (LPNMHDR)lParam;
      switch(pnmh->code)
      {
        case PSN_SETACTIVE:
          PropSheet_SetWizButtons(GetParent(hwndDlg), PSWIZB_BACK | PSWIZB_NEXT);
          break;
        case PSN_WIZNEXT:
          SetWindowLong(hwndDlg, DWL_MSGRESULT, IDD_INSTALLING_VMWARE_TOOLS);
          break;
      }
      break;
    }
  }
  return FALSE;
}

VOID
InstTerminateInstaller(BOOL Wait)
{
  if(hInstallationThread != NULL)
  {
    if(Wait)
    {
      InterlockedExchange((LONG*)&AbortInstall, 2);
      WaitForSingleObject(hInstallationThread, INFINITE);
    }
    else
    {
      InterlockedExchange((LONG*)&AbortInstall, 1);
    }
  }
}

DWORD STDCALL
InstInstallationThread(LPVOID lpParameter)
{
  HANDLE hThread;
  BOOL DriveAvailable;
  int DrivesTested = 0;

  if(AbortInstall != 0) goto done;
  PostMessage(hInstallationNotifyWnd, WM_INSTSTATUSUPDATE, IDS_SEARCHINGFORCDROM, 0);

  while(AbortInstall == 0)
  {
    Sleep(500);
    DriveAvailable = IsVMwareCDInDrive(&CDDrive);
    if(DriveAvailable)
      break;
    if(DrivesTested++ > 20)
    {
      PostMessage(hInstallationNotifyWnd, WM_INSTABORT, IDS_FAILEDTOLOCATEDRIVERS, 0);
      goto cleanup;
    }
  }

  if(AbortInstall != 0) goto done;
  PostMessage(hInstallationNotifyWnd, WM_INSTSTATUSUPDATE, IDS_COPYINGFILES, 0);

  if(AbortInstall != 0) goto done;
  if(!InstallFile(DestinationPath, vmx_fb))
  {
    PostMessage(hInstallationNotifyWnd, WM_INSTABORT, IDS_FAILEDTOCOPYFILES, 0);
    goto cleanup;
  }

  Sleep(250);

  if(AbortInstall != 0) goto done;
  if(!InstallFile(DestinationPath, vmx_mode))
  {
    PostMessage(hInstallationNotifyWnd, WM_INSTABORT, IDS_FAILEDTOCOPYFILES, 0);
    goto cleanup;
  }

  Sleep(250);

  if(AbortInstall != 0) goto done;
  if(!InstallFile(DestinationDriversPath, vmx_svga))
  {
    PostMessage(hInstallationNotifyWnd, WM_INSTABORT, IDS_FAILEDTOCOPYFILES, 0);
    goto cleanup;
  }

  Sleep(250);

  if(AbortInstall != 0) goto done;
  PostMessage(hInstallationNotifyWnd, WM_INSTSTATUSUPDATE, IDS_ENABLINGDRIVER, 0);
  if(!AddVmwareRegistryEntries())
  {
    PostMessage(hInstallationNotifyWnd, WM_INSTABORT, IDS_FAILEDTOADDREGENTRIES, 0);
    goto cleanup;
  }
  if(!EnableVmwareDriver(TRUE, TRUE, TRUE))
  {
    PostMessage(hInstallationNotifyWnd, WM_INSTABORT, IDS_FAILEDTOACTIVATEDRIVER, 0);
    goto cleanup;
  }

  Sleep(500);

done:
  switch(AbortInstall)
  {
    case 0:
      SendMessage(hInstallationNotifyWnd, WM_INSTCOMPLETE, 0, 0);
      break;
    case 1:
      SendMessage(hInstallationNotifyWnd, WM_INSTABORT, 0, 0);
      break;
  }

cleanup:
  hThread = (HANDLE)InterlockedExchange((LONG*)&hInstallationThread, 0);
  if(hThread != NULL)
  {
    CloseHandle(hThread);
  }
  return 0;
}

BOOL
InstStartInstallationThread(HWND hwndNotify)
{
  if(hInstallationThread == NULL)
  {
    DWORD ThreadId;
    hInstallationNotifyWnd = hwndNotify;
    AbortInstall = 0;
    hInstallationThread = CreateThread(NULL,
                                       0,
                                       InstInstallationThread,
                                       NULL,
                                       CREATE_SUSPENDED,
                                       &ThreadId);
    if(hInstallationThread == NULL)
    {
      return FALSE;
    }

    ResumeThread(hInstallationThread);
    return TRUE;
  }

  return FALSE;
}

/* Property page dialog callback */
INT_PTR CALLBACK
PageInstallingProc(
  HWND hwndDlg,
  UINT uMsg,
  WPARAM wParam,
  LPARAM lParam
)
{
  switch(uMsg)
  {
    case WM_NOTIFY:
    {
      LPNMHDR pnmh = (LPNMHDR)lParam;
      switch(pnmh->code)
      {
        case PSN_SETACTIVE:
          SetDlgItemText(hwndDlg, IDC_INSTALLINGSTATUS, NULL);
          SendDlgItemMessage(hwndDlg, IDC_INSTALLINGPROGRESS, PBM_SETMARQUEE, TRUE, 50);
          PropSheet_SetWizButtons(GetParent(hwndDlg), PSWIZB_BACK);
          InstStartInstallationThread(hwndDlg);
          break;
        case PSN_RESET:
          InstTerminateInstaller(TRUE);
          break;
        case PSN_WIZBACK:
          if(hInstallationThread != NULL)
          {
            PropSheet_SetWizButtons(GetParent(hwndDlg), 0);
            InstTerminateInstaller(FALSE);
            SetWindowLong(hwndDlg, DWL_MSGRESULT, -1);
            return -1;
          }
          else
          {
            SendDlgItemMessage(hwndDlg, IDC_INSTALLINGPROGRESS, PBM_SETMARQUEE, FALSE, 0);
            SetWindowLong(hwndDlg, DWL_MSGRESULT, IDD_INSERT_VMWARE_TOOLS);
          }
          break;
      }
      break;
    }
    case WM_INSTABORT:
      /* go back in case we aborted the installation thread */
      SendDlgItemMessage(hwndDlg, IDC_INSTALLINGPROGRESS, PBM_SETMARQUEE, FALSE, 0);
      PropSheet_SetCurSelByID(GetParent(hwndDlg), IDD_INSERT_VMWARE_TOOLS);
      if(wParam != 0)
      {
        WCHAR Msg[1024];
        LoadString(hAppInstance, wParam, Msg, sizeof(Msg) / sizeof(WCHAR));
        MessageBox(GetParent(hwndDlg), Msg, NULL, MB_ICONWARNING);
      }
      break;
    case WM_INSTCOMPLETE:
      SendDlgItemMessage(hwndDlg, IDC_INSTALLINGPROGRESS, PBM_SETMARQUEE, FALSE, 0);
      PropSheet_SetWizButtons(GetParent(hwndDlg), PSWIZB_BACK | PSWIZB_NEXT);
      PropSheet_SetCurSelByID(GetParent(hwndDlg), IDD_CONFIG);
      break;
    case WM_INSTSTATUSUPDATE:
    {
      WCHAR Msg[1024];
      LoadString(hAppInstance, wParam, Msg, sizeof(Msg) / sizeof(WCHAR));
      SetDlgItemText(hwndDlg, IDC_INSTALLINGSTATUS, Msg);
      break;
    }
  }
  return FALSE;
}

/* Property page dialog callback */
INT_PTR CALLBACK
PageInstallFailedProc(
  HWND hwndDlg,
  UINT uMsg,
  WPARAM wParam,
  LPARAM lParam
)
{
  switch(uMsg)
  {
    case WM_NOTIFY:
    {
      LPNMHDR pnmh = (LPNMHDR)lParam;
      switch(pnmh->code)
      {
        case PSN_SETACTIVE:
          PropSheet_SetWizButtons(GetParent(hwndDlg), PSWIZB_FINISH);
          break;
      }
      break;
    }
  }
  return FALSE;
}

void
FillComboBox(HWND Dlg, int idComboBox, int From, int To)
{
  int i;
  WCHAR Text[256];

  for(i = From; i <= To; i++)
  {
    if(LoadString(hAppInstance, i, Text, 255) > 0)
    {
      SendDlgItemMessage(Dlg, idComboBox, CB_ADDSTRING, 0, (LPARAM)Text);
    }
  }
}

typedef struct
{
  int ControlID;
  int ResX;
  int ResY;
} MAPCTLRES;

/* Property page dialog callback */
INT_PTR CALLBACK
PageConfigProc(
  HWND hwndDlg,
  UINT uMsg,
  WPARAM wParam,
  LPARAM lParam
)
{
  LPNMHDR pnmh = (LPNMHDR)lParam;
  switch(uMsg)
  {
    case WM_INITDIALOG:
    {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?