尝试从我从 VB6 运行的 .NET 可执行文件获取错误代码

发布于 2024-10-28 01:17:38 字数 500 浏览 3 评论 0原文

我使用 ShellExecute API 调用从 VB6 执行 .NET 控制台应用程序:

ExitCode = ShellExecute(Me.hWnd, "open", GetAppPath & "\SQL Utilities\" & "DocXferClient.exe", strFlags, vbNull, SW_HIDE)

在 .NET 应用程序 (DocXFerClient) 中,我发送尝试使用 Environment.ExitCode 返回“错误代码”:

Sub Main()
    BuildConnectionObject()
    ProcessRequest()

    Environment.ExitCode = 55 
End Sub

但是,无论我尝试返回什么错误代码(在在本例中为“55”),VB6 中的 ExitCode 始终为“42”。我是否在 VB6 端、.NET 端或两者都做错了什么?

I execute a .NET console app from wihin VB6 using the ShellExecute API call:

ExitCode = ShellExecute(Me.hWnd, "open", GetAppPath & "\SQL Utilities\" & "DocXferClient.exe", strFlags, vbNull, SW_HIDE)

Within the .NET app (DocXFerClient), I send attempt to return an "error code" using Environment.ExitCode:

Sub Main()
    BuildConnectionObject()
    ProcessRequest()

    Environment.ExitCode = 55 
End Sub

However, whatever error code I try returning (in this case "55"), the ExitCode within VB6 is always "42." Am I doing something wrong on the VB6 side, the .NET side, or both?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅浅淡淡 2024-11-04 01:17:39

ShellExecute 不返回退出代码。它返回成功(值大于 32)或失败(32 或更小)。人们可以通过阅读文档或观察以下事实来弄清楚这一点: ShellExecute 在进程退出之前返回其值。

强制性 Raymond Chen 链接:我可以用返回的 HINSTANCE 做什么通过 ShellExecute 函数?


要获取退出代码,请使用 GetExitCodeProcess

要获取进程句柄,请使用 CreateProcess< 运行应用程序/a>,而不是 ShellExecute

不要忘记在两个进程上调用 CloseHandle并线程处理它的返回。

ShellExecute does not return an exit code. It returs a success (value greater than 32) or a failure (32 or less). One could figure that out either by reading the documentation or observing the fact that ShellExecute returns its value before the process exits.

Obligatory Raymond Chen link: What can I do with the HINSTANCE returned by the ShellExecute function?


To get the exit code, use GetExitCodeProcess.

To get the process handle, run your app with CreateProcess, not ShellExecute.

Do not forget calling CloseHandle on both process and thread handles it returns.

小忆控 2024-11-04 01:17:39

对于那些在我们先进的时代仍然受到 VB6 影响的人,我想发布一个大部分完整的示例。以下站点给出的代码很棒: http://vbnet.mvps .org/index.html?code/faq/waitforsingleobject2.htm,但是您必须将 2 个示例放在一起以获得一个工作位,该工作位将等待加壳应用程序退出而不进入 CPU 密集型循环,并且获取正确的退出代码。

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const SYNCHRONIZE = &H100000
Private Const WAIT_INFINITE = -1&
Private Const PROCESS_QUERY_INFORMATION = &H400

ProcessId = Shell("D:\iwCode\dev\proj\appmanager\MoveIniFileUtil\bin\Debug\MoveIniFileUtil.exe 5", vbNormalFocus)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or SYNCHRONIZE, True, ProcessId)
dummyRet = WaitForSingleObject(hProcess, WAIT_INFINITE)
GetExitCodeProcess hProcess, exitCode
CloseHandle hProcess
MsgBox exitCode

这里重要的一点对我来说并不是立即显而易见的是,您必须使用 SYNCHRONIZE 和 PROCESS_QUERY_INFORMATION 标志或组合在一起调用 OpenProcess 函数才能使其正常工作。

For those still subjected to VB6 in our advanced times I would like to post a mostly-complete example. The code given at the following site is great: http://vbnet.mvps.org/index.html?code/faq/waitforsingleobject2.htm, but you have to put 2 of the examples together to get a working bit that will wait for the shelled app to exit without entering a CPU intensive loop and get a correct exit code.

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const SYNCHRONIZE = &H100000
Private Const WAIT_INFINITE = -1&
Private Const PROCESS_QUERY_INFORMATION = &H400

ProcessId = Shell("D:\iwCode\dev\proj\appmanager\MoveIniFileUtil\bin\Debug\MoveIniFileUtil.exe 5", vbNormalFocus)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or SYNCHRONIZE, True, ProcessId)
dummyRet = WaitForSingleObject(hProcess, WAIT_INFINITE)
GetExitCodeProcess hProcess, exitCode
CloseHandle hProcess
MsgBox exitCode

The important bit here that wasn't immediately apparent to me was that you must call the OpenProcess function with the SYNCHRONIZE and PROCESS_QUERY_INFORMATION flags Or'ed together to get this to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文