Found handle of a DLL open with another application [Solved]

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Found handle of a DLL open with another application [Solved]

Post by Kwai chang caine »

Hello at all

I have create a DLL with PB, i use it with PB and VB6.
But i have several problem to unload the DLL with VB.

Is it possible to found the handle of the DLL load in memory by VB with PB.
Like this i can check if my DLL is fine unloaded :D

Thanks for your help
Last edited by Kwai chang caine on Mon Nov 10, 2008 12:44 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Post by endo »

I guess your dll is not an activex dll right?
If so you may try LoadLibrary and FreeLibrary Windows APIs in your VB code. It should work, but I didn't try.
-= endo (registered user of purebasic since 98) =-
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Yes, but if she already open.......
Is what windows open a new handle, or he take the first handle

That is the question ??? :wink:
ImageThe happiness is a road...
Not a destination
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Post by endo »

I coudn't understand what you mean exactly (it may because of my bad english)
could you post a code example (vb/pb)?
you can put a function to your dll and your vb program can send his handle to the dll function, so you can check is it what you want or completly different handle.
-= endo (registered user of purebasic since 98) =-
jeslar360
User
User
Posts: 20
Joined: Thu Aug 28, 2008 6:24 am

Post by jeslar360 »

endo: From what I read, it sounds like what he is trying to do is see if a library has ALREADY been loaded by another program, and retrieve the Handle ID if it has. I guess he wants to share the access with the other program, rather than load it again.

I do not have much experience with libraries, so I can't really answer his question, or provide any advice.
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Post by endo »

jeslar360 wrote:endo: From what I read, it sounds like what he is trying to do is see if a library has ALREADY been loaded by another program, and retrieve the Handle ID if it has. I guess he wants to share the access with the other program, rather than load it again.

I do not have much experience with libraries, so I can't really answer his question, or provide any advice.
I see. But LoadLibrary will do this already, if a library is loaded into memory and another program opens that library then it gives the same handle to it (and increments the reference count)
But with this you can not share data between two different applications.
I confused because he says "several unload problems". Normally FreeLibrary decrements the reference count and if it 0 then it unloads the dll from memory. You don't have to care about it.

@Kwai: You may try a 3rd party tool to see the dlls & handles used by processes. I guess ProcessXp from systeminternals can do this.
regards.
-= endo (registered user of purebasic since 98) =-
jeslar360
User
User
Posts: 20
Joined: Thu Aug 28, 2008 6:24 am

Post by jeslar360 »

I see. But LoadLibrary will do this already, if a library is loaded into memory and another program opens that library then it gives the same handle to it (and increments the reference count)
But with this you can not share data between two different applications.
I confused because he says "several unload problems". Normally FreeLibrary decrements the reference count and if it 0 then it unloads the dll from memory. You don't have to care about it.
I figured as much. Reading further, it also looks like he wants to be able to tell when the other program has released the DLL.

I am not 100% sure if he wishes to do this by merely keeping track of the reference count (to see how many programs are using it)...there is most likely an API call for this...

It is ALSO possible that he may wish to get the file names (or process IDs) of programs that have already loaded it...so that he can tell when a specific program has released it.

But then, as I said...that is just my guess. I am trying to read between the grammar :)
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Post by endo »

so we should wait for Kwaï chang caïne's post about what he really trying to do :)
-= endo (registered user of purebasic since 98) =-
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Thanks for you two
jeslar360 as right, it's exactly what i want to do.

Sometimes my VB application open a DLL and not close correctly for X reason (Stop the program in the middle, Scratch the program, etc...).
In the VB forum, the member say to me, it's impossible to know if the DLL is really close :shock:

There are an API, i'm not remember the name, but it's perhaps "GetModule...." to test, if she has always in the memory.
But the means it's shared between the programer, someone say to me that this API not give the real and good result.
So if i loop a closelibrary with the return of this API, it's very dangerous because it's possible to disturb the memory and the array of the handle.

This is the reason why, i ask this question, if PB can control the handle of an API, open with another application, because VB can't do this :cry:
ImageThe happiness is a road...
Not a destination
endo
Enthusiast
Enthusiast
Posts: 141
Joined: Fri Apr 30, 2004 10:44 pm
Location: Turkiye (istanbul)
Contact:

Post by endo »

:roll: you can not close a library opened by another process. And you can not be sure the program which opened the library is crashed, so it is not a good idea to put some code into dll to release resources used by itself (before the app close the library)
So it is better to try on VB app to release the dll handle correctly in any situation.
I do not know if there is more advanced ways to do it, but I don't thing so.
regards.
-= endo (registered user of purebasic since 98) =-
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Ok thanks ENDO, for your explain 8)
ImageThe happiness is a road...
Not a destination
Post Reply