请问在VB中我可以让程序执行结束时,返回一个0或1的值吗?应该如何实现呢?

jeapvan 2006-08-28 04:09:35
请问在VB中我可以让程序执行结束时,返回一个0或1的值吗?应该如何实现呢?
...全文
337 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
benyfeifei 2006-08-30
  • 打赏
  • 举报
回复
谢谢pigsanddogs(我爱吃猪肉,但是长不胖,为什么??)提醒我什么叫懂什么叫不懂。
我不懂C和ASM,我知道VB的main是void。
liujingxiong 2006-08-30
  • 打赏
  • 举报
回复
程序执行结束时把返回的数值写到文件或指定的一个地方,让需要的自己去读出来。
boywang 2006-08-29
  • 打赏
  • 举报
回复
关键还是要知道程序能不能有返回值.
benyfeifei 2006-08-29
  • 打赏
  • 举报
回复
下面是个例子
Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Sub Form_Load()
'end this process
ExitProcess GetExitCodeProcess(GetCurrentProcess, 0)
End Sub
yinxu 2006-08-29
  • 打赏
  • 举报
回复
ExitProcess
说明
ExitProcess 函数 结束一个进程和进程打开的线程
声明
VOID ExitProcess(
UINT uExitCode // exit code for all threads
);

说明
中止一个进程
应尽量避免用该函数来关闭进程。
不要在自己的程序中使用它。此时,
应试着向要关闭的那个程序的主窗口投递一条WM_CLOSE消息
参数表
参数 类型及说明
uExitCode Long,指定想中断的那个程序的一个退出代码

不知道VB为什么不行!
benyfeifei 2006-08-29
  • 打赏
  • 举报
回复
如果一个进程的main函数是void类型的,ExitProcess 函数能让程序执行完返回一个值吗?
如果你的程序是一般的EXE的话,VB做不到。
jeapvan 2006-08-29
  • 打赏
  • 举报
回复
具体的应该如何做呢?
pigsanddogs 2006-08-29
  • 打赏
  • 举报
回复
抱歉,我原来没看清你的题目, 以为你是需要ExitProcess
如前面一朋友所说, vb不能调用ExitProcess的, 要不退出不完整。


上述代码测试通过。
由于vb编译环境中的保护。 请编译成exe运行。
现在你可以做个程序, 来得到这个exe的GetExitCodeProcess
正常途径是VB是没办法设置的。


benyfeifei(狒狒) :
ExitProcess GetExitCodeProcess(GetCurrentProcess, 0)
====================================================
我不知道这个起什么作用。
1:GetExitCodeProcess一直返回true的(如果不出错的话)
我不知道你把true传给ExitProcess有什么目的
2: 说后面那个0, 看下你的声明, 既然没有byval 为什么要给0
虽然vb给了一个临时可写的地址, 也没出问题。 但显然不符合你的要求
你显然是想给ExitProcess一个0, 同样错误的认为GetExitCodeProcess为1个0
总之, 完全错误的认识。



每个进程确实都有一个exitcode,但这不表示exitcode就是程序执行完的返回值啊。
main结束了确实不表示进程就立即结束,但main是void,msvcrt.dll再进行后续处理有什么用?它能返回一个你想要的值?
=========================
很明显,我看到main以为是c的main, 所以我想说 用int main() 就行了
但vb确实只能是void main. 这是我的失误
而你却完全没看明白, 顺着c去讨论了. c就是根据main的返回值来调用ExitProcess的
还说 "但main是void,怎么办"?
main是void你不会改成int main 啊?
既然你说怎么办, 又说明你认为是vb, 那你怎么又接着我说 msvcrt.dll呢
还有告诉你, 如果void main的话, 在c中可以在ret前 插入一条代码 __asm mov eax, 100
来设置返回值

综上所述, 你完全什么都不懂.
pigsanddogs 2006-08-29
  • 打赏
  • 举报
回复
Option Explicit
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Private Declare Function VirtualProtect Lib "kernel32" (ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flNewProtect As Long, lpflOldProtect As Long) As Long

Private Sub Command3_Click()
If SetExitCode(Text1.Text) = 0 Then
MsgBox "设置退出代码true"
Else
MsgBox "设置退出代码false"
End If
End Sub

'函数返回0成功. 1失败, exitcode应该小于&H80
Private Function SetExitCode(ByVal exitcode As Byte) As Long
Static patchAddr As Long
SetExitCode = 1
If patchAddr = 0 Then
Dim tmp As Long, hModuleVB As Long, searchLen As Long
Dim searchs1(3) As Byte, searchs2(7) As Byte, strobj(3) As Byte
Dim searchStr As String
Dim pehead As Long, oldprotect As Long

searchLen = &H10000
hModuleVB = GetModuleHandle("msvbvm60.dll")
If hModuleVB = 0 Then Exit Function
CopyMemory VarPtr(pehead), hModuleVB, 4
tmp = GetModuleHandle("Kernel32.dll")
If tmp = 0 Then Exit Function
tmp = GetProcAddress(tmp, "ExitProcess")
If tmp = 0 Then Exit Function
CopyMemory VarPtr(searchs1(0)), VarPtr(tmp), 4


'set the str point to image head, modify pehead to searchlen
VirtualProtect hModuleVB, 4, &H40, oldprotect
CopyMemory VarPtr(pehead), VarPtr(searchLen), 1
CopyMemory VarPtr(strobj(0)), VarPtr(searchStr), 4
hModuleVB = hModuleVB + 4
CopyMemory VarPtr(searchStr), VarPtr(hModuleVB), 4
hModuleVB = hModuleVB - 4

tmp = InStrB(1, searchStr, searchs1, vbBinaryCompare)


