Post by Nick TIs there a way to establish if VB6 code is running in the IDE. I need to
control some aspects of the VB6 legacy application when it's running in the
IDE.
The solutions you've gotten are fine if your project is a standard EXE. If
it is, I suggest you use them as they're simple and very small. However, if
this project is anything other than a standard EXE you may want something
more robust. For example, if you project is a DLL, then the examples will
work fine ONLY if the DLL project itself is running in the IDE. If you need
to determine if your COMPILED DLL is being used by a project in the IDE,
none of them will work. Here's a function you can use that will.
----------BEGIN CODE
Private Declare Function GetModuleHandle Lib "kernel32" Alias
"GetModuleHandleA" (ByVal lpModuleName As String) As Long
Public Function IsIDE() As Boolean
'This function attempts to determine if the component is being
'instantiated within any of a number of "development" environments.
'These include VB, VC++, Internet Explorer, and Frontpage, among others.
'The "trick" is really pretty simple. All ActiveX DLLs and OCXs run
'in the same process space as their client. The GetModuleHandle API
'function is called for each development environment. If
GetModuleHandle
'returns a value greater than 0, the DLL/OCX is running in the process
'space of that environment.
Dim hVB432 As Long
Dim hVB416 As Long
Dim hVB5 As Long
Dim hVB6 As Long
Dim hIE As Long
Dim hVC As Long
Dim hMSDev As Long
Dim hFP As Long
Dim hFPE As Long
hVB432 = GetModuleHandle("VB32.EXE")
hVB416 = GetModuleHandle("VB.EXE")
hVB5 = GetModuleHandle("VB5.EXE")
hVB6 = GetModuleHandle("VB6.EXE")
IsIDE = (hVB432 > 0 Or hVB416 > 0 Or hVB5 > 0 Or hVB6 > 0)
'Now that we know if an attempt was made to instantiate it in VB,
'check to see if certain other environments (such as Internet Explorer)
'are trying to instantiate it.
hIE = GetModuleHandle("IEXPLORE.EXE")
'While I can't imagine a Visual C++ programmer wanting to use
'a component compiled with VB, check for VC++ too. This is the
'file name used for VC++ 4.2, 5.0, and 6.0.
hVC = GetModuleHandle("MSDEV.EXE")
'Check Microsoft Development Environment, which is used
'for Visual InterDev, Visual Studio Installer, and Visual J++.
'This is also the development environment for all VS.NET products
(at least up to VS.NET 2003).
hMSDev = GetModuleHandle("DEVENV.exe")
'FrontPage and FrontPage Express are also capable of instantiating
'ActiveX components for use in web pages.
hFPE = GetModuleHandle("Fpxpress.exe")
hFP = 0 'Need to find out what FrontPage's filename is
IsIDE = IsIDE Or (hIE > 0 Or hVC > 0 Or hMSDev > 0 Or hFP > 0 Or hFPE >
0)
End Function
---------END CODE
I call this function is practically every DLL I write to prevent
unauthorized use of the DLL by others (there is another function,
IsDevelopmentUseAllowed, that is called if IsIDE returns True). I seldom use
in it OCXs though because VB supports requiring a license key for those and
for most OCXs I write, that's good enough.
--
Mike
Microsoft MVP Visual Basic