Click here to Skip to main content
15,353,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am in need of a little help, i'm trying to exit sub, if a test.dll is detected (test.dll is just example), and if the dll does not exist, just continue to do the other things in the code. I'm trying to do it like this, however even when the dll exists, the sub doesn't exit.Can you tell me where i am wrong. Any advice?I am leaving an example below.

Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long

Sub main()
If GetModuleHandle("test.dll") = 1 Then
Exit Sub
Do other things
End if
End sub

What I have tried:

I tried also to use function with true or false, and it happened the same.
Updated 18-Feb-22 5:44am

The GetModuleHandle function (libloaderapi.h) - Win32 apps | Microsoft Docs[^] returns either NULL (0) if the dll cannot be found, or a Windows HANDLE if it is found. So comparing it with the value 1 is unlikely to work. You should compare it not equal to zero.
You can use the following, should work fine -

Sub main()
If Not GetModuleHandle("test.dll") = Nothing Then
Exit Sub
''Do other things
End if
End sub
Member 14359403 18-Feb-22 12:27pm
Thank you for the answer, Andre, but it won't work on this way.I think, i found a way to make it work, but there is another issue.

I use, this method:

If Dir("path to my .dll") <> "" Then
Exit sub
Do other things...
End If
End sub
The problem here is, if this .dll is detected, the code after "Exit sub" will not be read.
What do I mean.
Detecting .dll -> exit sub -> and the rest part of the code, after end sub will not be read.(for example do other things)
Becase all stop after "Exit sub". My idea is, if this .dll is detected exit sub, and then continue to read the another part of the code. Any suggestions?
Andre Oosthuizen 18-Feb-22 13:26pm
I will then suggest that you use a select statement, if you use exit sub it will do exactly that, it will exit the sub and any other code within. Using a select statement will then check if dll exist, if so cool. You can then have other code after the select in the same sub which will then be executed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900