CreateToolhelp32Snapshot 按名称查ID?解决办法
CreateToolhelp32Snapshot 按名称查ID??
我按网上的资料写了一个按名称查进程ID,但是总是返回的是内存中没有进程,即(Process32First(handle, ref info)一直返回是0)我确定我PDA(MC35--WM6)中是有进程的
------解决方案--------------------
debug看看CreateToolhelp32Snapshot()返回的handle是不是有效的。
如果handle有效的话,再dllimport GetLastError()看看错误代码是什么,然后到WM SDK下的winerror.h文件中查找一下是什么问题。
------解决方案--------------------
C# 没搞过,不过c++调用肯定是没有问题的
------解决方案--------------------
dllimport改下试试:
[DllImport( "toolhelp.dll ")]
public static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);
[DllImport( "toolhelp.dll ")]
public static extern int Process32First(int snaph, ref PROCESSENTRY32 lppe);
[DllImport( "toolhelp.dll ")]
public static extern int Process32Next(IntPtr snaph, ref PROCESSENTRY32 lppe);
我按网上的资料写了一个按名称查进程ID,但是总是返回的是内存中没有进程,即(Process32First(handle, ref info)一直返回是0)我确定我PDA(MC35--WM6)中是有进程的
- C# code
[DllImport("toolhelp.dll")] private static extern int Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe); [DllImport("toolhelp.dll")] private static extern int Process32Next(IntPtr hSnapshot,ref PROCESSENTRY32 lppe); [DllImport("toolhelp.dll", SetLastError = true)] private static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags,uint th32ProcessID); private const uint TH32CS_SNAPPROCESS = 0x00000002; [StructLayout(LayoutKind.Sequential)] private struct PROCESSENTRY32 { public uint dwSize; public uint cntUsage; public uint th32ProcessID; public IntPtr th32DefaultHeapID; public uint th32ModuleID; public uint cntThreads; public uint th32ParentProcessID; public int pcPriClassBase; public uint dwFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szExeFile; } private void button1_Click(object sender, EventArgs e) { IntPtr handle = IntPtr.Zero; try { handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); PROCESSENTRY32 info = new PROCESSENTRY32(); info.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32)); int first = Process32First(handle, ref info); if (first == 0) MessageBox.Show("no pr"); else { do { if (string.Compare(info.szExeFile,"CamWedge.exe", true) == 0) MessageBox.Show("yes"); } while (Process32Next(handle, ref info) != 0); } } catch { throw; } }
------解决方案--------------------
debug看看CreateToolhelp32Snapshot()返回的handle是不是有效的。
如果handle有效的话,再dllimport GetLastError()看看错误代码是什么,然后到WM SDK下的winerror.h文件中查找一下是什么问题。
------解决方案--------------------
C# 没搞过,不过c++调用肯定是没有问题的
------解决方案--------------------
dllimport改下试试:
[DllImport( "toolhelp.dll ")]
public static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);
[DllImport( "toolhelp.dll ")]
public static extern int Process32First(int snaph, ref PROCESSENTRY32 lppe);
[DllImport( "toolhelp.dll ")]
public static extern int Process32Next(IntPtr snaph, ref PROCESSENTRY32 lppe);