Outils pour utilisateurs

Outils du site


lang:csharp:windows

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
lang:csharp:windows [2018/01/15 13:29] – Ajout de "Version de Windows" rootlang: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://referencesource.microsoft.com/download.html|ReferenceSource]] et rechercher dans tous les fichiers ''UnsafeNativeMethods.cs'' ou dans le fichier ''win32native.cs''. Il y a presque tous les API Windows.
 +
 +====Mauvaises pratiques====
 +===IntPtr===
 +Ne pas utiliser ''IntPtr'' mais ''HandleRef'' pour les paramètres de type ''HWND'' selon la ''MSDN''.
 +<code csharp>
 +IntPtr iptr = ...;
 +HandleRef href = new HandleRef(null, iptr);
 +</code>
 +
 +[[https://stackoverflow.com/questions/526661/intptr-safehandle-and-handleref-explained|IntPtr, SafeHandle and HandleRef - Explained]] {{ :lang:csharp:windows:net_-_intptr_safehandle_and_handleref_-_explained_-_stack_overflow_2020-04-28_8_23_31_am_.html |Archive du 08/02/2009 le 28/04/2020}}
 +
 +[[https://stackoverflow.com/questions/27348119/safehandle-and-handleref|SafeHandle and HandleRef]] {{ :lang:csharp:windows:c_-_safehandle_and_handleref_-_stack_overflow_2020-04-28_8_23_48_am_.html |Archive du 07/12/2014 le 28/04/2020}}
 +
 +===LPCSTR et LPCWSTR===
 +Utiliser ''string'' à la place en spécifiant le ''Marshal'' pour chaque paramètre.
 +
 +<code chsarp>
 +public static extern IntPtr FindWindowA([MarshalAs(UnmanagedType.LPStr)] string className, [MarshalAs(UnmanagedType.LPStr)] string windowName);
 +</code>
 +
 +Il est aussi possible de mettre directement ''DllImport(CharSet = CharSet.Ansi)'' ou ''CharSet.Unicode'' mais l'analyseur statique de Visual Studio préfère l'utilisation de ''MarshalAs''.
 +
 +Si ''CharSet.Ansi'' est utilisé, il est préférable de rajouter ''DllImport(BestFitMapping = false, ThrowOnUnmappableChar = true)''.
 +
 =====Récupérer le nom d'un disque dur qui est affiché depuis l'explorateur Windows et qui est différent de celui du label du disque dur===== =====Récupérer le nom d'un disque dur qui est affiché depuis l'explorateur Windows et qui est différent de celui du label du disque dur=====
 Ne marche qu'à partir de Windows Vista. Ne marche qu'à partir de Windows Vista.
  
-[[https://stackoverflow.com/questions/2843935/get-drive-label-in-c-sharp|Get drive label in C#]] {{ :lang:csharp:windows:get_drive_label_in_c_-_stack_overflow.mhtml |Archive}}+[[https://stackoverflow.com/questions/2843935/get-drive-label-in-c-sharp|Get drive label in C#]] {{ :lang:csharp:windows:get_drive_label_in_c_-_stack_overflow_2020-04-28_8_24_02_am_.html |Archive du 16/05/2010 le 28/04/2020}} 
 <code csharp> <code csharp>
     public const string SHELL = "shell32.dll";     public const string SHELL = "shell32.dll";
Ligne 42: Ligne 69:
  
 =====Version de Windows===== =====Version de Windows=====
-[[https://code.msdn.microsoft.com/windowsapps/How-to-determine-the-263b1850/sourcecode?fileId=159408&pathId=547478701 |How to determine the Windows version by using Visual C#]] {{ :lang:csharp:windows:program.cs_-_how_to_determine_the_windows_version_by_using_visual_c_.mhtml |Archive}} 
- 
 <code csharp> <code csharp>
 OperatingSystem osVersionObj = Environment.OSVersion;  OperatingSystem osVersionObj = Environment.OSVersion; 
Ligne 58: Ligne 83:
 Correspondance : Correspondance :
  
-[[https://msdn.microsoft.com/library/windows/desktop/ms724832.aspx|Operating System Version]] {{ :lang:csharp:windows:operating_system_version_windows_.mhtml |Archive}}+[[https://docs.microsoft.com/en-us/windows/win32/sysinfo/operating-system-version|Operating System Version]] {{ :lang:csharp:windows:operating_system_version_-_win32_apps_microsoft_docs_2020-04-28_8_24_00_am_.html |Archive du 31/05/2018 le 28/04/2020}}
  
 ^Operating system^Version number^ ^Operating system^Version number^
Ligne 76: Ligne 101:
 |Windows XP|5.1| |Windows XP|5.1|
 |Windows 2000|5.0| |Windows 2000|5.0|
 +
 +=====Accès bas niveau d'un disque dur=====
 +<del>[[https://code.msdn.microsoft.com/windowsapps/CCS-LABS-C-Low-Level-Disk-91676ca9|CCS LABS C#: Low Level Disk Access]]</del> {{ :lang:csharp:windows:ccs_labs_c_23_low_level_disk_access.zip |Archive}}
 +
 +=====Ne lancer une application qu'une seule fois=====
 +[[https://www.codeproject.com/Articles/3014/Single-Process-Instance-Object|Single Process Instance Object]] {{ :lang:csharp:windows:single_process_instance_object_-_codeproject_2020-04-28_8_24_00_am_.html |Archive du 09/10/2002 le 28/04/2020}}
 +Dans ''Program.cs'' :
 +<code csharp>
 +[DllImport("user32.dll")]
 +private static extern bool SetForegroundWindow(IntPtr hWnd);
 +[DllImport("user32.dll")]
 +private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
 +[DllImport("user32.dll")]
 +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 "this" process
 +    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, SW_RESTORE);
 +      }
 +      SetForegroundWindow(hWnd);
 +      break;
 +    }
 +  }
 +}
 +
 +static void Main()
 +{
 +  bool _owned;
 +  Mutex _processSync = new Mutex(true, Assembly.GetExecutingAssembly().GetName().Name + "RDVision", out _owned);
 +
 +  if (!_owned)
 +  {
 +    RaiseOtherProcess();
 +    return;
 +  }
 +
 +  // La suite.
 +</code>
 +
lang/csharp/windows.1516019345.txt.gz · Dernière modification : 2018/01/15 13:29 de root