lang:csharp:windows
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| lang:csharp:windows [2018/08/27 16:55] – Ajout de "Accès bas niveau d'un disque dur" root | lang:csharp:windows [2020/10/26 16:01] (Version actuelle) – [Accès bas niveau d'un disque dur] : le lien d'origine n'est plus disponible root | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | =====Trouver la bonne déclaration pour appeler les DLL Windows===== | ||
| + | Télécharger la source du .NET depuis [[https:// | ||
| + | |||
| + | ====Mauvaises pratiques==== | ||
| + | ===IntPtr=== | ||
| + | Ne pas utiliser '' | ||
| + | <code csharp> | ||
| + | IntPtr iptr = ...; | ||
| + | HandleRef href = new HandleRef(null, | ||
| + | </ | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | ===LPCSTR et LPCWSTR=== | ||
| + | Utiliser '' | ||
| + | |||
| + | <code chsarp> | ||
| + | public static extern IntPtr FindWindowA([MarshalAs(UnmanagedType.LPStr)] string className, [MarshalAs(UnmanagedType.LPStr)] string windowName); | ||
| + | </ | ||
| + | |||
| + | Il est aussi possible de mettre directement '' | ||
| + | |||
| + | Si '' | ||
| + | |||
| =====Récupérer le nom d'un disque dur qui est affiché depuis l' | =====Récupérer le nom d'un disque dur qui est affiché depuis l' | ||
| Ne marche qu'à partir de Windows Vista. | Ne marche qu'à partir de Windows Vista. | ||
| - | [[https:// | + | [[https:// |
| <code csharp> | <code csharp> | ||
| public const string SHELL = " | public const string SHELL = " | ||
| Ligne 42: | Ligne 69: | ||
| =====Version de Windows===== | =====Version de Windows===== | ||
| - | [[https:// | ||
| - | |||
| <code csharp> | <code csharp> | ||
| OperatingSystem osVersionObj = Environment.OSVersion; | OperatingSystem osVersionObj = Environment.OSVersion; | ||
| Ligne 58: | Ligne 83: | ||
| Correspondance : | Correspondance : | ||
| - | [[https://msdn.microsoft.com/ | + | [[https://docs.microsoft.com/ |
| ^Operating system^Version number^ | ^Operating system^Version number^ | ||
| Ligne 78: | Ligne 103: | ||
| =====Accès bas niveau d'un disque dur===== | =====Accès bas niveau d'un disque dur===== | ||
| - | [[https:// | + | <del>[[https:// |
| + | |||
| + | =====Ne lancer une application qu'une seule fois===== | ||
| + | [[https:// | ||
| + | Dans '' | ||
| + | <code csharp> | ||
| + | [DllImport(" | ||
| + | private static extern bool SetForegroundWindow(IntPtr hWnd); | ||
| + | [DllImport(" | ||
| + | private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); | ||
| + | [DllImport(" | ||
| + | private static extern bool IsIconic(IntPtr hWnd); | ||
| + | |||
| + | private const int SW_RESTORE = 9; | ||
| + | |||
| + | public static void RaiseOtherProcess() | ||
| + | { | ||
| + | Process proc = Process.GetCurrentProcess(); | ||
| + | // Using Process.ProcessName does not function properly when | ||
| + | // the actual name exceeds 15 characters. Using the assembly | ||
| + | // name takes care of this quirk and is more accruate than | ||
| + | // other work arounds. | ||
| + | string assemblyName = Assembly.GetExecutingAssembly().GetName().Name; | ||
| + | foreach (Process otherProc in Process.GetProcessesByName(assemblyName)) | ||
| + | { | ||
| + | //ignore " | ||
| + | if (proc.Id != otherProc.Id) | ||
| + | { | ||
| + | // Found a "same named process" | ||
| + | // Assume it is the one we want brought to the foreground. | ||
| + | // Use the Win32 API to bring it to the foreground. | ||
| + | IntPtr hWnd = otherProc.MainWindowHandle; | ||
| + | if (IsIconic(hWnd)) | ||
| + | { | ||
| + | ShowWindowAsync(hWnd, | ||
| + | } | ||
| + | SetForegroundWindow(hWnd); | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | static void Main() | ||
| + | { | ||
| + | bool _owned; | ||
| + | Mutex _processSync = new Mutex(true, Assembly.GetExecutingAssembly().GetName().Name + " | ||
| + | |||
| + | if (!_owned) | ||
| + | { | ||
| + | RaiseOtherProcess(); | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | // La suite. | ||
| + | </ | ||
lang/csharp/windows.1535381729.txt.gz · Dernière modification : de root
