可靠地找到进程的路径...Win XP、Vista 和 7
总之,
我在互联网上进行了研究,发现了很多潜在的解决方案,但我在 Windows 7 计算机上不断收到错误。
这是独家新闻。我有一个用 VB.NET 编写的应用程序,它启动一个控制台应用程序。控制台应用程序将独立于我的 VB.NET 应用程序运行,因此用户可以关闭并重新启动 VB.NET 应用程序,并可能从同一安装启动控制台应用程序的第二个会话。
我不想要这个。我想在 VB 应用程序启动时识别正在运行的控制台应用程序的实例,并禁用第二次、第三次等运行控制台应用程序的功能。时间。但是...我只想在控制台应用程序与 VB.NET 应用程序位于同一文件夹中启动时禁用此功能。
这是我当前使用的代码: 公共函数 CheckForConsoleApp() 作为布尔值 '检查控制台应用程序是否正在该文件夹中运行。如果是则返回 true。
Dim p() As System.Diagnostics.Process
p = System.Diagnostics.Process.GetProcessesByName("MyConsoleApp")
If p.Length = 0 Then return false
Dim Path As String = My.Application.Info.DirectoryPath
Dim i As Integer
For i = 0 To p.Length - 1
Dim pPath As String = p(i).MainModule.FileName
pPath = pPath.Substring(0, pPath.LastIndexOf("\"))
if pPath = Path Then Return True
Next i
Return False
End Function
在我的系统(32 位 Windows XP)上,这不是问题,并且每次都有效。在同事的 64 位 Windows 7 计算机上,生成错误,只能完成“ReadProcessMemory 或 WriteProcessMemory”的一部分。使用日志文件和其他努力表明:
Dim pPath As String = p(i).MainModule.FileName
行是发生故障的地方。
我们的应用程序必须在 Windows 7、Vista 和 XP 计算机上运行,无论是 32 位还是 64 位平台。我发现 WinAPI 命令可以将读/写权限应用于某些内存位置,但这些命令专门与 ReadProcessMemory API 命令一起使用,因此我还没有看到如何在 .NET 中应用类似的命令。
帮助? 谢谢, SH
All,
I have researched the Internet and found more than a few potential solutions, but I keep getting errors on a Windows 7 machine.
Here's the scoop. I have an application written in VB.NET that launches a console application. The console application will run independently of my VB.NET app, so the user can close and restart the VB.NET app and potentially start a second session of the console app from the same installation.
I don't want this. I want to identify instances of the running console app when my VB app starts, and disable the ability to run the console app a second, third,...etc. time. But...I only want to disable this ability when the console app has been launched from the same folder as the VB.NET app.
Here's the code I'm using currently:
Public Function CheckForConsoleApp() As Boolean
'Check to see if console app is running in this folder. Retrun true if so.
Dim p() As System.Diagnostics.Process
p = System.Diagnostics.Process.GetProcessesByName("MyConsoleApp")
If p.Length = 0 Then return false
Dim Path As String = My.Application.Info.DirectoryPath
Dim i As Integer
For i = 0 To p.Length - 1
Dim pPath As String = p(i).MainModule.FileName
pPath = pPath.Substring(0, pPath.LastIndexOf("\"))
if pPath = Path Then Return True
Next i
Return False
End Function
On my system (32 bit Windows XP) this is not a problem, and it works every time. On a co-worker's 64-bit Windows 7 machine, an error is generated that only a portion of the "ReadProcessMemory or WriteProcessMemory" can be completed. The use of log files and other efforts have shown that the:
Dim pPath As String = p(i).MainModule.FileName
line is where the failure occurs.
Our application has to work on Windows 7, Vista, and XP machines, on both 32-bit and 64-bit platforms. I've found WinAPI commands to apply read/write permissions to certain memory locations, but those were specifically used with ReadProcessMemory API commands, so I've yet to see how to apply similar commands in .NET.
Help?
Thanks,
SH
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道您是否找到了解决方案,但您需要将构建选项设置为“任何 CPU”而不是 X86,那么它对我来说效果很好。
I don't know if you found your solution, but you need to set your Build Options to "Any CPU" and not X86 then it works fine for me.
我几天甚至几周以来一直在寻找这个问题的解决方案。我昨天放弃了希望,并在这里发布了一个问题,今天重新措辞了我的搜索,找到了听起来像答案的内容。
其要点是,在其他进程上查找数据的进程是一个 x86 进程,并且正在尝试在 x64 进程上查找数据。这就是我的申请失败的原因。有人有如何解决它的建议吗?
http://social .msdn.microsoft.com/Forums/en/vbgeneral/thread/ca5fc242-fbc0-4b5b-be86-4cbd3824211b?prof=required
这是一个相当长的线程,但如果您搜索“Charl”(Charl 的名字)该帖子的作者)您会找到我认为是我的问题的答案。它可能不适用于该问题的所有其他情况,但我认为这就是我的情况。
SH
I have been looking for the solution to this problem for days...weeks even. I gave up hope yesterday and posted a question here, and today reworded my search and found what sounds like an answer.
The gist of it is that the process looking for data on other processes is an x86 process and is trying to find data on x64 processes. This is why my application fails. Does anyone have a suggestion how to get around it?
http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/ca5fc242-fbc0-4b5b-be86-4cbd3824211b?prof=required
It's a rather long thread, but if you search for "Charl" (the name of the post's author) you'll find what I think is the answer to my question. It may not apply to all other occurrences of the issue, but I think this is what's happening with mine.
SH