[comp.windows.ms] registering a call in a DLL

lab@sdgsun.uucp (Larry Baird) (07/14/90)

There are a couple of tricks you need to apply.	
Register your new class from you DLL's init function.
Have every application that uses your new class call a registeration and dereg 
function. In the reg function keep count
of how many peple are using your class.  In the dereg
if this is last user you need to help windows
and do a little clean up.  Here's the code i use for
reg and dereg.  NOTE: Each should only be called once per applicatio.
Hope this helps.




static WORD wRefCount = 0;			// Number of users

void FAR PASCAL pgStartUp( void )
	{
	LockData( 0 );
	
	wRefCount++;
	
	UnlockData( 0 );
	}

void PASCAL FAR pgCleanUp( HANDLE hInstance )
	{
	LockData( 0 );
	
	if ( --wRefCount == 0 )
		{
		HWND		hWnd;
		HANDLE		hModule;
		
		hWnd = CreateWindow ( 
					PAGE_CLASS,							// window class name
                    NULL,  		  	 					// window caption
					0L,									// window style
                    0,     								// initial x position
                    0,   								// initial y position
                    0,									// initial x size
                    0,									// initial y size
                    NULL,                   			// parent window handle
                    NULL,                    			// window menu handle
                    hInstance,               			// program handle
                    NULL );			           			// create parameters
		UTIL_WIN_ASSERT( hWnd != NULL );
			
		hModule = GetModuleHandle( MAKEINTRESOURCE( hInstance ) );
		UTIL_WIN_ASSERT( hModule != NULL );
			
		SetClassWord( hWnd, GCW_HMODULE, hModule );
		DestroyWindow( hWnd );
		}
		
	UnlockData( 0 );
	}
-- 
Larry A. Baird 				SAIC ComSystems, SDG division
Manager, Software Development           450 N. Lakemont Avenue
UUCP:ucf-cs!sdgsun!lab                  Winter Park, FL 32792
CIS: 72355,171                          (407) 657-1300

davidds@microsoft.UUCP (David D'SOUZA) (07/18/90)

Sorry... But Larry's method below is a bad thing to do for Windows 3.0.
It kind of/sort of works in 2.x but doesn't work in 3.0. There really
was no kosher way to register classes in a dll in 2.x but there were
hacks that ended up working. Larry's method will work in Windows 2 but
is not recommended for 3.0.


Here's one way to do it in Windows 3.0: In your lib init, register your
class using the hModule of the library as the hinstance.  Also make
sure you have the CS_GLOBALCLASS style bit set. 

Then, in your applications that use this class, use LoadLibrary(<lib
name>)" to load the dll.  This will cause your class to be registered
if the library isn't already loaded.  When your app is done, destroy
all your windows that use this class and, use FreeLibrary to get rid of
the dll.  (Your applications call createwindow using the class name and
the app's hInstance NOT the hModule of the dll.)

Windows keeps a reference count on the library when you make Load/Free
calls and doesn't really free the library until everyone who is using
it has freed it.  Also, the class is automatically unregistered when
the library is finally freeded.

An alternate to the LoadLibrary is you can export a function from the
dll, say RegisterMyClasses.  This dummy function does nothing but you
can call this from all your apps that depend on this dll.  This will 
cause your dll to automatically be loaded/freed when your app
starts/terminates.  Since your classes are registered in the LibInit,
they will only be registered once.  The CS_GLOBALSTYLE makes the
classes available to all apps.

Hope this helps

Dave.



In article <1990Jul13.231427.4144@sdgsun.uucp> lab@sdgsun.uucp (Larry Baird) writes:
>There are a couple of tricks you need to apply.	
>Register your new class from you DLL's init function.
>Have every application that uses your new class call a registeration and dereg 
>function. In the reg function keep count
>of how many peple are using your class.  In the dereg
>if this is last user you need to help windows
>and do a little clean up.  Here's the code i use for
>reg and dereg.  NOTE: Each should only be called once per applicatio.
>Hope this helps.

CODE SAMPLE DELETED

>
>
>
>-- 
>Larry A. Baird 				SAIC ComSystems, SDG division
>Manager, Software Development           450 N. Lakemont Avenue
>UUCP:ucf-cs!sdgsun!lab                  Winter Park, FL 32792
>CIS: 72355,171                          (407) 657-1300