www.pudn.com > Ge_opc_Server_v1.rar > EXEMAIN.CPP, change:2005-06-23,size:5526b


// exemain.cpp 
// 
//  This source file contains the EXE Mainline code for 
//  this sample OPC server. 
// 
// 
// (c) COPYRIGHT 1996,1997 INTELLUTION INC. 
// ALL RIGHTS RESERVED 
// 
// Original Author: Al Chisholm 
// 
// Modification Log: 
//	Vers    Date   By    Notes 
//	----  -------- ---   ----- 
//	0.00  11/18/96 ACC 
// 0.90  04/08/97 ACC   add async logic 
//       09/11/97 ACC   move init/delete critical section to fix shutdown crash 
// 
 
#define INITGUID 
#define WIN32_LEAN_AND_MEAN 
#include "windows.h" 
#include "OLE2.h" 
 
#include "OPCLHEpipeview.h" 
#include "OPCfact.h" 
#include "OPCTHRD.h" 
 
#define TimerRes	1000		// This sample updates 1 per second max 
// Globals 
// 
DWORD			objectCount = 0;		// Number of servers created 
DWORD			lockCount = 0;			// Standard COM Server lock count 
HINSTANCE		serverInstance = 0;		// DLL instance (e.g. for LoadString) 
FILETIME		serverStartTime;		// OPC specific server start time 
IMalloc			*pIMalloc = 0;			// Common memory allocator 
CRITICAL_SECTION	CritSec;			// For compatability with DLL version 
 
HWND	serverWindow = NULL; 
UINT	TimerID; 
BOOL	Register( IUnknown *Fact, DWORD regCls); 
BOOL	Unregister( void); 
STDAPI	DllCanUnloadNow( void); 
 
 
// 
// The following code supports the EXE version 
//  of the OPC server 
// 
 
 
// OPCWndProc() 
//   This is the main windows procedure for the OPC Sample LocalServer. 
 
LRESULT CALLBACK OPCWndProc( HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam) 
{ 
  switch ( iMsg) { 
    case WM_DESTROY: 
      KillTimer(serverWindow, TimerID); 
      PostQuitMessage( 0); 
      break; 
	case WM_TIMER: 
		UpdateServers(TimerRes); 
		break; 
    default: 
      return DefWindowProc( hWnd, iMsg, wParam, lParam); 
  } 
  return 0; 
} 
 
 
 
// WinMain() 
//   This is the entry point for the EXE. 
 
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) 
{ 
  BOOL success = TRUE; 
  MSG  msg; 
 
  serverInstance = hInstance; 
  msg.wParam = 0; 
  InitializeCriticalSection(&CritSec); 
 
  if ( SUCCEEDED( CoInitialize( NULL))) 
  { 
//	  hr = CoInitializeSecurity(NULL, , , NULL); 
    IClassFactory* serverFactory = new OPCClassFactory( CLSID_OPCSampleServer); 
    if ( serverFactory != NULL && Register(serverFactory,REGCLS_MULTIPLEUSE)) 
	{ 
      if ( !hPrevInstance) 
	  { 
        WNDCLASS wc; 
        wc.style = CS_HREDRAW | CS_VREDRAW; 
        wc.lpfnWndProc = OPCWndProc; 
        wc.cbClsExtra = 0; 
        wc.cbWndExtra = 0; 
        wc.hInstance = serverInstance; 
        wc.hIcon = 0; 
        wc.hCursor = LoadCursor( NULL, IDC_ARROW); 
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
        wc.lpszMenuName = 0; 
        wc.lpszClassName = "LONGHUI OPC SERVER"; 
 
        success = (RegisterClass( &wc) != 0); 
      } 
 
      if ( success && ( serverWindow = CreateWindow( "LONGHUI OPC SERVER", "OPC Server For LongHui Epipeview", WS_OVERLAPPEDWINDOW, 
                                                     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
                                                     NULL, NULL, serverInstance, NULL)) != NULL) 
	  { 
        CoGetMalloc(MEMCTX_TASK, &pIMalloc);	// 'TASK' ok for DLL use 
        ShowWindow( serverWindow, SW_MINIMIZE); 
        UpdateWindow( serverWindow); 
 
        CoFileTimeNow( &serverStartTime); 
 
		// Start a psuedo-thread for Async Handling 
		// 
		InitServerSlots(); 
		TimerID = SetTimer(serverWindow, 0, TimerRes, 0);	// timer could be faster... 
 
        while ( GetMessage( &msg, NULL, 0, 0)) 
		{ 
          TranslateMessage( &msg); 
          DispatchMessage( &msg); 
        } 
 
        pIMalloc->Release(); 
      } 
      Unregister(); 
    } 
    else 
	{ 
      // Delete the factory if we did not successfully register it.  If it was registered, the factory 
      // will be deleted by the Unregister() function. 
      if ( serverFactory != NULL) 
        delete serverFactory; 
    } 
    CoUninitialize(); 
    DeleteCriticalSection(&CritSec); 
  } 
 
  return msg.wParam; 
} 
 
 
 
 
// OPCServerUnload() 
//   This function is called when an object inside the server is destroyed.  If the server 
//   can legally unload, this function posts a WM_QUIT message to the main window. 
 
void OPCServerUnload( void) 
{ 
  if ( serverWindow != NULL && DllCanUnloadNow() == S_OK) 
    PostMessage( serverWindow, WM_CLOSE, 0, 0); 
} 
 
 
 
 
DWORD mRegisterID; 
 
// Register() 
//   This function is called by the EXE version of the server to register this class factory 
//   with the OLE libraries.  It returns TRUE if it is successful. 
 
BOOL Register( IUnknown *Fact, DWORD regCls) 
{ 
  return SUCCEEDED( CoRegisterClassObject( CLSID_OPCSampleServer, 
				Fact, CLSCTX_LOCAL_SERVER, regCls, &mRegisterID)); 
} 
 
 
 
// Unregister() 
//   Unregisters this class factory with the OLE libraries. 
 
BOOL Unregister( void) 
{ 
  BOOL success = SUCCEEDED( CoRevokeClassObject( mRegisterID)); 
  mRegisterID = 0; 
  return success; 
} 
 
/////////////////////////////////////// 
// DllCanUnloadNow() 
//  This is the standard function that is called 
//  to determine whether or not this DLL is 
//  in use. 
// 
/////////////////////////////////////// 
STDAPI DllCanUnloadNow( void) 
{ 
	SCODE sc; 
	BOOL canUnload; 
 
	// If we have no objects and we are not locked 
	// then we can be unloaded (e.g. by VB). 
	// 
	canUnload = ( objectCount == 0 && lockCount == 0); 
	sc = canUnload ? S_OK : S_FALSE; 
	return sc; 
}