C#中使用API函数的问题

mashuyi 2007-09-21 11:18:46
最近搞Wince开发
需要根据exe文件名字取得相应的进程ID
看了下精简框架中的Process类 没有方法:GetProcessesByName(),和GetProcesses()于是只能用API函数 凸-_-

找到了如下3个API函数能完成任务 但是不知道怎么使用,请大家帮忙!

CreateToolhelp32Snapshot
Process32First
Process32Next
...全文
581 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
keyboarduser 2007-09-21
  • 打赏
  • 举报
回复
顺路发个,谁作wince,ppc的同行。加我QQ:32610303
KKND2006 2007-09-21
  • 打赏
  • 举报
回复
//(2)Find Process
HANDLE hProcessSnap = NULL;
PROCESSENTRY32 oPROCESSENTRY32 = {0};

//Get Process Handle
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (hProcessSnap == (HANDLE)-1)
{
return 1;
}

//Must Set this
oPROCESSENTRY32.dwSize = sizeof(PROCESSENTRY32);

//Enum Process
if (Process32First(hProcessSnap, &oPROCESSENTRY32) == false)
{
CloseHandle(hProcessSnap);
return 1;
}

bool boolFindflag = false;
while(TRUE)
{
if (strcmp(cstrProcessImageName_In, oPROCESSENTRY32.szExeFile) == 0)
{
for(int intIndex = 0; intIndex <= pCArray_DWORD_ProcID.GetUpperBound(); intIndex++)
{
if(pCArray_DWORD_ProcID.GetAt(intIndex) == oPROCESSENTRY32.th32ProcessID)
{
AfxMessageBox(pCArray_CString_WindowTitle.GetAt(intIndex));

boolFindflag = true;
break;
}
}

if(boolFindflag == true)
{
break;
}
}

if(Process32Next(hProcessSnap, &oPROCESSENTRY32) == FALSE)
{
break;
}
}

CloseHandle(hProcessSnap);
keyboarduser 2007-09-21
  • 打赏
  • 举报
回复
/// <summary>
/// Wrapper around the ToolHelp ProcessEntry information
/// </summary>
/// <remarks>
/// This class requires the toolhelp32.dll
/// </remarks>
public class ProcessEntry
{
private PROCESSENTRY32 m_pe;

private ProcessEntry(PROCESSENTRY32 pe)
{
m_pe = pe;
}

/// <summary>
/// Get the short name of the current process
/// </summary>
/// <returns>The current process name</returns>
public override string ToString()
{
return System.IO.Path.GetFileName(m_pe.ExeFile);
}

/// <summary>
/// Base address for the process
/// </summary>
[CLSCompliant(false)]
public uint BaseAddress
{
get { return m_pe.BaseAddress; }
}

/// <summary>
/// Number of execution threads started by the process.
/// </summary>
public int ThreadCount
{
get { return (int)m_pe.cntThreads; }
}

/// <summary>
/// Identifier of the process. The contents of this member can be used by Win32 API elements.
/// </summary>
[CLSCompliant(false)]
public uint ProcessID
{
get { return (uint)m_pe.ProcessID; }
}

/// <summary>
/// Null-terminated string that contains the path and file name of the executable file for the process.
/// </summary>
public string ExeFile
{
get { return m_pe.ExeFile; }
}

/// <summary>
/// Kill the Process
/// </summary>
public void Kill()
{
IntPtr hProcess;

hProcess = Process.OpenProcess(Util.PROCESS_TERMINATE, false, this.ProcessID);

if(hProcess.ToInt32() != Util.INVALID_HANDLE_VALUE)
{
bool bRet;
bRet = Process.TerminateProcess(hProcess, 0);
Process.CloseHandle(hProcess);
}
}

/// <summary>
/// Rerieves an array of all running processes
/// </summary>
/// <returns></returns>
public static ProcessEntry[] GetProcesses()
{
ArrayList procList = new ArrayList();

IntPtr handle = Util.CreateToolhelp32Snapshot(Util.TH32CS_SNAPPROCESS, 0);

if ((int)handle > 0)
{
try
{
PROCESSENTRY32 peCurrent;
PROCESSENTRY32 pe32 = new PROCESSENTRY32();
//Get byte array to pass to the API calls
byte[] peBytes = pe32.ToByteArray();
//Get the first process
int retval = Util.Process32First(handle, peBytes);

while(retval == 1)
{
//Convert bytes to the class
peCurrent = new PROCESSENTRY32(peBytes);
//New instance
ProcessEntry proc = new ProcessEntry(peCurrent);

procList.Add(proc);

retval = Util.Process32Next(handle, peBytes);
}
}
catch(Exception ex)
{
throw new Exception("Exception: " + ex.Message);
}

//Close handle
Util.CloseToolhelp32Snapshot(handle);

return (ProcessEntry[])procList.ToArray(typeof(ProcessEntry));

}
else
{
throw new Exception("Unable to create snapshot");
}


}

}
keyboarduser 2007-09-21
  • 打赏
  • 举报