'restore the str point and the pehead
CopyMemory hModuleVB, VarPtr(pehead), 4
CopyMemory VarPtr(searchStr), VarPtr(strobj(0)), 4
VirtualProtect hModuleVB, 4, oldprotect, oldprotect

If tmp = 0 Then Exit Function
tmp = tmp - 1 + 4 + hModuleVB


searchs2(0) = &H6A
searchs2(1) = &H0
searchs2(2) = &HFF
searchs2(3) = &H15
CopyMemory VarPtr(searchs2(4)), VarPtr(tmp), 4

'set the str point to image head, modify pehead to searchlen
VirtualProtect hModuleVB, 4, &H40, oldprotect
CopyMemory VarPtr(pehead), VarPtr(searchLen), 1
CopyMemory VarPtr(strobj(0)), VarPtr(searchStr), 4
hModuleVB = hModuleVB + 4
CopyMemory VarPtr(searchStr), VarPtr(hModuleVB), 4
hModuleVB = hModuleVB - 4

tmp = InStrB(1, searchStr, searchs2, vbBinaryCompare)


'restore the str point and the pehead
CopyMemory VarPtr(searchStr), VarPtr(strobj(0)), 4
CopyMemory hModuleVB, VarPtr(pehead), 4
VirtualProtect hModuleVB, 4, oldprotect, oldprotect

If tmp = 0 Then Exit Function

patchAddr = tmp - 1 + 4 + hModuleVB + 1
End If

If exitcode >= &H80 Then Exit Function
VirtualProtect patchAddr, 1, &H40, oldprotect
CopyMemory patchAddr, VarPtr(exitcode), 1
VirtualProtect patchAddr, 1, oldprotect, oldprotect
SetExitCode = 0
End Function
yinxu 2006-08-29
  • 打赏
  • 举报
回复
ExitProcess,由于进程的强行退出,而有可能造成资源没有来得及释放,在这里我们可以理解为:
main函数还没有来得及正常结束的时候,进程实际已经结束了.那么和main函数是不是有返回值没有关系:因为即使是在C++或DELPHI这样的语言中,使用ExitProcess,也是一样可能造成main函数没有正常结束的,那么自然就不存在main的返回值的说法.
而我们使用GetExitCodeProcess函数却可以得到由ExitProcess函数执行之后的返回值,因为我们使用的是对内核对象操作的同一组函数.
请看下面链接,有一段:"VisualBasic 应用程序, 将源自超出调用 ExitProcess() 仅优点是可以设置为进程退出代码。"
http://support.microsoft.com/default.aspx?scid=kb%3Bzh-cn%3B288216
benyfeifei 2006-08-29
  • 打赏
  • 举报
回复
GetExitCodeProcess是get别人的, 你怎么能get自己的
----------------------------------
为什么能get别人的就不能get自己的?你可以试试这段代码,看看能不能get自己的。


任何一个进程退出都是最终调用ExitProcess而已, 任何一个进程退出一定有一个exitcode
你跟main扯什么关系。谁说的main结束了进程就结束了?
main的返回值不过是后来会传给msvcrt.dll来处理而已。
你写成void,表示不给eax值, 那eax原来是什么exitcode就返回什么。
----------------
每个进程确实都有一个exitcode,但这不表示exitcode就是程序执行完的返回值啊。
main结束了确实不表示进程就立即结束,但main是void,msvcrt.dll再进行后续处理有什么用?它能返回一个你想要的值?
pigsanddogs 2006-08-29
  • 打赏
  • 举报
回复
ExitProcess GetExitCodeProcess(GetCurrentProcess, 0)
===================================================
有这么用的吗?
GetExitCodeProcess是get别人的, 你怎么能get自己的。
你自己都没了还get个啥


如果一个进程的main函数是void类型的,ExitProcess 函数能让程序执行完返回一个值吗?
如果你的程序是一般的EXE的话,VB做不到。
===========================================
任何一个进程退出都是最终调用ExitProcess而已, 任何一个进程退出一定有一个exitcode
你跟main扯什么关系。谁说的main结束了进程就结束了?
main的返回值不过是后来会传给msvcrt.dll来处理而已。
你写成void,表示不给eax值, 那eax原来是什么exitcode就返回什么。
yinxu 2006-08-28
  • 打赏
  • 举报
回复
ExitProcess不过是个API,你在VB里面声明一下,就可以使用了!
jeapvan 2006-08-28
  • 打赏
  • 举报
回复
他没有一个象VC里的exitprocess(N)这样的函数吗?就是程序执行完成,我让他返回一个0或1
VOID ExitProcess(
UINT uExitCode // exit code for all threads
);

Parameters
uExitCode
Specifies the exit code for the process, and for all threads that are terminated as a result of this call. Use the GetExitCodeProcess function to retrieve the process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.
Return Values
This function does not return a value.

pigsanddogs 2006-08-28
  • 打赏
  • 举报
回复
ExitProcess(0)
ExitProcess(1)
jeapvan 2006-08-28
  • 打赏
  • 举报
回复
他没有一个象VC
zq972 2006-08-28
  • 打赏
  • 举报
回复
返回给谁?
是同一个程序的另外一个窗体么--dde或者一个全局变量
不是同一个程序 --写文件或者注册表
jeapvan 2006-08-28
  • 打赏
  • 举报
回复
TO 开心海:
那样不可以的,他们只是要求我们的程序执行后有一个0或1的返回值,这样他们根据这个值决定执行什么操作!有没有别的办法??
lizhongheng 2006-08-28
  • 打赏
  • 举报
回复
应用一个返回integer或boolean值得函数来执行程序中的过程
fantiny 2006-08-28
  • 打赏
  • 举报
回复
function a(byval b as integer) as integer
if b>0 then
a = 0
else
a = -1
end if
end function
加载更多回复(4)

7,789

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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