尝试从我从 VB6 运行的 .NET 可执行文件获取错误代码
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 than32
) or a failure (32
or less). One could figure that out either by reading the documentation or observing the fact thatShellExecute
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
, notShellExecute
.Do not forget calling
CloseHandle
on both process and thread handles it returns.对于那些在我们先进的时代仍然受到 VB6 影响的人,我想发布一个大部分完整的示例。以下站点给出的代码很棒: http://vbnet.mvps .org/index.html?code/faq/waitforsingleobject2.htm,但是您必须将 2 个示例放在一起以获得一个工作位,该工作位将等待加壳应用程序退出而不进入 CPU 密集型循环,并且获取正确的退出代码。
这里重要的一点对我来说并不是立即显而易见的是,您必须使用 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.
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.