About DLL

  • Thread starter Thread starter Varadha
  • Start date Start date
V

Varadha

I am using a mutiple application sharing a dll. In my
application i am loading the dll using LoadLibrary Function.

My Question is:
1) I have loaded the dll in the 1st appication
2) Now if again if i try to load the same dll again in
another application whether i can know if the dll is loaded
already.
If so how should i know it.

-Thanks in Advance
Varadha
 
If you need to detect whether another instance already exists, create a
uniquely named mutex using the CreateMutex function. CreateMutex will
succeed even if the mutex already exists, but the function will return
ERROR_ALREADY_EXISTS. This indicates that another instance of your
application exists, because it created the mutex first.

if you do this in your DLL_PROCESS_ATTACH code you can do something like
g_firstinstance = true;

kind regards,
Bruno.
 
I am using a mutiple application sharing a dll. In my
application i am loading the dll using LoadLibrary Function.

My Question is:
1) I have loaded the dll in the 1st appication
2) Now if again if i try to load the same dll again in
another application whether i can know if the dll is loaded
already.
If so how should i know it.

It depends what you're really trying to achieve.

One way would be to have a shared global variable:

#pragma data_seg(".shared")
bool g_bFlag = false;
#pragma data_seg()

.... and set it true in DllMain.

Dave
 
I was thinking something similar except an int so you can actually track the
number of instances currently allocated.

Ray
 
I was thinking something similar except an int so you can actually track the
number of instances currently allocated.

Ray,

If you're looking to limit the number of instances, you could use a
semaphore something like this:

int main(int argc, char* argv[])
{
/* Create or open an existing semaphore of this
* name allowing a max count of 3
*/
HANDLE hSema = CreateSemaphore( NULL, 0, 3,
"UseAGUIDHereToBeUnique" );

if ( hSema != NULL )
{
/* Increment the count on the semaphore */
if ( ReleaseSemaphore( hSema, 1, NULL ) )
{
printf("Hello World!\n");
getchar();

/* Decrement the semaphore count */
WaitForSingleObject( hSema, 1000 );
}
else
{
printf("You've run out of users on this
machine.\n");
}
}
else
{
printf( "Failed to create or open the semaphore\n" );
}

return 0;
}

Dave
 
Back
Top