commit c98a0d37d989de6edfe870c301918f28e622f8fd Author: Simone Date: Wed Apr 29 14:04:59 2026 +0200 chore: initial commit (extracted from Launchers monorepo) Plugin: nsShellExecAsUser v1.0.0 Architectures: x86-ansi, x86-unicode, amd64-unicode License: zlib diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..55629c5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,24 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = space +indent_size = 4 + +[*.{yml,yaml,json}] +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false + +[*.{cmd,bat,ps1}] +end_of_line = crlf + +[*.{nsi,nsh,sln,vcxproj,filters}] +end_of_line = crlf + +[Makefile] +indent_style = tab diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..20b175b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,52 @@ +# Normalizzazione line endings +* text=auto eol=lf + +# File di codice +*.c text eol=lf +*.cpp text eol=lf +*.h text eol=lf +*.hpp text eol=lf +*.def text eol=lf +*.py text eol=lf +*.ps1 text eol=lf +*.cmd text eol=crlf +*.bat text eol=crlf + +# NSIS +*.nsi text eol=crlf +*.nsh text eol=crlf + +# Markdown / docs / config +*.md text eol=lf +*.yml text eol=lf +*.yaml text eol=lf +*.json text eol=lf +*.txt text eol=lf +*.xml text eol=lf + +# Visual Studio (CRLF richiesto) +*.sln text eol=crlf +*.vcxproj text eol=crlf merge=union +*.filters text eol=crlf merge=union +*.rc binary +*.rc2 binary + +# Binari +*.dll binary +*.exe binary +*.lib binary +*.obj binary +*.pdb binary +*.zip binary +*.7z binary +*.png binary +*.ico binary +*.gif binary +*.jpg binary +*.jpeg binary +*.bmp binary +*.pdf binary + +# Linguistic detection (GitHub Linguist / Gitea analogo) +*.nsi linguist-language=NSIS +*.nsh linguist-language=NSIS diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml new file mode 100644 index 0000000..7599f2a --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -0,0 +1,76 @@ +name: Bug Report +description: Segnala un bug riproducibile +title: "[Bug]: " +labels: ["bug", "triage"] +body: + - type: markdown + attributes: + value: | + Grazie per la segnalazione. Compila i campi sotto per aiutarci a riprodurre il problema. + + - type: input + id: plugin-version + attributes: + label: Versione plugin + description: Contenuto del file VERSION o tag della release scaricata + placeholder: "1.6.0" + validations: + required: true + + - type: dropdown + id: architecture + attributes: + label: Architettura DLL usata + options: + - x86-ansi + - x86-unicode + - amd64-unicode + - non so / multiple + validations: + required: true + + - type: input + id: nsis-version + attributes: + label: Versione NSIS + placeholder: "3.10" + validations: + required: true + + - type: input + id: os + attributes: + label: Sistema operativo + placeholder: "Windows 11 23H2 x64" + validations: + required: true + + - type: textarea + id: repro + attributes: + label: Passi per riprodurre + description: Script .nsi minimale che riproduce il problema + render: nsis + validations: + required: true + + - type: textarea + id: expected + attributes: + label: Comportamento atteso + validations: + required: true + + - type: textarea + id: actual + attributes: + label: Comportamento osservato + description: Includi messaggi di errore esatti, output del log NSIS, screenshot + validations: + required: true + + - type: textarea + id: additional + attributes: + label: Note aggiuntive + description: Qualunque altra informazione utile (workaround tentati, ecc.) diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..10d50a4 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,8 @@ +blank_issues_enabled: false +contact_links: + - name: Domande generali + url: https://gitea.emulab.it/Simone/nsis-plugin-nsshellexecasuser/issues?type=question + about: Per domande di utilizzo che non sono bug, apri una issue con label "question" + - name: Security + url: https://gitea.emulab.it/Simone/nsis-plugin-nsshellexecasuser/security/advisories/new + about: Segnala vulnerabilità di security in privato (non aprire issue pubbliche) diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml new file mode 100644 index 0000000..fae6d8d --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.yml @@ -0,0 +1,32 @@ +name: Feature Request +description: Suggerisci una nuova funzionalità o miglioramento +title: "[Feature]: " +labels: ["enhancement"] +body: + - type: textarea + id: problem + attributes: + label: Problema / Caso d'uso + description: Quale problema vorresti risolvere? Quale workflow vuoi abilitare? + validations: + required: true + + - type: textarea + id: proposal + attributes: + label: Proposta + description: Come dovrebbe funzionare? Includi esempio di API/uso negli script NSIS se possibile. + render: nsis + validations: + required: true + + - type: textarea + id: alternatives + attributes: + label: Alternative considerate + description: Altri approcci che hai valutato e perché sono stati scartati + + - type: textarea + id: additional + attributes: + label: Note aggiuntive diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..3758b50 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,31 @@ +## Descrizione + + + +## Tipo di Modifica + +- [ ] Bug fix (modifica non breaking che risolve un problema) +- [ ] Feature (modifica non breaking che aggiunge funzionalità) +- [ ] Breaking change (fix o feature che rompe la compatibilità) +- [ ] Documentazione +- [ ] Build / CI + +## Architetture Testate + +- [ ] `x86-ansi` +- [ ] `x86-unicode` +- [ ] `amd64-unicode` + +## Checklist + +- [ ] Build completa senza warning: `python build_plugin.py` +- [ ] CHANGELOG.md aggiornato (sezione `[Unreleased]`) +- [ ] README / docs aggiornati se l'API è cambiata +- [ ] Esempi aggiornati se l'API è cambiata +- [ ] Conventional Commits rispettato +- [ ] Nessun file in `dist/` o `__pycache__/` committato +- [ ] Self-review completata + +## Note Aggiuntive + + diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..a521284 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,63 @@ +# Build (GitHub) — pipeline CI primaria +# +# Strategia hosting: +# - Repo primario su Gitea (push iniziale) +# - Push mirror automatico Gitea -> GitHub +# - GitHub esegue TUTTA la CI (Gitea non esegue Actions) +# +# Trigger: +# - push su main (incluso quello propagato dal mirror Gitea) +# - PR aperte su GitHub +# - run manuale via UI + +name: Build + +on: + push: + branches: [main] + paths-ignore: + - '**.md' + - 'docs/**' + - 'examples/**' + pull_request: + branches: [main] + workflow_dispatch: + +jobs: + build: + runs-on: windows-latest + strategy: + fail-fast: false + matrix: + config: [x86-ansi, x86-unicode, x64-unicode] + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Setup MSBuild + uses: microsoft/setup-msbuild@v2 + + - name: Read VERSION + id: version + shell: pwsh + run: | + $v = (Get-Content VERSION -Raw).Trim() + "version=$v" | Out-File -FilePath $env:GITHUB_OUTPUT -Append + + - name: Build ${{ matrix.config }} + run: python build_plugin.py --config ${{ matrix.config }} --verbose + + - name: Upload DLL + uses: actions/upload-artifact@v4 + with: + name: ${{ github.event.repository.name }}-${{ steps.version.outputs.version }}-${{ matrix.config }} + path: dist/** + if-no-files-found: error + retention-days: 30 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..e4cf67d --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,99 @@ +# Release (GitHub) — pipeline release primaria +# +# Triggerata quando un tag v* viene propagato dal mirror Gitea. +# Costruisce gli artifact per tutte le architetture e crea la GitHub Release. + +name: Release + +on: + push: + tags: ['v*'] + workflow_dispatch: + inputs: + tag: + description: 'Tag versione (es. v1.7.0)' + required: true + +permissions: + contents: write + +jobs: + build: + runs-on: windows-latest + strategy: + fail-fast: true + matrix: + config: [x86-ansi, x86-unicode, x64-unicode] + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + + - uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - uses: microsoft/setup-msbuild@v2 + + - name: Build ${{ matrix.config }} + run: python build_plugin.py --config ${{ matrix.config }} + + - name: Determine version & arch + id: meta + shell: pwsh + run: | + $tag = "${{ github.ref_name }}" + if ([string]::IsNullOrEmpty($tag) -or $tag -eq "main") { $tag = "${{ inputs.tag }}" } + $v = $tag -replace '^v','' + $map = @{ 'x86-ansi' = 'x86-ansi'; 'x86-unicode' = 'x86-unicode'; 'x64-unicode' = 'amd64-unicode' } + $dir = $map['${{ matrix.config }}'] + echo "version=$v" >> $env:GITHUB_OUTPUT + echo "tag=$tag" >> $env:GITHUB_OUTPUT + echo "dir=$dir" >> $env:GITHUB_OUTPUT + + - name: Package + shell: pwsh + run: | + $name = "${{ github.event.repository.name }}-${{ steps.meta.outputs.version }}-${{ steps.meta.outputs.dir }}" + $stage = "stage/$name" + New-Item -ItemType Directory -Force -Path $stage | Out-Null + Copy-Item "dist/${{ steps.meta.outputs.dir }}/*" $stage -Recurse + Copy-Item README.md, LICENSE, VERSION, CHANGELOG.md $stage + if (Test-Path "include") { Copy-Item "include" $stage -Recurse } + if (Test-Path "examples") { Copy-Item "examples" $stage -Recurse } + Compress-Archive -Path "$stage/*" -DestinationPath "$name.zip" -Force + + - uses: actions/upload-artifact@v4 + with: + name: pkg-${{ matrix.config }} + path: '*.zip' + + publish: + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/download-artifact@v4 + with: + path: artifacts + merge-multiple: true + + - name: SHA256SUMS + run: | + cd artifacts + sha256sum *.zip > SHA256SUMS.txt + cat SHA256SUMS.txt + + - name: Create GitHub Release + uses: softprops/action-gh-release@v2 + with: + files: | + artifacts/*.zip + artifacts/SHA256SUMS.txt + generate_release_notes: true + body: | + **Repository primario**: vedi link Gitea nel README. + + Il mirror GitHub è generato automaticamente dal repo Gitea (sorgente di verità). + Per issue e PR aprire ticket sul repository primario Gitea. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c3a730 --- /dev/null +++ b/.gitignore @@ -0,0 +1,63 @@ +# Build output (src/ subdirs only — dist/ is tracked, contains pre-built DLLs) +bin/ +obj/ +Build/ +build/ + +# Visual Studio +*.user +*.suo +*.ncb +*.sdf +.vs/ +ipch/ +*.VC.db +*.VC.VC.opendb +*.opendb +*.aps +*.tlog +*.idb +*.pdb +*.ilk +*.exp +*.lib +*.obj + +# Python +__pycache__/ +*.pyc +*.pyo +.pytest_cache/ +*.egg-info/ +.venv/ +venv/ + +# Editor / IDE +.vscode/ +.idea/ +*.swp +*.swo +*~ + +# OS +Thumbs.db +desktop.ini +.DS_Store + +# NSIS +*.nsh.bak + +# Logs +*.log +.logs/ + +# Sensitive +.env +.env.local +*.pem +*.key + +# Migration/automation scripts (temporary, contain tokens — never commit) +tools/t*.py +tools/check_*.py +tools/legacy/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..f0cbdfd --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog + +Tutte le modifiche rilevanti a questo progetto sono documentate in questo file. + +Il formato segue [Keep a Changelog](https://keepachangelog.com/it/1.0.0/), +e il progetto aderisce al [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [1.0.0] — 2026-04-29 + +### Added +- Prima release come repository indipendente +- Build script unificato (`build_plugin.py`) con CLI canonica +- CI/CD via GitHub Actions (mirror automatico Gitea → GitHub) +- Documentazione completa (README, CONTRIBUTING, SECURITY) + +[Unreleased]: https://gitea.emulab.it/Simone/nsis-plugin-nsshellexecasuser/compare/v1.0.0...HEAD +[1.0.0]: https://gitea.emulab.it/Simone/nsis-plugin-nsshellexecasuser/releases/tag/v1.0.0 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..04031b0 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,137 @@ +# Contribuire a nsShellExecAsUser + +Grazie per il tuo interesse a contribuire! + +> Repository primario: [https://gitea.emulab.it/Simone/nsis-plugin-nsshellexecasuser](https://gitea.emulab.it/Simone/nsis-plugin-nsshellexecasuser) +> Mirror GitHub: [https://github.com/systempal/nsis-plugin-nsshellexecasuser](https://github.com/systempal/nsis-plugin-nsshellexecasuser) (read-only — apri PR su Gitea) + +## Prerequisiti + +| Strumento | Versione | Note | +|-----------|----------|------| +| Visual Studio | 2022 / 2026 | Workload: Desktop development with C++ (toolset v143/v145) | +| Python | 3.7+ | Per `build_plugin.py` | +| Git | 2.30+ | Con supporto submoduli se applicabile | + +## Quick Start + +```powershell +git clone https://gitea.emulab.it/Simone/nsis-plugin-nsshellexecasuser.git +cd nsis-plugin-nsshellexecasuser +python build_plugin.py +``` + +Output in `dist/{x86-ansi,x86-unicode,amd64-unicode}/`. + +## Workflow Sviluppo + +### 1. Branch + +```powershell +git checkout -b feat/nome-feature # nuova feature +git checkout -b fix/descrizione-bug # bugfix +git checkout -b docs/aggiornamento # solo doc +git checkout -b chore/manutenzione # build, dipendenze +``` + +### 2. Commit + +Segui [Conventional Commits](https://www.conventionalcommits.org/it/v1.0.0/): + +``` +feat: aggiunge supporto per formato XYZ +fix: corregge crash su Windows 11 ARM +docs: aggiorna esempio di utilizzo +chore: aggiorna toolset MSVC +refactor: estrae logica parsing in funzione separata +test: aggiunge test per edge case +build: aggiorna script di build +ci: aggiorna workflow GitHub Actions +``` + +Breaking changes: + +``` +feat!: rinomina API Foo() in Bar() + +BREAKING CHANGE: Foo() rinominata in Bar(); aggiornare tutti i chiamanti. +``` + +### 3. Pull Request + +PR aperte verso il branch `main` su Gitea. Verifica: + +- [ ] Build completa senza warning per tutte le architetture: `python build_plugin.py` +- [ ] Esempi aggiornati se cambia l'API +- [ ] `CHANGELOG.md` aggiornato (sezione `[Unreleased]`) +- [ ] Documentazione aggiornata (README, docs/API.md se esiste) +- [ ] Conventional Commits rispettato +- [ ] Nessun file in `dist/` o `__pycache__/` committato + +## Build Script + +``` +python build_plugin.py [opzioni] +``` + +| Opzione | Default | Descrizione | +|---------|---------|-------------| +| `--config` | `all` | Architettura: `x86-ansi`, `x86-unicode`, `x64-unicode`, `all` | +| `--toolset` | `auto` | Visual Studio: `2022`, `2026`, `auto` (rileva il più recente) | +| `--jobs` | numero CPU | Thread paralleli per la build | +| `--clean` | false | Pulisce `dist/` prima di compilare | +| `--install-dir` | none | Copia DLL anche in `/{arch}/` (es. NSIS plugin folder) | +| `--verbose` | false | Output dettagliato (mostra comandi MSBuild) | + +Exit codes: +- `0` — Successo +- `1` — Errore di build +- `2` — Prerequisiti mancanti +- `3` — Argomenti invalidi + +## Release Workflow + +Le release sono automatiche via tag Git: + +```powershell +# 1. Aggiorna VERSION e CHANGELOG.md +echo "1.7.0" > VERSION +# (modifica CHANGELOG.md spostando entry da [Unreleased] a [1.7.0]) + +# 2. Commit +git add VERSION CHANGELOG.md +git commit -m "chore(release): v1.7.0" + +# 3. Tag annotato +git tag -a v1.7.0 -m "Release v1.7.0" + +# 4. Push +git push origin main +git push origin v1.7.0 +``` + +Il workflow `.github/workflows/release.yml` compilerà tutte le architetture +e creerà la release su Gitea con gli artifact ZIP allegati. Il mirror GitHub +riceverà automaticamente il tag. + +## Code Style + +- C/C++: stile coerente con il file modificato +- Python: PEP 8 (4 spazi indent, line length flessibile) +- Header file: `#include` ordinati: stdlib → Win32 → progetto +- Encoding: UTF-8 senza BOM (eccetto `.rc` che richiede UTF-16 LE) +- Line endings: LF in repo (`.gitattributes` normalizza) + +## Reporting Bug + +Apri una [issue](https://gitea.emulab.it/Simone/nsis-plugin-nsshellexecasuser/issues/new) usando il template "Bug Report". +Includi: +- Versione plugin e architettura DLL usata +- Versione NSIS +- Sistema operativo +- Script `.nsi` minimale che riproduce il problema +- Output / errore esatto + +## Domande + +Per domande generali, apri una [issue](https://gitea.emulab.it/Simone/nsis-plugin-nsshellexecasuser/issues) con label `question`. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bc3fdc5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +zlib License + +Copyright (c) 2026 Simone + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source distribution. diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f27968 --- /dev/null +++ b/README.md @@ -0,0 +1,100 @@ +# nsShellExecAsUser NSIS Plugin + +**Versione personale modificata** + +--- + +## Descrizione Originale + +**ShellExecAsUser plug-in** + +Execute the specified program using ShellExecute. +Useful when the installer process is running with elevated privileges (UAC) but you want to launch an application as the standard logged-in user (e.g., launching the application after installation). + +## Features + +- Launches applications as the current desktop user +- Bypasses UAC elevation if the installer is running as Administrator +- Supports standard ShellExecute verbs (open, runas, print, etc.) +- Supports window show modes (SW_SHOWNORMAL, SW_HIDE, etc.) +- Win95/98/ME/NT/2000/XP/Vista/7/8/10/11 support +- NSIS UNICODE support + +## Usage + +```nsis +ShellExecAsUser::ShellExecAsUser "action" "command" "parameters" "show_mode" +``` + +**Parameters:** + +1. `action` - The verb to use (e.g., "open", "runas", "print"). Can be empty `""` for default action. +2. `command` - Path to the executable or file to open. +3. `parameters` - Command line arguments. +4. `show_mode` - Window display mode. + +**Supported Show Modes:** + +- `SW_SHOWDEFAULT` +- `SW_SHOWNORMAL` (default) +- `SW_SHOWMAXIMIZED` +- `SW_SHOWMINIMIZED` +- `SW_HIDE` + +**Working Directory:** + +The plugin uses the current NSIS output directory (`$OUTDIR`) as the working directory for the launched process. + +## Example + +```nsis +Section "Launch Application" + SetOutPath "$INSTDIR" + ShellExecAsUser::ShellExecAsUser "open" "$INSTDIR\MyApp.exe" "" "SW_SHOWNORMAL" +SectionEnd +``` + +To launch a URL in the default browser as the user: +```nsis +ShellExecAsUser::ShellExecAsUser "open" "https://example.com" "" "SW_SHOWNORMAL" +``` + +## Include file + +There is no wrapper macros file for this plugin. Call the plugin directly as shown in the usage section. + +--- + +## ⚠️ Differenze nella versione personale + +### Architetture supportate + +Il progetto supporta le configurazioni già presenti nell'originale: +- **amd64-unicode** (x64) +- **x86-ansi** +- **x86-unicode** + +### Progetto Visual Studio + +Il progetto è stato aggiornato e riorganizzato: +- `Contrib/ShellExecAsUser/ShellExecAsUser.sln` - Solution VS2022 +- `Contrib/ShellExecAsUser/ShellExecAsUser.vcxproj` - Progetto VS2022 aggiornato + +### File aggiunti + +- `build_plugin.py` - Script Python per compilare il plugin per tutte le architetture supportate. +- `nsis/` - Header NSIS inclusi per facilitare la compilazione. + +### Modifiche al Codice + +- **Rimozione Warning Deprecazione**: Sostituita la chiamata deprecata `GetVersionEx` in `VistaTools.cxx` con la moderna API `VerifyVersionInfo` per evitare warning C4996 durante la compilazione con i toolset moderni. +- **Aggiornamento Build**: Configurazione aggiornata per utilizzare il toolset **v143** (Visual Studio 2022). + +### Compilazione + +```cmd +cd nsShellExecAsUser +python build_plugin.py +``` + +I DLL vengono copiati in `plugins/{platform}/ShellExecAsUser.dll`. diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..c1a4100 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,40 @@ +# Security Policy + +## Versioni Supportate + +Solo l'ultima versione minor riceve fix di security. + +| Versione | Supporto | +|----------|----------| +| Latest minor (es. 1.0.0) | ✅ | +| Versioni precedenti | ❌ | + +## Reporting di una Vulnerabilità + +**Non aprire issue pubbliche per vulnerabilità di security.** + +Per segnalare una vulnerabilità: + +1. **Preferito**: usa la funzione [Security Advisory privato](https://gitea.emulab.it/Simone/nsis-plugin-nsshellexecasuser/security/advisories/new) di Gitea +2. **Alternativa**: invia email a `security@example.com` (sostituire con email reale prima del primo release) + +Includi: +- Descrizione della vulnerabilità +- Passi per riprodurla +- Versione/i affette +- Possibile mitigazione (se nota) + +## Tempo di Risposta + +- Acknowledgment iniziale: entro 7 giorni +- Valutazione: entro 14 giorni +- Patch (se confermata): tempistiche dipendono dalla severità + +## Hardening + +Questo plugin gira con i permessi del processo NSIS chiamante (tipicamente +l'utente che esegue l'installer). Non richiede privilegi elevati per la +compilazione né per l'esecuzione, salvo dove esplicitamente documentato. + +DLL distribuite tramite Releases sono compilate da CI GitHub Actions con +toolchain pinnata. Verificare hash SHA256 pubblicati nelle release. diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..3eefcb9 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +1.0.0 diff --git a/build_plugin.py b/build_plugin.py new file mode 100644 index 0000000..8174b54 --- /dev/null +++ b/build_plugin.py @@ -0,0 +1,158 @@ +#!/usr/bin/env python3 +""" +Build script canonico per il plugin ShellExecAsUser. + +CLI standard: vedi `python build_plugin.py --help` +""" +from __future__ import annotations + +import argparse +import io +import os +import shutil +import subprocess +import sys +from pathlib import Path + +# Ensure UTF-8 output on Windows (avoids cp1252 UnicodeEncodeError) +if sys.stdout.encoding and sys.stdout.encoding.lower() != "utf-8": + sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace") + sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8", errors="replace") + +ROOT = Path(__file__).resolve().parent +SRC_DIR = ROOT / "src" +DIST_DIR = ROOT / "dist" +VERSION_FILE = ROOT / "VERSION" + +# Mapping CLI config -> (MSBuild Configuration, MSBuild Platform, dist subdir) +CONFIGS = { + "x86-ansi": ("Release", "Win32", "x86-ansi"), + "x86-unicode": ("Release Unicode", "Win32", "x86-unicode"), + "x64-unicode": ("Release Unicode", "x64", "amd64-unicode"), +} + +VCXPROJ = SRC_DIR / "ShellExecAsUser.vcxproj" +DLL_NAME = "ShellExecAsUser.dll" + + +def read_version() -> str: + # utf-8-sig strips BOM (\ufeff) automatically + return VERSION_FILE.read_text(encoding="utf-8-sig").strip() + + +def find_msbuild(toolset: str) -> Path: + """Localizza MSBuild via vswhere; toolset='2022', '2026', o 'auto'. + + In auto mode, tries 2022 before 2026 because most vcxproj files use + PlatformToolset v143 (VS2022) which may not be available in VS2026 build tools. + """ + pf86 = os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)") + vswhere = Path(pf86) / "Microsoft Visual Studio" / "Installer" / "vswhere.exe" + if not vswhere.exists(): + sys.exit(2) + + def _query(version_range: str | None) -> list[str]: + args = [str(vswhere), "-latest", "-products", "*", + "-requires", "Microsoft.Component.MSBuild", + "-find", r"MSBuild\**\Bin\MSBuild.exe"] + if version_range: + args += ["-version", version_range] + result = subprocess.run(args, capture_output=True, text=True) + return [ln.strip() for ln in result.stdout.splitlines() if ln.strip()] + + if toolset == "2022": + paths = _query("[17.0,18.0)") + elif toolset == "2026": + paths = _query("[18.0,19.0)") + else: # auto: prefer 2022 (v143) over 2026 (v145) + paths = _query("[17.0,18.0)") or _query("[18.0,19.0)") or _query(None) + + if not paths: + sys.exit(2) + return Path(paths[0]) + + +def build_one(msbuild: Path, config: str, platform: str, jobs: int, verbose: bool) -> bool: + cmd = [ + str(msbuild), str(VCXPROJ), + f"/p:Configuration={config}", + f"/p:Platform={platform}", + f"/m:{jobs}", + "/nologo", + "/v:" + ("normal" if verbose else "minimal"), + ] + print(f"[build] {config} | {platform}") + return subprocess.run(cmd).returncode == 0 + + +def collect_dll(config: str, platform: str, dist_subdir: str) -> Path | None: + """Trova la DLL prodotta dal vcxproj e la copia in dist//.""" + plat_dir = "" if platform == "Win32" else platform + "/" + candidates = list(SRC_DIR.glob(f"**/{plat_dir}{config}/{DLL_NAME}")) + candidates += list(ROOT.glob(f"**/{plat_dir}{config}/{DLL_NAME}")) + if not candidates: + return None + src = candidates[0] + dst_dir = DIST_DIR / dist_subdir + dst_dir.mkdir(parents=True, exist_ok=True) + dst = dst_dir / DLL_NAME + shutil.copy2(src, dst) + return dst + + +def main() -> int: + parser = argparse.ArgumentParser(description=f"Build ShellExecAsUser plugin") + parser.add_argument("--config", default="all", + choices=list(CONFIGS.keys()) + ["all"]) + parser.add_argument("--toolset", default="auto", choices=["2022", "2026", "auto"]) + parser.add_argument("--jobs", type=int, default=os.cpu_count() or 4) + parser.add_argument("--clean", action="store_true") + parser.add_argument("--install-dir", type=Path, default=None) + parser.add_argument("--verbose", action="store_true") + parser.add_argument("--version", action="store_true", + help="Mostra versione e termina") + args = parser.parse_args() + + if args.version: + print(read_version()) + return 0 + + if not VCXPROJ.exists(): + print(f"ERROR: {VCXPROJ} not found", file=sys.stderr) + return 3 + + if args.clean and DIST_DIR.exists(): + shutil.rmtree(DIST_DIR) + + msbuild = find_msbuild(args.toolset) + print(f"[info] MSBuild: {msbuild}") + print(f"[info] Version: {read_version()}") + + targets = list(CONFIGS.keys()) if args.config == "all" else [args.config] + failed = [] + for cfg_name in targets: + config, platform, subdir = CONFIGS[cfg_name] + if not build_one(msbuild, config, platform, args.jobs, args.verbose): + failed.append(cfg_name) + continue + dst = collect_dll(config, platform, subdir) + if dst is None: + print(f"ERROR: DLL not found for {cfg_name}", file=sys.stderr) + failed.append(cfg_name) + else: + print(f"[ok] {dst}") + if args.install_dir: + install_path = args.install_dir / subdir + install_path.mkdir(parents=True, exist_ok=True) + shutil.copy2(dst, install_path / DLL_NAME) + print(f"[install] {install_path / DLL_NAME}") + + if failed: + print(f"FAILED: {failed}", file=sys.stderr) + return 1 + print("[done] all targets built successfully") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/dist/amd64-unicode/ShellExecAsUser.dll b/dist/amd64-unicode/ShellExecAsUser.dll new file mode 100644 index 0000000..87671cd Binary files /dev/null and b/dist/amd64-unicode/ShellExecAsUser.dll differ diff --git a/dist/x86-ansi/ShellExecAsUser.dll b/dist/x86-ansi/ShellExecAsUser.dll new file mode 100644 index 0000000..c8229f1 Binary files /dev/null and b/dist/x86-ansi/ShellExecAsUser.dll differ diff --git a/dist/x86-unicode/ShellExecAsUser.dll b/dist/x86-unicode/ShellExecAsUser.dll new file mode 100644 index 0000000..bfcc94b Binary files /dev/null and b/dist/x86-unicode/ShellExecAsUser.dll differ diff --git a/docs/ShellExecAsUser.txt b/docs/ShellExecAsUser.txt new file mode 100644 index 0000000..789a01e --- /dev/null +++ b/docs/ShellExecAsUser.txt @@ -0,0 +1,20 @@ +ShellExecAsUser plug-in +----------------------- +Execute the specified program using ShellExecute in non-admin context. +The ShellExecAsUser plug-in is intended for installers that run with admin rights, but need to execute something as a normal non-elevated process. +This is accomplished by having Explorer launch the application on the installers behalf (http://brandonlive.com/2008/04/27/getting-the-shell-to-run-an-application-for-you-part-2-how/). +As such it's a light alternative to UAC plugin. + +Usage +----- +Syntax is exactly the same as NSIS built in ExecShell command: +ShellExecAsUser::ShellExecAsUser action command [parameters] [SW_SHOWDEFAULT | SW_SHOWNORMAL | SW_SHOWMAXIMIZED | SW_SHOWMINIMIZED | SW_HIDE] + +Note that action is usually "open", "print", etc, but can be an empty string to use the default action. Parameters and the show type are optional. $OUTDIR is used for the working directory. +When installer process is non-elevated (e.g. executed on Windows XP, or UAC is disabled, etc), defaults to ShellExecute method. +Example: + ShellExecAsUser::ShellExecAsUser "open" 'http://www.google.com/' + +Release notes +--------------------------- +- Updated to build Ansi and Unicode versions, using Visual Studio 2010. To reduce the dependencies, dll is built with "Multi-threaded" (/MT) runtime library option. Unfortunately the size grew up to 44K (comparing to original 7K with Visual Studio 6). If you install Visual Studio redistributables with your installer, recompile the dll with "Multi-threaded dll" (/MD) option. It will reduce the size to 11K. diff --git a/examples/example.nsi b/examples/example.nsi new file mode 100644 index 0000000..822c357 --- /dev/null +++ b/examples/example.nsi @@ -0,0 +1,19 @@ +; ShellExecAsUser Example +; Executes notepad.exe as standard user from an elevated installer + +!include "LogicLib.nsh" + +; Add plugin dir if needed +; !addplugindir "plugins\x86-unicode" + +Name "ShellExecAsUser Example" +OutFile "example_output.exe" +RequestExecutionLevel admin +ShowInstDetails show + +Section "Test" + ; Run notepad as the logged-on user (even from elevated context) + ShellExecAsUser::ShellExecAsUser "open" "notepad.exe" "" + Pop $0 + DetailPrint "Return: $0" +SectionEnd diff --git a/src/ShellExecAsUser.cpp b/src/ShellExecAsUser.cpp new file mode 100644 index 0000000..37c4b5c --- /dev/null +++ b/src/ShellExecAsUser.cpp @@ -0,0 +1,272 @@ +// ShellExecAsUser.cpp : Defines the entry point for the DLL application. +// +#include +#include +#include +#include +#include +#include +//#include +#include "pluginapi.h" // nsis plugin +#include +//#pragma comment(linker,"/merge:.rdata=.text") + +//VistaTools +#define NO_DLL_IMPORTS 1 +#define DONTWANT_RunNonElevated 1 +#define IMPLEMENT_VISTA_TOOLS +#include "VistaTools.cxx" +////////////////////////////////////// + +//missing in old sdk, had to replace shlwapi.lib as well +//LWSTDAPI IUnknown_QueryService(__in IUnknown* punk, __in REFGUID guidService, __in REFIID riid, __deref_out void ** ppvOut); + + +HINSTANCE g_hInstance; +HWND g_hwndParent; +HWND g_hwnd; +HANDLE g_hThread; + +typedef struct _ExecShellParams { + TCHAR action[50]; + TCHAR command[_MAX_PATH]; + TCHAR params[1024]; + TCHAR directory[_MAX_PATH]; + int iShow; +} ExecShellParams; + +UINT_PTR NSISPluginCallback(enum NSPIM Event) +{ + switch(Event) + { + case NSPIM_UNLOAD: + if (g_hThread) { + //OutputDebugString("ExecShellAsUser: NSPIM_UNLOAD wait..."); + ::PostMessage(g_hwnd, WM_USER+11, 0, 0); + ::WaitForSingleObject(g_hThread, INFINITE); + ::CloseHandle(g_hThread); + //OutputDebugString("ExecShellAsUser: NSPIM_UNLOAD"); + } + break; + } + return NULL; +} + +//http://brandonlive.com/2008/04/27/getting-the-shell-to-run-an-application-for-you-part-2-how/ +static CComQIPtr retrieveDesktop() +{ + CComPtr psw; + HRESULT hr = psw.CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_LOCAL_SERVER); + if (SUCCEEDED(hr)) + { + HWND hwnd; + CComPtr pdisp; + VARIANT vEmpty; + vEmpty.vt = VT_EMPTY; + if (S_OK == (hr = psw->FindWindowSW(&vEmpty, &vEmpty, 0x8/*SWC_DESKTOP*/, (long*)&hwnd, SWFO_NEEDDISPATCH, &pdisp))) + { + CComPtr psb; + + hr = IUnknown_QueryService(pdisp, SID_STopLevelBrowser, __uuidof(IShellBrowser), (void**) &psb); //IID_PPV_ARGS(&psb) + if (SUCCEEDED(hr)) + { + CComPtr psv; + hr = psb->QueryActiveShellView(&psv); + + CComPtr pdispBackground; + HRESULT hr = psv->GetItemObject(SVGIO_BACKGROUND, __uuidof(IDispatch), (void**) &pdispBackground); + if (SUCCEEDED(hr)) + { + if (CComQIPtr psfvd = pdispBackground) + { + CComPtr pdisp; + hr = psfvd->get_Application(&pdisp); + if (SUCCEEDED(hr)) + { + return CComQIPtr(pdisp); + } + } + } + } + } else { + TCHAR buf[100]; + wsprintf(buf, _T("ShellExecAsUser: FindWindowSW failed: %x"), hr); + OutputDebugString(buf); + } + } + return CComQIPtr(); +} + +static int getShowMode(const TCHAR* s) +{ + if (s[0] == 0) return SW_SHOWNORMAL; + const TCHAR* strModes[] = { _T("SW_SHOWDEFAULT"), _T("SW_SHOWNORMAL"), _T("SW_SHOWMAXIMIZED"), _T("SW_SHOWMINIMIZED"), _T("SW_HIDE") }; + int modes[] = { SW_SHOWDEFAULT, SW_SHOWNORMAL, SW_SHOWMAXIMIZED, SW_SHOWMINIMIZED, SW_HIDE }; + for (int i=0; icommand); + CComVariant vAction, vParams, vDir; + if (params->action[0] != 0) vAction = params->action; + if (params->params[0] != 0) vParams = params->params; + if (params->directory[0] != 0) vDir = params->directory; + CComVariant vShow; + if (params->iShow != -1) vShow = params->iShow; +//#pragma push_macro("ShellExecute") +//#undef ShellExecute + HRESULT hr = psd->ShellExecute(command, vParams, vDir, vAction, vShow); +//#pragma pop_macro("ShellExecute") + return hr == S_OK; +} + +static LRESULT WndProc(HWND hWnd, UINT uMsg,WPARAM wParam, LPARAM lParam) +{ + switch(uMsg) + { + case WM_CREATE: + { + void* param = ((LPCREATESTRUCT)lParam)->lpCreateParams; + ::SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)param); + break; + } + case WM_USER+10: + { + ExecShellParams* p = (ExecShellParams*)lParam; + IShellDispatch2* pSD = (IShellDispatch2*)::GetWindowLongPtr(hWnd, GWLP_USERDATA); + runCommand(pSD, p); + delete p; + break; + } + case WM_USER+11: + DestroyWindow(hWnd); + break; + case WM_DESTROY: + { + PostQuitMessage(0); + break; + } + default: + return ::DefWindowProc(hWnd,uMsg,wParam,lParam); + }; + return 0; +} + +HWND CreateMessageWindow(void* data) +{ + WNDCLASSEX wincl; + ZeroMemory(&wincl, sizeof(WNDCLASSEX)); + wincl.hInstance = g_hInstance; + wincl.lpszClassName = _T("NSIS_DESKTOP_LAUNCH_CLASS"); + wincl.lpfnWndProc = (WNDPROC)WndProc; + wincl.cbSize = sizeof (WNDCLASSEX); + + ATOM atom = RegisterClassEx (&wincl); + + return CreateWindowEx( + 0, + MAKEINTATOM(atom), + _T(""), + 0, + 0, + 0, + 0, + 0, + HWND_MESSAGE, + NULL, + wincl.hInstance, + data); + +} + +static unsigned __stdcall WorkerThreadProc(void* pParam) +{ + HANDLE hRunEvent = (HANDLE) pParam; + ::CoInitialize(NULL); + { + CComPtr pSD = retrieveDesktop(); + OutputDebugString(pSD.p ? _T("ExecShellAsUser: got desktop") : _T("ExecShellAsUser: failed to retrieve desktop!")); + if (pSD.p) g_hwnd = CreateMessageWindow(pSD.p); + SetEvent(hRunEvent); + + if (g_hwnd) { + MSG msg; + while ( GetMessage(&msg, NULL, 0, 0) > 0 ) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + } + ::CoUninitialize(); + OutputDebugString(_T("ExecShellAsUser: thread finished")); + return 0; +} + +#define EMPTY_TO_NULL(s) (s[0] == 0 ? NULL : s) + +extern "C" +void __declspec(dllexport) ShellExecAsUser(HWND hwndParent, int string_size, + TCHAR *variables, stack_t **stacktop, + extra_parameters *extra) +{ + static int initDone=0; + + g_hwndParent=hwndParent; + + EXDLL_INIT(); + if (initDone == 0) { + initDone=1; + extra->RegisterPluginCallback(g_hInstance, NSISPluginCallback); + if (IsVista() && IsElevated(NULL) == S_OK) { + //note: COM calls to shell interfaces have to be executed on another thread, + //otherwise I got RPC_E_CANTCALLOUT_ININPUTSYNCCALL error. + HANDLE hRunEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL); //manual reset + g_hThread = (HANDLE)_beginthreadex( NULL, 0, WorkerThreadProc, hRunEvent, 0, NULL); + if (g_hThread) ::WaitForSingleObject(hRunEvent, INFINITE); + CloseHandle(hRunEvent); + OutputDebugString(_T("ExecShellAsUser: elevated process detected")); + } else { + OutputDebugString(_T("ExecShellAsUser: process is not elevated, will fallback to ShellExecute")); + } + } + + // action command [parameters] [SW_SHOWDEFAULT | SW_SHOWNORMAL | SW_SHOWMAXIMIZED | SW_SHOWMINIMIZED | SW_HIDE] + ExecShellParams execShellParams; + ExecShellParams* params = g_hwnd ? new ExecShellParams : &execShellParams; + TCHAR show[50]; + popstring(params->action); + popstring(params->command); + popstring(params->params); + popstring(show); + params->iShow = getShowMode(show); + TCHAR* dir = getuservariable(INST_OUTDIR); + + if (g_hwnd) { + if (dir) lstrcpyn(params->directory, dir, sizeof(params->directory)/sizeof(params->directory[0])); + ::PostMessage(g_hwnd, WM_USER+10, 0, (LPARAM)params); + } else { + ::ShellExecute(NULL, EMPTY_TO_NULL(params->action), params->command, EMPTY_TO_NULL(params->params), dir, params->iShow); + } +} + +BOOL APIENTRY DllMain( HANDLE hModule, + DWORD ul_reason_for_call, + LPVOID lpReserved + ) +{ + if(ul_reason_for_call==DLL_PROCESS_ATTACH) + { + g_hInstance = (HINSTANCE) hModule; + } else if (ul_reason_for_call==DLL_PROCESS_DETACH) { + OutputDebugString(_T("ExecShellAsUser: DLL_PROCESS_DETACH")); + } + return TRUE; +} + diff --git a/src/ShellExecAsUser.sln b/src/ShellExecAsUser.sln new file mode 100644 index 0000000..9fde0fe --- /dev/null +++ b/src/ShellExecAsUser.sln @@ -0,0 +1,32 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ShellExecAsUser", "ShellExecAsUser.vcxproj", "{8384A5B1-BFFE-254C-4F8D-3C865339D9ED}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + ReleaseUnicode|Win32 = ReleaseUnicode|Win32 + ReleaseUnicode|x64 = ReleaseUnicode|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8384A5B1-BFFE-254C-4F8D-3C865339D9ED}.Debug|Win32.ActiveCfg = Debug|Win32 + {8384A5B1-BFFE-254C-4F8D-3C865339D9ED}.Debug|Win32.Build.0 = Debug|Win32 + {8384A5B1-BFFE-254C-4F8D-3C865339D9ED}.Debug|x64.ActiveCfg = Debug|x64 + {8384A5B1-BFFE-254C-4F8D-3C865339D9ED}.Debug|x64.Build.0 = Debug|x64 + {8384A5B1-BFFE-254C-4F8D-3C865339D9ED}.Release|Win32.ActiveCfg = Release|Win32 + {8384A5B1-BFFE-254C-4F8D-3C865339D9ED}.Release|Win32.Build.0 = Release|Win32 + {8384A5B1-BFFE-254C-4F8D-3C865339D9ED}.Release|x64.ActiveCfg = Release|x64 + {8384A5B1-BFFE-254C-4F8D-3C865339D9ED}.Release|x64.Build.0 = Release|x64 + {8384A5B1-BFFE-254C-4F8D-3C865339D9ED}.ReleaseUnicode|Win32.ActiveCfg = ReleaseUnicode|Win32 + {8384A5B1-BFFE-254C-4F8D-3C865339D9ED}.ReleaseUnicode|Win32.Build.0 = ReleaseUnicode|Win32 + {8384A5B1-BFFE-254C-4F8D-3C865339D9ED}.ReleaseUnicode|x64.ActiveCfg = ReleaseUnicode|x64 + {8384A5B1-BFFE-254C-4F8D-3C865339D9ED}.ReleaseUnicode|x64.Build.0 = ReleaseUnicode|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/ShellExecAsUser.vcxproj b/src/ShellExecAsUser.vcxproj new file mode 100644 index 0000000..2312d0c --- /dev/null +++ b/src/ShellExecAsUser.vcxproj @@ -0,0 +1,394 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + ReleaseUnicode + Win32 + + + ReleaseUnicode + x64 + + + Release + Win32 + + + Release + x64 + + + + + + + + + DynamicLibrary + false + MultiByte + + + DynamicLibrary + false + MultiByte + + + DynamicLibrary + false + MultiByte + true + + + DynamicLibrary + false + MultiByte + true + + + DynamicLibrary + false + Unicode + true + + + DynamicLibrary + false + Unicode + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + $(SolutionDir)..\..\Plugins\x86-ansi\ + $(SolutionDir)Intermediate\$(Configuration)\$(PlatformShortName)\ + false + + + $(SolutionDir)..\..\Plugins\amd64-ansi\ + $(SolutionDir)Intermediate\$(Configuration)\$(PlatformShortName)\ + false + + + false + $(SolutionDir)Intermediate\$(Configuration)\$(PlatformShortName)\ + $(SolutionDir)..\..\Plugins\x86-unicode\ + + + false + $(SolutionDir)Intermediate\$(Configuration)\$(PlatformShortName)\ + $(SolutionDir)..\..\Plugins\amd64-unicode\ + + + $(SolutionDir)..\..\Plugins\x86-ansi-debug\ + $(SolutionDir)Intermediate\$(Configuration)\$(PlatformShortName)\ + true + + + $(SolutionDir)..\..\Plugins\amd64-ansi-debug\ + $(SolutionDir)Intermediate\$(Configuration)\$(PlatformShortName)\ + true + + + + MultiThreaded + true + MinSpace + true + Level3 + WIN32;NDEBUG;_WINDOWS;_USRDLL;SHELLEXECASUSER_EXPORTS;%(PreprocessorDefinitions) + + + nsis + true + false + $(IntDir)\$(TargetName).pch + + + true + NDEBUG;%(PreprocessorDefinitions) + .\Release\ShellExecAsUser.tlb + true + Win32 + + + 0x0409 + NDEBUG;%(PreprocessorDefinitions) + + + true + .\Release\ShellExecAsUser.bsc + + + true + true + Windows + $(OutDir)$(TargetName)$(TargetExt) + + + %(AdditionalDependencies) + + + libc.lib + UseLinkTimeCodeGeneration + true + true + + + + + MultiThreaded + true + MinSpace + true + Level3 + WIN32;NDEBUG;_WINDOWS;_USRDLL;SHELLEXECASUSER_EXPORTS;%(PreprocessorDefinitions) + + + nsis + true + false + $(IntDir)\$(TargetName).pch + + + true + NDEBUG;%(PreprocessorDefinitions) + .\Release\ShellExecAsUser.tlb + true + + + 0x0409 + NDEBUG;%(PreprocessorDefinitions) + + + true + .\Release\ShellExecAsUser.bsc + + + true + true + Windows + $(OutDir)$(TargetName)$(TargetExt) + + + %(AdditionalDependencies) + + + libc.lib + UseLinkTimeCodeGeneration + true + true + + + + + MultiThreaded + true + MinSpace + true + Level3 + WIN32;NDEBUG;_WINDOWS;_USRDLL;SHELLEXECASUSER_EXPORTS;%(PreprocessorDefinitions) + + + nsis + true + false + $(IntDir)\$(TargetName).pch + + + true + NDEBUG;%(PreprocessorDefinitions) + .\Release\ShellExecAsUser.tlb + true + Win32 + + + 0x0409 + NDEBUG;%(PreprocessorDefinitions) + + + true + .\Release\ShellExecAsUser.bsc + + + true + true + Windows + %(AdditionalDependencies) + + + libc.lib + UseLinkTimeCodeGeneration + $(OutDir)$(TargetName)$(TargetExt) + + + + + MultiThreaded + true + MinSpace + true + Level3 + WIN32;NDEBUG;_WINDOWS;_USRDLL;SHELLEXECASUSER_EXPORTS;%(PreprocessorDefinitions) + + + nsis + true + false + $(IntDir)\$(TargetName).pch + + + true + NDEBUG;%(PreprocessorDefinitions) + .\Release\ShellExecAsUser.tlb + true + + + 0x0409 + NDEBUG;%(PreprocessorDefinitions) + + + true + .\Release\ShellExecAsUser.bsc + + + true + true + Windows + %(AdditionalDependencies) + + + libc.lib + UseLinkTimeCodeGeneration + $(OutDir)$(TargetName)$(TargetExt) + + + + + MultiThreadedDebug + Default + false + Disabled + true + Level3 + true + EditAndContinue + WIN32;_DEBUG;_WINDOWS;_USRDLL;SHELLEXECASUSER_EXPORTS;%(PreprocessorDefinitions) + + + EnableFastChecks + $(IntDir)\$(TargetName).pch + + + true + _DEBUG;%(PreprocessorDefinitions) + .\Debug\ShellExecAsUser.tlb + true + Win32 + + + 0x0409 + _DEBUG;%(PreprocessorDefinitions) + + + true + .\Debug\ShellExecAsUser.bsc + + + true + true + true + Windows + $(OutDir)$(TargetName)$(TargetExt) + + + shlwapi.lib;%(AdditionalDependencies) + + + + + MultiThreadedDebug + Default + false + Disabled + true + Level3 + ProgramDatabase + WIN32;_DEBUG;_WINDOWS;_USRDLL;SHELLEXECASUSER_EXPORTS;%(PreprocessorDefinitions) + + + EnableFastChecks + $(IntDir)\$(TargetName).pch + + + true + _DEBUG;%(PreprocessorDefinitions) + .\Debug\ShellExecAsUser.tlb + true + + + 0x0409 + _DEBUG;%(PreprocessorDefinitions) + + + true + .\Debug\ShellExecAsUser.bsc + + + true + true + true + Windows + $(OutDir)$(TargetName)$(TargetExt) + + + shlwapi.lib;%(AdditionalDependencies) + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ShellExecAsUser.vcxproj.filters b/src/ShellExecAsUser.vcxproj.filters new file mode 100644 index 0000000..b15542f --- /dev/null +++ b/src/ShellExecAsUser.vcxproj.filters @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/VistaTools.cxx b/src/VistaTools.cxx new file mode 100644 index 0000000..0e510b9 --- /dev/null +++ b/src/VistaTools.cxx @@ -0,0 +1,713 @@ +/////////////////////////////// +/* VistaTools.cxx - version 2.1 + +THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +PARTICULAR PURPOSE. + +Copyright (C) 2008. WinAbility Software Corporation. All rights reserved. + +Author: Andrei Belogortseff [ http://www.softblog.com ] + +TERMS OF USE: You are free to use this file in any way you like, +for both the commercial and non-commercial purposes, royalty-free, +AS LONG AS you agree with the warranty disclaimer above, +EXCEPT that you may not remove or modify this or any of the +preceeding paragraphs. If you make any changes, please document +them in the MODIFICATIONS section below. If the changes are of general +interest, please let us know and we will consider incorporating them in +this file, as well. + +If you use this file in your own project, an acknowledgment will be appreciated, +although it's not required. + +SUMMARY: + +This file contains several Vista-specific functions helpful when dealing with the +"elevation" features of Windows Vista. See the descriptions of the functions below +for information on what each function does and how to use it. + +This file contains the Win32 stuff only, it can be used with or without other frameworks, +such as MFC, ATL, etc. + +HOW TO USE THIS FILE: + +Make sure you have the latest Windows SDK (see msdn.microsoft.com for more information) +or this file may not compile! + +This is a "combo" file that contains both the declarations (usually placed in the .h files) +as well as the definitions (usually placed in the .cpp files) of the functions. + +To get the declarations only, include it as you would any .h file, for example: + +#include "VistaTools.cxx" + +To get both the declarations and definitions, define IMPLEMENT_VISTA_TOOLS before including the file: + +#define IMPLEMENT_VISTA_TOOLS +#include "VistaTools.cxx" + +(The above should be done once and only once per project). + +To use the function RunNonElevated, this file must be compiled into a DLL. +In such a case, define DLL_EXPORTS when compiling the DLL, +and do not define DLL_EXPORTS when compiling the projects linking to the DLL. + +If you don't need to use RunNonElevated, then this file may be a part of an EXE +project. In such a case, define DONTWANT_RunNonElevated and NO_DLL_IMPORTS to make +this file compile properly. + +NOTE: The file VistaTools.cxx can be included in the VisualStudio projects, but it should be +excluded from the build process (because its contents is compiled when it is included +in another .cpp file with IMPLEMENT_VISTA_TOOLS defined, as shown above.) + +MODIFICATIONS: + v.1.0 (2006-Dec-16) created by Andrei Belogortseff. + + v.2.0 (2007-Feb-20) deprecated RunAsStdUser(); + implemented RunNonElevated() to replace RunAsStdUser(); + added function declarations for use in a DLL project + + v.2.1 (2008-Feb-24) removed RunAsStdUser(); +*/ + +#if ( NTDDI_VERSION < NTDDI_LONGHORN ) +# error NTDDI_VERSION must be defined as NTDDI_LONGHORN or later +#endif + +#if !defined(NTDDI_VISTA) || defined(BUILD_OLDSDK) + +typedef enum _TOKEN_ELEVATION_TYPE { + TokenElevationTypeDefault = 1, + TokenElevationTypeFull, + TokenElevationTypeLimited, +} TOKEN_ELEVATION_TYPE, *PTOKEN_ELEVATION_TYPE; + +typedef struct _TOKEN_ELEVATION { + DWORD TokenIsElevated; +} TOKEN_ELEVATION, *PTOKEN_ELEVATION; + +enum _TOKEN_INFORMATION_CLASS___VISTA { + TokenElevationType = (TokenOrigin+1), + TokenLinkedToken, + TokenElevation, + TokenHasRestrictions, + TokenAccessInformation, + TokenVirtualizationAllowed, + TokenVirtualizationEnabled, + TokenIntegrityLevel, + TokenUIAccess, + TokenMandatoryPolicy, + TokenLogonSid, +}; + +#endif + +////////////////////////////////////////////////////////////////// +// if ASSERT was not defined already, let's define our own version, +// to use the CRT assert() + +#ifndef ASSERT +# ifdef _DEBUG +# include +# define ASSERT(x) assert( x ) +# define ASSERT_HERE assert( FALSE ) +# else// _DEBUG +# define ASSERT(x) +# define ASSERT_HERE +# endif//_DEBUG +#endif//ASSERT + +/////////////////////////////////////////////////////////////////// +// a handy macro to get the number of characters (not bytes!) +// a string buffer can hold + +#ifndef _tsizeof +# define _tsizeof( s ) (sizeof(s)/sizeof(s[0])) +#endif//_tsizeof + +/////////////////////////////////////////////////////////////// +// macros to handle the functions implemented in a DLL properly + +#ifdef DLL_EXPORTS +# define DLL_API __declspec(dllexport) +#else +# ifndef NO_DLL_IMPORTS +# define DLL_API __declspec(dllimport) +# else +# define DLL_API +# endif +#endif + +BOOL DLL_API +IsVista(); + +/* +Use IsVista() to determine whether the current process is running under Windows Vista or +(or a later version of Windows, whatever it will be) + +Return Values: + If the function succeeds, and the current version of Windows is Vista or later, + the return value is TRUE. + If the function fails, or if the current version of Windows is older than Vista + (that is, if it is Windows XP, Windows 2000, Windows Server 2003, Windows 98, etc.) + the return value is FALSE. +*/ + +#ifndef WIN64 + +BOOL DLL_API +IsWow64(); + +/* +Use IsWow64() to determine whether the current 32-bit process is running under 64-bit Windows +(Vista or XP) + +Return Values: + If the function succeeds, and the current version of Windows is x64, + the return value is TRUE. + If the function fails, or if the current version of Windows is 32-bit, + the return value is FALSE. + +While this function is not Vista specific (it works under XP as well), +we include it here to be able to prevent execution of the 32-bit code under 64-bit Windows, +when required. +*/ + +#endif//WIN64 + + +HRESULT DLL_API +GetElevationType( __out TOKEN_ELEVATION_TYPE * ptet ); + +/* +Use GetElevationType() to determine the elevation type of the current process. + +Parameters: + +ptet + [out] Pointer to a variable that receives the elevation type of the current process. + + The possible values are: + + TokenElevationTypeDefault - User is not using a "split" token. + This value indicates that either UAC is disabled, or the process is started + by a standard user (not a member of the Administrators group). + + The following two values can be returned only if both the UAC is enabled and + the user is a member of the Administrator's group (that is, the user has a "split" token): + + TokenElevationTypeFull - the process is running elevated. + + TokenElevationTypeLimited - the process is not running elevated. + +Return Values: + If the function succeeds, the return value is S_OK. + If the function fails, the return value is E_FAIL. To get extended error information, + call GetLastError(). +*/ + +HRESULT DLL_API +IsElevated( __out_opt BOOL * pbElevated = NULL ); + +/* +Use IsElevated() to determine whether the current process is elevated or not. + +Parameters: + +pbElevated + [out] [optional] Pointer to a BOOL variable that, if non-NULL, receives the result. + + The possible values are: + + TRUE - the current process is elevated. + This value indicates that either UAC is enabled, and the process was elevated by + the administrator, or that UAC is disabled and the process was started by a user + who is a member of the Administrators group. + + FALSE - the current process is not elevated (limited). + This value indicates that either UAC is enabled, and the process was started normally, + without the elevation, or that UAC is disabled and the process was started by a standard user. + +Return Values + If the function succeeds, and the current process is elevated, the return value is S_OK. + If the function succeeds, and the current process is not elevated, the return value is S_FALSE. + If the function fails, the return value is E_FAIL. To get extended error information, + call GetLastError(). +*/ + +BOOL DLL_API +RunElevated( + __in HWND hwnd, + __in LPCTSTR pszPath, + __in_opt LPCTSTR pszParameters = NULL, + __in_opt LPCTSTR pszDirectory = NULL ); + +/* +Use RunElevated() to start an elevated process. This function calls ShellExecEx() with the verb "runas" +to start the elevated process. + +Parameters: + +hwnd + [in] Window handle to any message boxes that the system might produce while executing this function. + +pszPath + [in] Address of a null-terminated string that specifies the name of the executable file that + should be used to start the process. + +pszParameters + [in] [optional] Address of a null-terminated string that contains the command-line parameters for the process. + If NULL, no parameters are passed to the process. + +pszDirectory + [in] [optional] Address of a null-terminated string that specifies the name of the working directory. + If NULL, the current directory is used as the working directory. . + +Return Values + If the function succeeds, the return value is TRUE. + If the function fails, the return value is FALSE. To get extended error information, + call GetLastError(). + +NOTE: This function will start a process elevated no matter which attribute (asInvoker, +highestAvailable, or requireAdministrator) is specified in its manifest, and even if +there is no such attribute at all. +*/ + +#ifndef DONTWANT_RunNonElevated + +#ifdef NO_DLL_IMPORTS +# error RunNonElevated must be used in a DLL project! +#endif//NO_DLL_IMPORTS + + +BOOL DLL_API +RunNonElevated( + __in HWND hwnd, + __in LPCTSTR pszPath, + __in_opt LPCTSTR pszParameters = NULL, + __in_opt LPCTSTR pszDirectory = NULL ); + +/* +Use RunNonElevated() to start a non-elevated process. If the current process is not elevated, +it calls ShellExecuteEx() to start the new process. If the current process is elevated, +it injects itself into the (non-elevated) shell process, and starts a non-elevated process from there. + +Parameters: + +hwnd + [in] Window handle to any message boxes that the system might produce while executing this function. + +pszPath + [in] Address of a null-terminated string that specifies the executable file that + should be used to start the process. + +pszParameters + [in] [optional] Address of a null-terminated string that contains the command-line parameters for + the process. If NULL, no parameters are passed to the process. + +pszDirectory + [in] [optional] Address of a null-terminated string that specifies the name of the working directory. + If NULL, the current directory is used as the working directory. . + +Return Values + If the function succeeds, the return value is TRUE. + If the function fails, the return value is FALSE. To get extended error information, + call GetLastError(). + +NOTE: For this function to work, the application must be marked with the asInvoker or +highestAvailable attributes in its manifest. If the executable to be started is marked +as requireAdministrator, it will be started elevated! +*/ +#endif //DONTWANT_RunNonElevated + +////////////////////////////////////////////////////////// +// MyShellExec is just a wrapper around a call to ShellExecuteEx, +// to be able to specify the verb easily. + +BOOL DLL_API +MyShellExec( HWND hwnd, + LPCTSTR pszVerb, + LPCTSTR pszPath, + LPCTSTR pszParameters = NULL, + int nShow = SW_SHOWNORMAL); + + +#ifdef IMPLEMENT_VISTA_TOOLS + +////////////////////////////////////////////////////////// +// MyShellExec is just a wrapper around a call to ShellExecuteEx, +// to be able to specify the verb easily. + +/* +BOOL +MyShellExec( HWND hwnd, + LPCTSTR pszVerb, + LPCTSTR pszPath, + LPCTSTR pszParameters, // = NULL, + int nShow) +{ + SHELLEXECUTEINFO shex; + + memset( &shex, 0, sizeof( shex) ); + + shex.cbSize = sizeof( SHELLEXECUTEINFO ); + shex.fMask = 0; + shex.hwnd = hwnd; + shex.lpVerb = pszVerb; + shex.lpFile = pszPath; + shex.lpParameters = pszParameters; + shex.lpDirectory = NULL; + shex.nShow = nShow; + + return ::ShellExecuteEx( &shex ); +} +*/ + +BOOL IsVista() +{ + OSVERSIONINFOEX osver; + DWORDLONG dwlConditionMask = 0; + int op = VER_GREATER_EQUAL; + + ZeroMemory(&osver, sizeof(OSVERSIONINFOEX)); + osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); + osver.dwMajorVersion = 6; + osver.dwMinorVersion = 0; + osver.dwPlatformId = VER_PLATFORM_WIN32_NT; + + VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, op); + VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, op); + VER_SET_CONDITION(dwlConditionMask, VER_PLATFORMID, VER_EQUAL); + + return VerifyVersionInfo(&osver, VER_MAJORVERSION | VER_MINORVERSION | VER_PLATFORMID, dwlConditionMask); +} + +#ifndef WIN64 // we need this when compiling 32-bit code only + +typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE hProcess,PBOOL Wow64Process); + +LPFN_ISWOW64PROCESS fnIsWow64Process = + (LPFN_ISWOW64PROCESS)::GetProcAddress( ::GetModuleHandle(_T("kernel32")),"IsWow64Process"); + +BOOL +IsWow64() +{ + BOOL bIsWow64 = FALSE; + + if (NULL != fnIsWow64Process) + { + if ( !fnIsWow64Process( ::GetCurrentProcess(),&bIsWow64) ) + { + ASSERT_HERE; + } + } + + return bIsWow64; +} + +#endif//WIN64 + +HRESULT +GetElevationType( __out TOKEN_ELEVATION_TYPE * ptet ) +{ + ASSERT( IsVista() ); + ASSERT( ptet ); + + HRESULT hResult = E_FAIL; // assume an error occured + HANDLE hToken = NULL; + + if ( !::OpenProcessToken( + ::GetCurrentProcess(), + TOKEN_QUERY, + &hToken ) ) + { + ASSERT_HERE; + return hResult; + } + + DWORD dwReturnLength = 0; + + if ( !::GetTokenInformation( + hToken, + (_TOKEN_INFORMATION_CLASS)TokenElevationType, + ptet, + sizeof( *ptet ), + &dwReturnLength ) ) + { + ASSERT_HERE; + } + else + { + ASSERT( dwReturnLength == sizeof( *ptet ) ); + hResult = S_OK; + } + + ::CloseHandle( hToken ); + + return hResult; +} + +HRESULT +IsElevated( __out_opt BOOL * pbElevated ) //= NULL ) +{ + ASSERT( IsVista() ); + + HRESULT hResult = E_FAIL; // assume an error occured + HANDLE hToken = NULL; + + if ( !::OpenProcessToken( + ::GetCurrentProcess(), + TOKEN_QUERY, + &hToken ) ) + { + ASSERT_HERE; + return hResult; + } + + TOKEN_ELEVATION te = { 0 }; + DWORD dwReturnLength = 0; + + if ( !::GetTokenInformation( + hToken, + (_TOKEN_INFORMATION_CLASS)TokenElevation, + &te, + sizeof( te ), + &dwReturnLength ) ) + { + ASSERT_HERE; + } + else + { + ASSERT( dwReturnLength == sizeof( te ) ); + + hResult = te.TokenIsElevated ? S_OK : S_FALSE; + + if ( pbElevated) + *pbElevated = (te.TokenIsElevated != 0); + } + + ::CloseHandle( hToken ); + + return hResult; +} + +//////////////////////////////// +// RunElevated simply calls ShellExecuteEx with the verb "runas" to start the elevated process. +// I wish there was a just as easy way to start a non-elevated process, as well. +/* +BOOL +RunElevated( + __in HWND hwnd, + __in LPCTSTR pszPath, + __in_opt LPCTSTR pszParameters, // = NULL, + __in_opt LPCTSTR pszDirectory ) // = NULL ); +{ + return MyShellExec( hwnd, _T("runas"), pszPath, pszParameters ); +} +*/ + +#ifndef DONTWANT_RunNonElevated +/////////////////////////////////////// +// RunNonElevated() implementation + +/////////////////////////////////////////////////// +// The shared data, to be able to share +// data between ours and the shell proceses + +#pragma section( "ve_shared", read, write, shared ) + +__declspec(allocate("ve_shared")) + HHOOK hHook = NULL; + +__declspec(allocate("ve_shared")) + UINT uVistaElevatorMsg = 0; + +__declspec(allocate("ve_shared")) + BOOL bSuccess = FALSE; + +__declspec(allocate("ve_shared")) + TCHAR szVE_Path[ MAX_PATH ] = _T(""); + +__declspec(allocate("ve_shared")) + TCHAR szVE_Parameters[ MAX_PATH ] = _T(""); + +__declspec(allocate("ve_shared")) + TCHAR szVE_Directory[ MAX_PATH ] = _T(""); + +//////////////////////////////////e +// the hook callback procedure, it is called in the context of th shell proces + +LRESULT CALLBACK +VistaEelevator_HookProc_MsgRet( int code, WPARAM wParam, LPARAM lParam ) +{ + if ( code >= 0 && lParam ) + { + CWPRETSTRUCT * pwrs = (CWPRETSTRUCT *)lParam; + + if (pwrs->message == uVistaElevatorMsg ) + { + bSuccess = ::MyShellExec( + pwrs->hwnd, + NULL, + szVE_Path, + szVE_Parameters, + szVE_Directory ); + } + } + + return ::CallNextHookEx( hHook, code, wParam, lParam ); +} + +BOOL +RunNonElevated( + __in HWND hwnd, + __in LPCTSTR pszPath, + __in_opt LPCTSTR pszParameters, // = NULL, + __in_opt LPCTSTR pszDirectory ) // = NULL ); +{ + ASSERT( pszPath && *pszPath ); // other args are optional + + ASSERT( IsVista() ); + ASSERT( pszPath ); + + if ( S_FALSE == IsElevated() ) + { + // if the current process is not elevated, we can use ShellExecuteAs directly! + + return MyShellExec( hwnd, + NULL, + pszPath, + pszParameters, + pszDirectory ); + } + + #ifndef WIN64 + + // If 32-bit code is executing under x64 version of Windows, it will not work, because + // the shell is a 64-bit process, and to hook it successfully this code needs to be 64-bit as well + + if ( IsWow64() ) + { + ASSERT_HERE; + return FALSE; + } + + #endif//WIN64 + + + ////////////////////////////////////////////// + // + // How this code works: + // + // To start a non-elevated process, we need to inject our code into a non-elevated + // process already running on the target computer. Since Windows shell is non-elevated, + // and we can safely assume it to always run, (when was the last time you saw Windows + // running without its shell?), we will try to inject our code into the shell process. + // + // To inject the code, we will install a global hook, and send a message to + // a window created by the shell. This will cause our hook callback procedure to be executed + // in the context of the shell proces. + // + // Because this trick uses a global hook, the hook procedure must be in a DLL. + // + + ///////////////////// + // First, register a private message, to communicate with the (hooked) shell process + + if ( !uVistaElevatorMsg ) + uVistaElevatorMsg = ::RegisterWindowMessage( _T("VistaElevatorMsg") ); + + ////////////////////////////////////// + // Find the shell window (the desktop) + + HWND hwndShell = ::FindWindow( _T("Progman"), NULL); + + if ( !hwndShell ) + { + ASSERT_HERE; + return FALSE; + } + + ///////////////////////////////////////// + // Install a global hook, to inject our code into the shell proces + + HMODULE hModule = NULL; // we need to know hModule of the DLL to install a global hook + + if ( !::GetModuleHandleEx( + GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + (LPCTSTR)VistaEelevator_HookProc_MsgRet, + &hModule ) ) + { + ASSERT_HERE; + return FALSE; + } + + ASSERT( hModule ); + + hHook = ::SetWindowsHookEx( WH_CALLWNDPROCRET, // hook called after SendMessage() !!! + (HOOKPROC)VistaEelevator_HookProc_MsgRet, hModule, 0); + + if ( !hHook ) + { + ASSERT_HERE; + return FALSE; + } + + ////////////////////////////////// + // Prepare the parameters for launching the non-elevated process + // from the hook callback procedure (they are placed into the shared data section) + + if ( FAILED( StringCchCopy( + szVE_Path, + _tsizeof(szVE_Path), + pszPath ) ) ) + { + ASSERT_HERE; + return FALSE; + } + + if ( FAILED( StringCchCopy( + szVE_Parameters, + _tsizeof(szVE_Parameters), + pszParameters ? pszParameters : _T("") ) ) ) + { + ASSERT_HERE; + return FALSE; + } + + if ( FAILED( StringCchCopy( + szVE_Directory, + _tsizeof(szVE_Directory), + pszDirectory ? pszDirectory : _T("") ) ) ) + { + ASSERT_HERE; + return FALSE; + } + + ///////////////////////////////////////// + // Activate our hook callback procedure: + + bSuccess = FALSE; // assume failure + + ::SendMessage( hwndShell, uVistaElevatorMsg, 0, 0 ); + + //////////////////////////////////////////////////////// + // At this point our hook procedure has been executed! + + ///////////////////////////////////// + // The hook is no longer needed, remove it: + + ::UnhookWindowsHookEx( hHook ); + hHook = NULL; + + ////////////////////// + // All done! + + return bSuccess; +} +#endif //DONTWANT_RunNonElevated + + +#endif// IMPLEMENT_VISTA_TOOLS \ No newline at end of file diff --git a/src/api.h b/src/api.h new file mode 100644 index 0000000..85d41c4 --- /dev/null +++ b/src/api.h @@ -0,0 +1,83 @@ +/* + * apih + * + * This file is a part of NSIS. + * + * Copyright (C) 1999-2013 Nullsoft and Contributors + * + * Licensed under the zlib/libpng license (the "License"); + * you may not use this file except in compliance with the License. + * + * Licence details can be found in the file COPYING. + * + * This software is provided 'as-is', without any express or implied + * warranty. + */ + +#ifndef _NSIS_EXEHEAD_API_H_ +#define _NSIS_EXEHEAD_API_H_ + +// Starting with NSIS 2.42, you can check the version of the plugin API in exec_flags->plugin_api_version +// The format is 0xXXXXYYYY where X is the major version and Y is the minor version (MAKELONG(y,x)) +// When doing version checks, always remember to use >=, ex: if (pX->exec_flags->plugin_api_version >= NSISPIAPIVER_1_0) {} + +#define NSISPIAPIVER_1_0 0x00010000 +#define NSISPIAPIVER_CURR NSISPIAPIVER_1_0 + +// NSIS Plug-In Callback Messages +enum NSPIM +{ + NSPIM_UNLOAD, // This is the last message a plugin gets, do final cleanup + NSPIM_GUIUNLOAD, // Called after .onGUIEnd +}; + +// Prototype for callbacks registered with extra_parameters->RegisterPluginCallback() +// Return NULL for unknown messages +// Should always be __cdecl for future expansion possibilities +typedef UINT_PTR (*NSISPLUGINCALLBACK)(enum NSPIM); + +// extra_parameters data structures containing other interesting stuff +// but the stack, variables and HWND passed on to plug-ins. +typedef struct +{ + int autoclose; + int all_user_var; + int exec_error; + int abort; + int exec_reboot; // NSIS_SUPPORT_REBOOT + int reboot_called; // NSIS_SUPPORT_REBOOT + int XXX_cur_insttype; // depreacted + int plugin_api_version; // see NSISPIAPIVER_CURR + // used to be XXX_insttype_changed + int silent; // NSIS_CONFIG_SILENT_SUPPORT + int instdir_error; + int rtl; + int errlvl; + int alter_reg_view; + int status_update; +} exec_flags_t; + +#ifndef NSISCALL +# define NSISCALL __stdcall +#endif + +typedef struct { + exec_flags_t *exec_flags; + int (NSISCALL *ExecuteCodeSegment)(int, HWND); + void (NSISCALL *validate_filename)(TCHAR *); + int (NSISCALL *RegisterPluginCallback)(HMODULE, NSISPLUGINCALLBACK); // returns 0 on success, 1 if already registered and < 0 on errors +} extra_parameters; + +// Definitions for page showing plug-ins +// See Ui.c to understand better how they're used + +// sent to the outer window to tell it to go to the next inner window +#define WM_NOTIFY_OUTER_NEXT (WM_USER+0x8) + +// custom pages should send this message to let NSIS know they're ready +#define WM_NOTIFY_CUSTOM_READY (WM_USER+0xd) + +// sent as wParam with WM_NOTIFY_OUTER_NEXT when user cancels - heed its warning +#define NOTIFY_BYE_BYE 'x' + +#endif /* _PLUGIN_H_ */ diff --git a/src/nsis_tchar.h b/src/nsis_tchar.h new file mode 100644 index 0000000..210bab1 --- /dev/null +++ b/src/nsis_tchar.h @@ -0,0 +1,229 @@ +/* + * nsis_tchar.h + * + * This file is a part of NSIS. + * + * Copyright (C) 1999-2013 Nullsoft and Contributors + * + * This software is provided 'as-is', without any express or implied + * warranty. + * + * For Unicode support by Jim Park -- 08/30/2007 + */ + +// Jim Park: Only those we use are listed here. + +#pragma once + +#ifdef _UNICODE + +#ifndef _T +#define __T(x) L ## x +#define _T(x) __T(x) +#define _TEXT(x) __T(x) +#endif + +#ifndef _TCHAR_DEFINED +#define _TCHAR_DEFINED +#if !defined(_NATIVE_WCHAR_T_DEFINED) && !defined(_WCHAR_T_DEFINED) +typedef unsigned short TCHAR; +#else +typedef wchar_t TCHAR; +#endif +#endif + + +// program +#define _tenviron _wenviron +#define __targv __wargv + +// printfs +#define _ftprintf fwprintf +#define _sntprintf _snwprintf +#if (defined(_MSC_VER) && (_MSC_VER<=1310)) || defined(__MINGW32__) +# define _stprintf swprintf +#else +# define _stprintf _swprintf +#endif +#define _tprintf wprintf +#define _vftprintf vfwprintf +#define _vsntprintf _vsnwprintf +#if defined(_MSC_VER) && (_MSC_VER<=1310) +# define _vstprintf vswprintf +#else +# define _vstprintf _vswprintf +#endif + +// scanfs +#define _tscanf wscanf +#define _stscanf swscanf + +// string manipulations +#define _tcscat wcscat +#define _tcschr wcschr +#define _tcsclen wcslen +#define _tcscpy wcscpy +#define _tcsdup _wcsdup +#define _tcslen wcslen +#define _tcsnccpy wcsncpy +#define _tcsncpy wcsncpy +#define _tcsrchr wcsrchr +#define _tcsstr wcsstr +#define _tcstok wcstok + +// string comparisons +#define _tcscmp wcscmp +#define _tcsicmp _wcsicmp +#define _tcsncicmp _wcsnicmp +#define _tcsncmp wcsncmp +#define _tcsnicmp _wcsnicmp + +// upper / lower +#define _tcslwr _wcslwr +#define _tcsupr _wcsupr +#define _totlower towlower +#define _totupper towupper + +// conversions to numbers +#define _tcstoi64 _wcstoi64 +#define _tcstol wcstol +#define _tcstoul wcstoul +#define _tstof _wtof +#define _tstoi _wtoi +#define _tstoi64 _wtoi64 +#define _ttoi _wtoi +#define _ttoi64 _wtoi64 +#define _ttol _wtol + +// conversion from numbers to strings +#define _itot _itow +#define _ltot _ltow +#define _i64tot _i64tow +#define _ui64tot _ui64tow + +// file manipulations +#define _tfopen _wfopen +#define _topen _wopen +#define _tremove _wremove +#define _tunlink _wunlink + +// reading and writing to i/o +#define _fgettc fgetwc +#define _fgetts fgetws +#define _fputts fputws +#define _gettchar getwchar + +// directory +#define _tchdir _wchdir + +// environment +#define _tgetenv _wgetenv +#define _tsystem _wsystem + +// time +#define _tcsftime wcsftime + +#else // ANSI + +#ifndef _T +#define _T(x) x +#define _TEXT(x) x +#endif + +#ifndef _TCHAR_DEFINED +#define _TCHAR_DEFINED +typedef char TCHAR; +#endif + +// program +#define _tenviron environ +#define __targv __argv + +// printfs +#define _ftprintf fprintf +#define _sntprintf _snprintf +#define _stprintf sprintf +#define _tprintf printf +#define _vftprintf vfprintf +#define _vsntprintf _vsnprintf +#define _vstprintf vsprintf + +// scanfs +#define _tscanf scanf +#define _stscanf sscanf + +// string manipulations +#define _tcscat strcat +#define _tcschr strchr +#define _tcsclen strlen +#define _tcscnlen strnlen +#define _tcscpy strcpy +#define _tcsdup _strdup +#define _tcslen strlen +#define _tcsnccpy strncpy +#define _tcsrchr strrchr +#define _tcsstr strstr +#define _tcstok strtok + +// string comparisons +#define _tcscmp strcmp +#define _tcsicmp _stricmp +#define _tcsncmp strncmp +#define _tcsncicmp _strnicmp +#define _tcsnicmp _strnicmp + +// upper / lower +#define _tcslwr _strlwr +#define _tcsupr _strupr + +#define _totupper toupper +#define _totlower tolower + +// conversions to numbers +#define _tcstol strtol +#define _tcstoul strtoul +#define _tstof atof +#define _tstoi atoi +#define _tstoi64 _atoi64 +#define _tstoi64 _atoi64 +#define _ttoi atoi +#define _ttoi64 _atoi64 +#define _ttol atol + +// conversion from numbers to strings +#define _i64tot _i64toa +#define _itot _itoa +#define _ltot _ltoa +#define _ui64tot _ui64toa + +// file manipulations +#define _tfopen fopen +#define _topen _open +#define _tremove remove +#define _tunlink _unlink + +// reading and writing to i/o +#define _fgettc fgetc +#define _fgetts fgets +#define _fputts fputs +#define _gettchar getchar + +// directory +#define _tchdir _chdir + +// environment +#define _tgetenv getenv +#define _tsystem system + +// time +#define _tcsftime strftime + +#endif + +// is functions (the same in Unicode / ANSI) +#define _istgraph isgraph +#define _istascii __isascii + +#define __TFILE__ _T(__FILE__) +#define __TDATE__ _T(__DATE__) +#define __TTIME__ _T(__TIME__) diff --git a/src/pluginapi.cpp b/src/pluginapi.cpp new file mode 100644 index 0000000..0fd25db --- /dev/null +++ b/src/pluginapi.cpp @@ -0,0 +1,296 @@ +//#include "StdAfx.h" + +#include +#include +#include "pluginapi.h" + +#ifdef _countof +#define COUNTOF _countof +#else +#define COUNTOF(a) (sizeof(a)/sizeof(a[0])) +#endif + +unsigned int g_stringsize; +stack_t **g_stacktop; +TCHAR *g_variables; + +// utility functions (not required but often useful) + +int NSISCALL popstring(TCHAR *str) +{ + stack_t *th; + if (!g_stacktop || !*g_stacktop) return 1; + th=(*g_stacktop); + if (str) lstrcpy(str,th->text); + *g_stacktop = th->next; + GlobalFree((HGLOBAL)th); + return 0; +} + +int NSISCALL popstringn(TCHAR *str, int maxlen) +{ + stack_t *th; + if (!g_stacktop || !*g_stacktop) return 1; + th=(*g_stacktop); + if (str) lstrcpyn(str,th->text,maxlen?maxlen:g_stringsize); + *g_stacktop = th->next; + GlobalFree((HGLOBAL)th); + return 0; +} + +void NSISCALL pushstring(const TCHAR *str) +{ + stack_t *th; + if (!g_stacktop) return; + th=(stack_t*)GlobalAlloc(GPTR,(sizeof(stack_t)+(g_stringsize)*sizeof(TCHAR))); + lstrcpyn(th->text,str,g_stringsize); + th->next=*g_stacktop; + *g_stacktop=th; +} + +TCHAR* NSISCALL getuservariable(const int varnum) +{ + if (varnum < 0 || varnum >= __INST_LAST) return NULL; + return g_variables+varnum*g_stringsize; +} + +void NSISCALL setuservariable(const int varnum, const TCHAR *var) +{ + if (var != NULL && varnum >= 0 && varnum < __INST_LAST) + lstrcpy(g_variables + varnum*g_stringsize, var); +} + +#ifdef _UNICODE +int NSISCALL PopStringA(char* ansiStr) +{ + wchar_t* wideStr = (wchar_t*) GlobalAlloc(GPTR, g_stringsize*sizeof(wchar_t)); + int rval = popstring(wideStr); + WideCharToMultiByte(CP_ACP, 0, wideStr, -1, ansiStr, g_stringsize, NULL, NULL); + GlobalFree((HGLOBAL)wideStr); + return rval; +} + +int NSISCALL PopStringNA(char* ansiStr, int maxlen) +{ + int realLen = maxlen ? maxlen : g_stringsize; + wchar_t* wideStr = (wchar_t*) GlobalAlloc(GPTR, realLen*sizeof(wchar_t)); + int rval = popstringn(wideStr, realLen); + WideCharToMultiByte(CP_ACP, 0, wideStr, -1, ansiStr, realLen, NULL, NULL); + GlobalFree((HGLOBAL)wideStr); + return rval; +} + +void NSISCALL PushStringA(const char* ansiStr) +{ + wchar_t* wideStr = (wchar_t*) GlobalAlloc(GPTR, g_stringsize*sizeof(wchar_t)); + MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, wideStr, g_stringsize); + pushstring(wideStr); + GlobalFree((HGLOBAL)wideStr); + return; +} + +void NSISCALL GetUserVariableW(const int varnum, wchar_t* wideStr) +{ + lstrcpyW(wideStr, getuservariable(varnum)); +} + +void NSISCALL GetUserVariableA(const int varnum, char* ansiStr) +{ + wchar_t* wideStr = getuservariable(varnum); + WideCharToMultiByte(CP_ACP, 0, wideStr, -1, ansiStr, g_stringsize, NULL, NULL); +} + +void NSISCALL SetUserVariableA(const int varnum, const char* ansiStr) +{ + if (ansiStr != NULL && varnum >= 0 && varnum < __INST_LAST) + { + wchar_t* wideStr = g_variables + varnum * g_stringsize; + MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, wideStr, g_stringsize); + } +} + +#else +// ANSI defs +int NSISCALL PopStringW(wchar_t* wideStr) +{ + char* ansiStr = (char*) GlobalAlloc(GPTR, g_stringsize); + int rval = popstring(ansiStr); + MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, wideStr, g_stringsize); + GlobalFree((HGLOBAL)ansiStr); + return rval; +} + +int NSISCALL PopStringNW(wchar_t* wideStr, int maxlen) +{ + int realLen = maxlen ? maxlen : g_stringsize; + char* ansiStr = (char*) GlobalAlloc(GPTR, realLen); + int rval = popstringn(ansiStr, realLen); + MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, wideStr, realLen); + GlobalFree((HGLOBAL)ansiStr); + return rval; +} + +void NSISCALL PushStringW(wchar_t* wideStr) +{ + char* ansiStr = (char*) GlobalAlloc(GPTR, g_stringsize); + WideCharToMultiByte(CP_ACP, 0, wideStr, -1, ansiStr, g_stringsize, NULL, NULL); + pushstring(ansiStr); + GlobalFree((HGLOBAL)ansiStr); +} + +void NSISCALL GetUserVariableW(const int varnum, wchar_t* wideStr) +{ + char* ansiStr = getuservariable(varnum); + MultiByteToWideChar(CP_ACP, 0, ansiStr, -1, wideStr, g_stringsize); +} + +void NSISCALL GetUserVariableA(const int varnum, char* ansiStr) +{ + lstrcpyA(ansiStr, getuservariable(varnum)); +} + +void NSISCALL SetUserVariableW(const int varnum, const wchar_t* wideStr) +{ + if (wideStr != NULL && varnum >= 0 && varnum < __INST_LAST) + { + char* ansiStr = g_variables + varnum * g_stringsize; + WideCharToMultiByte(CP_ACP, 0, wideStr, -1, ansiStr, g_stringsize, NULL, NULL); + } +} +#endif + +// playing with integers + +INT_PTR NSISCALL nsishelper_str_to_ptr(const TCHAR *s) +{ + INT_PTR v=0; + if (*s == _T('0') && (s[1] == _T('x') || s[1] == _T('X'))) + { + s++; + for (;;) + { + int c=*(++s); + if (c >= _T('0') && c <= _T('9')) c-=_T('0'); + else if (c >= _T('a') && c <= _T('f')) c-=_T('a')-10; + else if (c >= _T('A') && c <= _T('F')) c-=_T('A')-10; + else break; + v<<=4; + v+=c; + } + } + else if (*s == _T('0') && s[1] <= _T('7') && s[1] >= _T('0')) + { + for (;;) + { + int c=*(++s); + if (c >= _T('0') && c <= _T('7')) c-=_T('0'); + else break; + v<<=3; + v+=c; + } + } + else + { + int sign=0; + if (*s == _T('-')) sign++; else s--; + for (;;) + { + int c=*(++s) - _T('0'); + if (c < 0 || c > 9) break; + v*=10; + v+=c; + } + if (sign) v = -v; + } + + return v; +} + +unsigned int NSISCALL myatou(const TCHAR *s) +{ + unsigned int v=0; + + for (;;) + { + unsigned int c=*s++; + if (c >= _T('0') && c <= _T('9')) c-=_T('0'); + else break; + v*=10; + v+=c; + } + return v; +} + +int NSISCALL myatoi_or(const TCHAR *s) +{ + int v=0; + if (*s == _T('0') && (s[1] == _T('x') || s[1] == _T('X'))) + { + s++; + for (;;) + { + int c=*(++s); + if (c >= _T('0') && c <= _T('9')) c-=_T('0'); + else if (c >= _T('a') && c <= _T('f')) c-=_T('a')-10; + else if (c >= _T('A') && c <= _T('F')) c-=_T('A')-10; + else break; + v<<=4; + v+=c; + } + } + else if (*s == _T('0') && s[1] <= _T('7') && s[1] >= _T('0')) + { + for (;;) + { + int c=*(++s); + if (c >= _T('0') && c <= _T('7')) c-=_T('0'); + else break; + v<<=3; + v+=c; + } + } + else + { + int sign=0; + if (*s == _T('-')) sign++; else s--; + for (;;) + { + int c=*(++s) - _T('0'); + if (c < 0 || c > 9) break; + v*=10; + v+=c; + } + if (sign) v = -v; + } + + // Support for simple ORed expressions + if (*s == _T('|')) + { + v |= myatoi_or(s+1); + } + + return v; +} + +INT_PTR NSISCALL popintptr() +{ + TCHAR buf[128]; + if (popstringn(buf,COUNTOF(buf))) + return 0; + return nsishelper_str_to_ptr(buf); +} + +int NSISCALL popint_or() +{ + TCHAR buf[128]; + if (popstringn(buf,COUNTOF(buf))) + return 0; + return myatoi_or(buf); +} + +void NSISCALL pushintptr(INT_PTR value) +{ + TCHAR buffer[30]; + wsprintf(buffer, sizeof(void*) > 4 ? _T("%Id") : _T("%d"), value); + pushstring(buffer); +} diff --git a/src/pluginapi.h b/src/pluginapi.h new file mode 100644 index 0000000..f607b11 --- /dev/null +++ b/src/pluginapi.h @@ -0,0 +1,115 @@ +#ifndef ___NSIS_PLUGIN__H___ +#define ___NSIS_PLUGIN__H___ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "api.h" +#ifdef _UNICODE +#ifndef _T +#define __T(x) L ## x +#define _T(x) __T(x) +#define _TEXT(x) __T(x) +#endif +#else +#ifndef _T +#define _T(x) x +#define _TEXT(x) x +#endif +#endif + +#ifndef NSISCALL +# define NSISCALL __stdcall +#endif + +#define EXDLL_INIT() { \ + g_stringsize=string_size; \ + g_stacktop=stacktop; \ + g_variables=variables; } + +typedef struct _stack_t { + struct _stack_t *next; + TCHAR text[1]; // this should be the length of string_size +} stack_t; + +enum +{ +INST_0, // $0 +INST_1, // $1 +INST_2, // $2 +INST_3, // $3 +INST_4, // $4 +INST_5, // $5 +INST_6, // $6 +INST_7, // $7 +INST_8, // $8 +INST_9, // $9 +INST_R0, // $R0 +INST_R1, // $R1 +INST_R2, // $R2 +INST_R3, // $R3 +INST_R4, // $R4 +INST_R5, // $R5 +INST_R6, // $R6 +INST_R7, // $R7 +INST_R8, // $R8 +INST_R9, // $R9 +INST_CMDLINE, // $CMDLINE +INST_INSTDIR, // $INSTDIR +INST_OUTDIR, // $OUTDIR +INST_EXEDIR, // $EXEDIR +INST_LANG, // $LANGUAGE +__INST_LAST +}; + +extern unsigned int g_stringsize; +extern stack_t **g_stacktop; +extern TCHAR *g_variables; + +void NSISCALL pushstring(const TCHAR *str); +void NSISCALL pushintptr(INT_PTR value); +#define pushint(v) pushintptr((INT_PTR)(v)) +int NSISCALL popstring(TCHAR *str); // 0 on success, 1 on empty stack +int NSISCALL popstringn(TCHAR *str, int maxlen); // with length limit, pass 0 for g_stringsize +INT_PTR NSISCALL popintptr(); +#define popint() ( (int) popintptr() ) +int NSISCALL popint_or(); // with support for or'ing (2|4|8) +INT_PTR NSISCALL nsishelper_str_to_ptr(const TCHAR *s); +#define myatoi(s) ( (int) nsishelper_str_to_ptr(s) ) // converts a string to an integer +unsigned int NSISCALL myatou(const TCHAR *s); // converts a string to an unsigned integer, decimal only +int NSISCALL myatoi_or(const TCHAR *s); // with support for or'ing (2|4|8) +TCHAR* NSISCALL getuservariable(const int varnum); +void NSISCALL setuservariable(const int varnum, const TCHAR *var); + +#ifdef _UNICODE +#define PopStringW(x) popstring(x) +#define PushStringW(x) pushstring(x) +#define SetUserVariableW(x,y) setuservariable(x,y) + +int NSISCALL PopStringA(char* ansiStr); +void NSISCALL PushStringA(const char* ansiStr); +void NSISCALL GetUserVariableW(const int varnum, wchar_t* wideStr); +void NSISCALL GetUserVariableA(const int varnum, char* ansiStr); +void NSISCALL SetUserVariableA(const int varnum, const char* ansiStr); + +#else +// ANSI defs + +#define PopStringA(x) popstring(x) +#define PushStringA(x) pushstring(x) +#define SetUserVariableA(x,y) setuservariable(x,y) + +int NSISCALL PopStringW(wchar_t* wideStr); +void NSISCALL PushStringW(wchar_t* wideStr); +void NSISCALL GetUserVariableW(const int varnum, wchar_t* wideStr); +void NSISCALL GetUserVariableA(const int varnum, char* ansiStr); +void NSISCALL SetUserVariableW(const int varnum, const wchar_t* wideStr); + +#endif + +#ifdef __cplusplus +} +#endif + +#endif//!___NSIS_PLUGIN__H___