回复
找了一个
Tassadar1979 2007-09-21
  • 打赏
  • 举报
回复
哇,看见个信誉1的.
Tassadar1979 2007-09-21
  • 打赏
  • 举报
回复

[StructLayout(LayoutKind.Sequential)]
public 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=256)]
public string szExeFile;
};


[DllImport("KERNEL32.DLL")]
public static extern int CreateToolhelp32Snapshot(uint flags, uint processid);

[DllImport("KERNEL32.DLL")]
public static extern int Process32First(int handle, ref ProcessEntry32 pe);

[DllImport("KERNEL32.DLL")]
public static extern int Process32Next(int handle, ref ProcessEntry32 pe);
keyboarduser 2007-09-21
  • 打赏
  • 举报
回复
弄到c#里,也不麻烦。不过呢。我没功夫写。
wdzr_826 2007-09-21
  • 打赏
  • 举报
回复
mark
keyboarduser 2007-09-21
  • 打赏
  • 举报
回复
int hSnapshot = CreateToolhelp32Snapshot(2,0);
PROCESSENTRY32 processStruct;
processStruct.dwSize = sizeof(processStruct);
if(Process32First(hSnapshot,&processStruct))
{
while (Process32Next(hSnapshot,&processStruct))
{
//processStruct.szExeFile 这就是进程的启动文件名
}
}
CloseToolhelp32Snapshot(hSnapshot);
keyboarduser 2007-09-21
  • 打赏
  • 举报
回复
的确,你方法没错。就这3个api
brucenan999 2007-09-21
  • 打赏
  • 举报
回复
找个PInvoke,转转看.
skyell 2007-09-21
  • 打赏
  • 举报
回复
学习下。。。
keyboarduser 2007-09-21
  • 打赏
  • 举报
回复
日后再回答楼猪的问题。我宁可扔了这最后一点信誉!
keyboarduser 2007-09-21
  • 打赏
  • 举报
回复
垃圾楼猪。
keyboarduser 2007-09-21
  • 打赏
  • 举报
回复
mashuyi() ( ) 信誉:100 2007-9-21 14:33:46 得分: 0



to: keyboarduser()

你那个PROCESSENTRY32 的声明在哪?
叫我怎么看懂?




注释你不会看么???第一行就写的有!!!!!!!!!!


/// <summary>
/// Wrapper around the ToolHelp ProcessEntry information
/// </summary>
/// <remarks>
/// This class requires the toolhelp32.dll
/// </remarks>



貌似你很委屈啊。老子给你回答个问题回答错了,看错人了。
mashuyi 2007-09-21
  • 打赏
  • 举报
回复
问题解决了 谢谢Tassadar1979(一万年的期待!),sfalliance 这俩位兄弟
mashuyi 2007-09-21
  • 打赏
  • 举报
回复
to: keyboarduser()

你那个PROCESSENTRY32 的声明在哪?
叫我怎么看懂?
sfalliance 2007-09-21
  • 打赏
  • 举报
回复
DWORD和Uint都表示整数
只是根据操作系统的位数不同会有区别
32位操作系统下Uint和DWORD是4个字节
所以在32位操作系统下可以用Uint替换DWORD
sfalliance 2007-09-21
  • 打赏
  • 举报
回复
7楼的兄弟这个结构体没错
[StructLayout(LayoutKind.Sequential)]
public 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=256)]
public string szExeFile;
};

返回0的原因是因为dwSize值。
你在调用Process32First之前给他设置值 1024试试

另外这3个方法在wince中的toolhelp.dll中。

[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);
mythzz9236 2007-09-21
  • 打赏
  • 举报
回复
学习
加载更多回复(4)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