一个获取程序是否运行在VB的IDE开发环境的函数Option ExplicitPrivate Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long Public Function GetIDEmode() As Boolean Dim strFileName As String Dim lngCount As Long strFileName = String(255, 0) lngCount = GetModuleFileName(App.hInstance, strFileName, 255) strFileName = Left(strFileName, lngCount) If UCase(Right(strFileName, 7)) <> "VB6.EXE" Then GetIDEmode = False '是 Else GetIDEmode = True '否 End If End Function
CSDN里有现成的啊,看来AisaC只是为了单纯地散分,俺就来单纯地接分了。
If GetObject("", "Scripting.FileSystemObject").FileExists(App.Path & "\" & App.EXEName & ".exe") Then MsgBox "exe" Else MsgBox "ide" End If
楼上几位整的复杂了吧,并且有局限性。看这个:Public Function IsIDE() As Boolean On Error GoTo r: Dim i As Long Debug.Print 1 / 0 Exit Function r: IsIDE = True End Function
多了一行代码,看这个:Public Function IsIDE() As Boolean On Error GoTo r: Dim i As Long Debug.Print 1 / 0 Exit Function r: IsIDE = True End Function
OMG 又错了 自行去除:Dim i As Long 多余的代码
像是有个比较“恶心”的办法,看看debug能否成功。
private function isIDE() as boolean dim bln as boolen debug.assert tureit(bln) isIDE = bln end functionprivate sub tureit(bln as boolean) bln = true end sub
Public Function GetIDEmode() As Boolean
Dim strFileName As String
Dim lngCount As Long
strFileName = String(255, 0)
lngCount = GetModuleFileName(App.hInstance, strFileName, 255)
strFileName = Left(strFileName, lngCount)
If UCase(Right(strFileName, 7)) <> "VB6.EXE" Then
GetIDEmode = False '是
Else
GetIDEmode = True '否
End If
End Function
MsgBox "exe"
Else
MsgBox "ide"
End If
On Error GoTo r:
Dim i As Long
Debug.Print 1 / 0
Exit Function
r:
IsIDE = True
End Function
On Error GoTo r:
Dim i As Long
Debug.Print 1 / 0
Exit Function
r:
IsIDE = True
End Function
自行去除:Dim i As Long
多余的代码
dim bln as boolen
debug.assert tureit(bln)
isIDE = bln
end functionprivate sub tureit(bln as boolean)
bln = true
end sub
巧妙!IDE下是要执行Debug.Print的,而编译成EXE是要忽略Debug.Print!
'------------------------------------------------------------------------------'
' 功能: 检查当前是否运行在 VB-IDE 中
' 返回: 是/否
'------------------------------------------------------------------------------'
Function InIDE() As Boolean
Static s_bFlag As Boolean Debug.Assert InIDECalled(s_bFlag)
InIDE = s_bFlag
End Function'------------------------------------------------------------------------------'
' 功能: 仅供 InIDE() 调用
' 参数:
' [O] bFlag : 标志
' 返回: 始终为 True,保证断言为真
'------------------------------------------------------------------------------'
Private Function InIDECalled(ByRef bFlag As Boolean) As Boolean
bFlag = True
InIDECalled = True
End Function
Debug.print 1/0来触发错误