[VB .NET] Comment utiliser une classe écrite en C#

Comment utiliser une classe écrite en C# [VB .NET] - C#/.NET managed - Programmation

Marsh Posté le 06-10-2008 à 17:15:00    

Bonjour,
 
Je développe une petite appli pour un Windows CE, avec VS 2008, en VB .NET. J'ai besoin d'accéder aux process du Windows CE mais la fonction System.Diagnostics.Process.GetProcessesByName n'existe pas. Donc je voudrais utiliser une classe que j'ai vue sur MSDN :
 
http://msdn.microsoft.com/en-us/library/aa446560.aspx
 
J'ai téléchargé le source de la classe Process.cs (cf à la fin) et je l'ai ajouté à mon projet VB, mais je n'arrive pas à instancier un objet avec cette classe, style  
 
Dim p as new Process
 
Quelqu'un sait comme je peux m'en sortir et utiliser une classe écrite en C# en VB ?
 
Merci
 
Matz
 
Code de la classe :

Code :
  1. using System;
  2. using System.Collections;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace ToolHelp
  6. {
  7. #region Process class
  8. /// <summary>
  9. /// Summary description for Process.
  10. /// </summary>
  11. public class Process
  12. {
  13.  private string processName;
  14.  private IntPtr handle;
  15.  private int threadCount;
  16.  private int baseAddress;
  17.  //default constructor
  18.  public Process()
  19.  {
  20.  }
  21.  private Process(IntPtr id, string procname, int threadcount, int baseaddress)
  22.  {
  23.   handle = id;
  24.   processName = procname;
  25.   threadCount = threadcount;
  26.   baseAddress = baseaddress;
  27.  }
  28.  //ToString implementation for ListBox binding
  29.  public override string ToString()
  30.  {
  31.   return processName;
  32.  }
  33.  public int BaseAddress
  34.  {
  35.   get
  36.   {
  37.    return baseAddress;
  38.   }
  39.  }
  40.  public int ThreadCount
  41.  {
  42.   get
  43.   {
  44.    return threadCount;
  45.   }
  46.  }
  47.  public IntPtr Handle
  48.  {
  49.   get
  50.   {
  51.    return handle;
  52.   }
  53.  }
  54.  public string ProcessName
  55.  {
  56.   get
  57.   {
  58.    return processName;
  59.   }
  60.  }
  61.  public int BaseAddess
  62.  {
  63.   get
  64.   {
  65.    return baseAddress;
  66.   }
  67.  }
  68.  public void Kill()
  69.  {
  70.   IntPtr hProcess;
  71.   hProcess = OpenProcess(PROCESS_TERMINATE, false, (int) handle);
  72.   if(hProcess != (IntPtr) INVALID_HANDLE_VALUE)
  73.   {
  74.    bool bRet;
  75.    bRet = TerminateProcess(hProcess, 0);
  76.    CloseHandle(hProcess);
  77.   }
  78.  }
  79.  public static Process[] GetProcesses()
  80.  {
  81.   ArrayList procList = new ArrayList();
  82.   IntPtr handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  83.   if ((int)handle > 0)
  84.   {
  85.    try
  86.    {
  87.     PROCESSENTRY32 peCurrent;
  88.     PROCESSENTRY32 pe32 = new PROCESSENTRY32();
  89.     //Get byte array to pass to the API calls
  90.     byte[] peBytes = pe32.ToByteArray();
  91.     //Get the first process
  92.     int retval = Process32First(handle, peBytes);
  93.     while(retval == 1)
  94.     {
  95.      //Convert bytes to the class
  96.      peCurrent = new PROCESSENTRY32(peBytes);
  97.      //New instance
  98.      Process proc = new Process(new IntPtr((int)peCurrent.PID), peCurrent.Name, (int)peCurrent.ThreadCount, (int)peCurrent.BaseAddress);
  99.      procList.Add(proc);
  100.      retval = Process32Next(handle, peBytes);
  101.     }
  102.    }
  103.    catch(Exception ex)
  104.    {
  105.     throw new Exception("Exception: " + ex.Message);
  106.    }
  107.    //Close handle
  108.    CloseToolhelp32Snapshot(handle);
  109.    return (Process[])procList.ToArray(typeof(Process));
  110.   }
  111.   else
  112.   {
  113.    throw new Exception("Unable to create snapshot" );
  114.   }
  115.  }
  116.  #endregion
  117.  #region PROCESSENTRY32 implementation
  118. //  typedef struct tagPROCESSENTRY32  
  119. //  {  
  120. //   DWORD dwSize;  
  121. //   DWORD cntUsage;  
  122. //   DWORD th32ProcessID;  
  123. //   DWORD th32DefaultHeapID;  
  124. //   DWORD th32ModuleID;  
  125. //   DWORD cntThreads;  
  126. //   DWORD th32ParentProcessID;  
  127. //   LONG pcPriClassBase;  
  128. //   DWORD dwFlags;  
  129. //   TCHAR szExeFile[MAX_PATH];  
  130. //   DWORD th32MemoryBase;
  131. //   DWORD th32AccessKey;
  132. //  } PROCESSENTRY32;
  133.  private class PROCESSENTRY32
  134.  {
  135.   // constants for structure definition
  136.   private const int SizeOffset = 0;
  137.   private const int UsageOffset = 4;
  138.   private const int ProcessIDOffset=8;
  139.   private const int DefaultHeapIDOffset = 12;
  140.   private const int ModuleIDOffset = 16;
  141.   private const int ThreadsOffset = 20;
  142.   private const int ParentProcessIDOffset = 24;
  143.   private const int PriClassBaseOffset = 28;
  144.   private const int dwFlagsOffset = 32;
  145.   private const int ExeFileOffset = 36;
  146.   private const int MemoryBaseOffset = 556;
  147.   private const int AccessKeyOffset = 560;
  148.   private const int Size = 564;
  149.   private const int MAX_PATH = 260;
  150.   // data members
  151.   public uint dwSize;
  152.   public uint cntUsage;
  153.   public uint th32ProcessID;
  154.   public uint th32DefaultHeapID;
  155.   public uint th32ModuleID;
  156.   public uint cntThreads;
  157.   public uint th32ParentProcessID;
  158.   public long pcPriClassBase;
  159.   public uint dwFlags;
  160.   public string szExeFile;
  161.   public uint th32MemoryBase;
  162.   public uint th32AccessKey;
  163.   //Default constructor
  164.   public PROCESSENTRY32()
  165.   {
  166.   }
  167.   // create a PROCESSENTRY instance based on a byte array  
  168.   public PROCESSENTRY32(byte[] aData)
  169.   {
  170.    dwSize = GetUInt(aData, SizeOffset);
  171.    cntUsage = GetUInt(aData, UsageOffset);
  172.    th32ProcessID = GetUInt(aData, ProcessIDOffset);
  173.    th32DefaultHeapID = GetUInt(aData, DefaultHeapIDOffset);
  174.    th32ModuleID = GetUInt(aData, ModuleIDOffset);
  175.    cntThreads = GetUInt(aData, ThreadsOffset);
  176.    th32ParentProcessID = GetUInt(aData, ParentProcessIDOffset);
  177.    pcPriClassBase = (long) GetUInt(aData, PriClassBaseOffset);
  178.    dwFlags = GetUInt(aData, dwFlagsOffset);
  179.    szExeFile = GetString(aData, ExeFileOffset, MAX_PATH);
  180.    th32MemoryBase = GetUInt(aData, MemoryBaseOffset);
  181.    th32AccessKey = GetUInt(aData, AccessKeyOffset);
  182.   }
  183.   #region Helper conversion functions
  184.   // utility:  get a uint from the byte array
  185.   private static uint GetUInt(byte[] aData, int Offset)
  186.   {
  187.    return BitConverter.ToUInt32(aData, Offset);
  188.   }
  189.   // utility:  set a uint int the byte array
  190.   private static void SetUInt(byte[] aData, int Offset, int Value)
  191.   {
  192.    byte[] buint = BitConverter.GetBytes(Value);
  193.    Buffer.BlockCopy(buint, 0, aData, Offset, buint.Length);
  194.   }
  195.   // utility:  get a ushort from the byte array
  196.   private static ushort GetUShort(byte[] aData, int Offset)
  197.   {
  198.    return BitConverter.ToUInt16(aData, Offset);
  199.   }
  200.   // utility:  set a ushort int the byte array
  201.   private static void SetUShort(byte[] aData, int Offset, int Value)
  202.   {
  203.    byte[] bushort = BitConverter.GetBytes((short)Value);
  204.    Buffer.BlockCopy(bushort, 0, aData, Offset, bushort.Length);
  205.   }
  206.   // utility:  get a unicode string from the byte array
  207.   private static string GetString(byte[] aData, int Offset, int Length)
  208.   {
  209.    String sReturn =  Encoding.Unicode.GetString(aData, Offset, Length);
  210.    return sReturn;
  211.   }
  212.   // utility:  set a unicode string in the byte array
  213.   private static void SetString(byte[] aData, int Offset, string Value)
  214.   {
  215.    byte[] arr = Encoding.ASCII.GetBytes(Value);
  216.    Buffer.BlockCopy(arr, 0, aData, Offset, arr.Length);
  217.   }
  218.   #endregion
  219.   // create an initialized data array
  220.   public byte[] ToByteArray()
  221.   {
  222.    byte[] aData;
  223.    aData = new byte[Size];
  224.    //set the Size member
  225.    SetUInt(aData, SizeOffset, Size);
  226.    return aData;
  227.   }
  228.   public string Name
  229.   {
  230.    get
  231.    {
  232.     return szExeFile.Substring(0, szExeFile.IndexOf('\0'));
  233.    }
  234.   }
  235.   public ulong PID
  236.   {
  237.    get
  238.    {
  239.     return th32ProcessID;
  240.    }
  241.   }
  242.   public ulong BaseAddress
  243.   {
  244.    get
  245.    {
  246.     return th32MemoryBase;
  247.    }
  248.   }
  249.   public ulong ThreadCount
  250.   {
  251.    get
  252.    {
  253.     return cntThreads;
  254.    }
  255.   }
  256.  }
  257.  #endregion
  258.  #region PInvoke declarations
  259.  private const int TH32CS_SNAPPROCESS = 0x00000002;
  260.  [DllImport("toolhelp.dll" )]
  261.  public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid);
  262.  [DllImport("toolhelp.dll" )]
  263.  public static extern int CloseToolhelp32Snapshot(IntPtr handle);
  264.  [DllImport("toolhelp.dll" )]
  265.  public static extern int Process32First(IntPtr handle, byte[] pe);
  266.  [DllImport("toolhelp.dll" )]
  267.  public static extern int Process32Next(IntPtr handle, byte[] pe);
  268.  [DllImport("coredll.dll" )]
  269.  private static extern IntPtr OpenProcess(int flags, bool fInherit, int PID);
  270.  private const int PROCESS_TERMINATE = 1;
  271.  [DllImport("coredll.dll" )]
  272.  private static extern bool TerminateProcess(IntPtr hProcess, uint ExitCode);
  273.  [DllImport("coredll.dll" )]
  274.  private static extern bool CloseHandle(IntPtr handle);
  275.  private const int INVALID_HANDLE_VALUE = -1;
  276.  #endregion
  277. }
  278. }

Reply

Marsh Posté le 06-10-2008 à 17:15:00   

Reply

Marsh Posté le 07-10-2008 à 00:09:31    

il faut compiler ta classe C# en .netmodule et la joindre à ton projet. regarde ici pour plus d'infos
http://blogs.msdn.com/junfeng/arch [...] 71683.aspx


---------------
J'ai un string dans l'array (Paris Hilton)
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed