src/SeeDll/Packet32.c
/*! 
  \brief Opens an adapter using the NPF device driver.
  \param AdapterName A string containing the name of the device to open. 
  \return If the function succeeds, the return value is the pointer to a properly initialized ADAPTER object,
   otherwise the return value is NULL.

  \note internal function used by PacketOpenAdapter() and AddAdapter()
*/
\brief Opens an adapter using the NPF device driver.
\param AdapterName A string containing the name of the device to open.
\return If the function succeeds, the return value is the pointer to a properly initialized ADAPTER object,
otherwise the return value is NULL.

\note internal function used by PacketOpenAdapter() and AddAdapter()
\ brief Opens an adapter using the NPF device driver . \ param AdapterName A string containing the name of the device to open . \ return If the function succeeds the return value is the pointer to a properly initialized ADAPTER object otherwise the return value is NULL . \ note internal function used by PacketOpenAdapter () and AddAdapter ()
LPADAPTER PacketOpenAdapterNPF(PCHAR AdapterName)
{
    LPADAPTER lpAdapter;
    BOOLEAN Result;
	DWORD error;
	SC_HANDLE svcHandle = NULL;
	SC_HANDLE scmHandle = NULL;
	LONG KeyRes;
	HKEY PathKey;
	SERVICE_STATUS SStat;
	BOOLEAN QuerySStat;
	WCHAR SymbolicLink[128];
	ODS("PacketOpenAdapterNPF\n");
	scmHandle = OpenSCManager(NULL, NULL, GENERIC_READ);
	if(scmHandle == NULL)
	{
		error = GetLastError();
		ODSEx("OpenSCManager failed! LastError=%d\n", error);
	}
	else
	{
		KeyRes=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
			NPFRegistryLocation,
			0,
			KEY_READ,
			&PathKey);
		if(KeyRes != ERROR_SUCCESS)
		{
			Result = PacketInstallDriver();
		}
		else
		{
			Result = TRUE;
			RegCloseKey(PathKey);
		}
		Result = FALSE;
		svcHandle = OpenService(scmHandle, NPFServiceName, GENERIC_READ);
		if (svcHandle != NULL)
		{
			Result = TRUE;
			CloseServiceHandle(svcHandle);
		}
		if (Result) 
		{
			svcHandle = OpenService(scmHandle, NPFServiceName, SERVICE_START | SERVICE_QUERY_STATUS );
			if (svcHandle != NULL)
			{
				QuerySStat = QueryServiceStatus(svcHandle, &SStat);
#if defined(_DBG) || defined(_DEBUG_TO_FILE)				
				switch (SStat.dwCurrentState)
				{
				case SERVICE_CONTINUE_PENDING:
					ODS("The status of the driver is: SERVICE_CONTINUE_PENDING\n");
					break;
				case SERVICE_PAUSE_PENDING:
					ODS("The status of the driver is: SERVICE_PAUSE_PENDING\n");
					break;
				case SERVICE_PAUSED:
					ODS("The status of the driver is: SERVICE_PAUSED\n");
					break;
				case SERVICE_RUNNING:
					ODS("The status of the driver is: SERVICE_RUNNING\n");
					break;
				case SERVICE_START_PENDING:
					ODS("The status of the driver is: SERVICE_START_PENDING\n");
					break;
				case SERVICE_STOP_PENDING:
					ODS("The status of the driver is: SERVICE_STOP_PENDING\n");
					break;
				case SERVICE_STOPPED:
					ODS("The status of the driver is: SERVICE_STOPPED\n");
					break;
				default:
					ODS("The status of the driver is: unknown\n");
					break;
				}
#endif
				if(!QuerySStat || SStat.dwCurrentState != SERVICE_RUNNING)
				{
					ODS("Calling startservice\n");
					if (StartService(svcHandle, 0, NULL)==0)
					{ 
						error = GetLastError();
						if(error!=ERROR_SERVICE_ALREADY_RUNNING && error!=ERROR_ALREADY_EXISTS)
						{
							SetLastError(error);
							if (scmHandle != NULL) 
								CloseServiceHandle(scmHandle);
							error = GetLastError();
							ODSEx("PacketOpenAdapterNPF: StartService failed, LastError=%d\n",error);
							SetLastError(error);
							return NULL;
						}
					}				
				}
				CloseServiceHandle( svcHandle );
       			svcHandle = NULL;
			}
			else
			{
				error = GetLastError();
				ODSEx("OpenService failed! Error=%d", error);
				SetLastError(error);
			}
		}
		else
		{
			if (KeyRes == FALSE)
				Result = PacketInstallDriver();
			else
				Result = TRUE;
			if (Result) {
				svcHandle = OpenService(scmHandle,NPFServiceName,SERVICE_START);
				if (svcHandle != NULL)
				{
					QuerySStat = QueryServiceStatus(svcHandle, &SStat);
#if defined(_DBG) || defined(_DEBUG_TO_FILE)				
					switch (SStat.dwCurrentState)
					{
					case SERVICE_CONTINUE_PENDING:
						ODS("The status of the driver is: SERVICE_CONTINUE_PENDING\n");
						break;
					case SERVICE_PAUSE_PENDING:
						ODS("The status of the driver is: SERVICE_PAUSE_PENDING\n");
						break;
					case SERVICE_PAUSED:
						ODS("The status of the driver is: SERVICE_PAUSED\n");
						break;
					case SERVICE_RUNNING:
						ODS("The status of the driver is: SERVICE_RUNNING\n");
						break;
					case SERVICE_START_PENDING:
						ODS("The status of the driver is: SERVICE_START_PENDING\n");
						break;
					case SERVICE_STOP_PENDING:
						ODS("The status of the driver is: SERVICE_STOP_PENDING\n");
						break;
					case SERVICE_STOPPED:
						ODS("The status of the driver is: SERVICE_STOPPED\n");
						break;
					default:
						ODS("The status of the driver is: unknown\n");
						break;
					}
#endif
					if(!QuerySStat || SStat.dwCurrentState != SERVICE_RUNNING){
						ODS("Calling startservice\n");
						if (StartService(svcHandle, 0, NULL)==0){ 
							error = GetLastError();
							if(error!=ERROR_SERVICE_ALREADY_RUNNING && error!=ERROR_ALREADY_EXISTS){
								if (scmHandle != NULL) CloseServiceHandle(scmHandle);
								ODSEx("PacketOpenAdapterNPF: StartService failed, LastError=%d\n",error);
								SetLastError(error);
								return NULL;
							}
						}
					}
					CloseServiceHandle( svcHandle );
					svcHandle = NULL;
				}
				else{
					error = GetLastError();
					ODSEx("OpenService failed! LastError=%d", error);
					SetLastError(error);
				}
			}
		}
	}
    if (scmHandle != NULL) CloseServiceHandle(scmHandle);
	lpAdapter=(LPADAPTER)GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof(ADAPTER));
	if (lpAdapter==NULL)
	{
		ODS("PacketOpenAdapterNPF: GlobalAlloc Failed\n");
		error=GetLastError();
		SetLastError(error);
	    ODS("PacketOpenAdapterNPF: Failed to allocate the adapter structure\n");
		return NULL;
	}
	lpAdapter->NumWrites=1;
 	if (LOWORD(GetVersion()) == 4)
 		wsprintf(SymbolicLink,TEXT("\\\\.\\%s"),&AdapterName[16]);
 	else
 		wsprintf(SymbolicLink,TEXT("\\\\.\\Global\\%s"),&AdapterName[16]);
	memcpy(lpAdapter->SymbolicLink, (PCHAR)SymbolicLink, MAX_LINK_NAME_LENGTH);
	lpAdapter->hFile=CreateFile(SymbolicLink,GENERIC_WRITE | GENERIC_READ,
		0,NULL,OPEN_EXISTING,0,0);
	if (lpAdapter->hFile != INVALID_HANDLE_VALUE) 
	{
		if(PacketSetReadEvt(lpAdapter)==FALSE){
			error=GetLastError();
			ODS("PacketOpenAdapterNPF: Unable to open the read event\n");
			GlobalFreePtr(lpAdapter);
			SetLastError(error);
		    ODSEx("PacketOpenAdapterNPF: PacketSetReadEvt failed, LastError=%d\n",error);
			return NULL;
		}		
		PacketSetMaxLookaheadsize(lpAdapter);
		_snprintf(lpAdapter->Name, ADAPTER_NAME_LENGTH, "%S", AdapterName);
		return lpAdapter;
	}
	error=GetLastError();
	GlobalFreePtr(lpAdapter);
    ODSEx("PacketOpenAdapterNPF: CreateFile failed, LastError= %d\n",error);
	SetLastError(error);
	return NULL;
}
LPADAPTER PacketOpenAdapterNPF ( PCHAR AdapterName ) { LPADAPTER lpAdapter ; BOOLEAN Result ; DWORD error ; SC_HANDLE svcHandle = NULL ; SC_HANDLE scmHandle = NULL ; LONG KeyRes ; HKEY PathKey ; SERVICE_STATUS SStat ; BOOLEAN QuerySStat ; WCHAR SymbolicLink [ 128 ] ; ODS ( " \n " ) ; scmHandle = OpenSCManager ( NULL , NULL , GENERIC_READ ) ; if ( scmHandle == NULL ) { error = GetLastError ( ) ; ODSEx ( " \n " , error ) ; } else { KeyRes = RegOpenKeyEx ( HKEY_LOCAL_MACHINE , NPFRegistryLocation , 0 , KEY_READ , & PathKey ) ; if ( KeyRes != ERROR_SUCCESS ) { Result = PacketInstallDriver ( ) ; } else { Result = TRUE ; RegCloseKey ( PathKey ) ; } Result = FALSE ; svcHandle = OpenService ( scmHandle , NPFServiceName , GENERIC_READ ) ; if ( svcHandle != NULL ) { Result = TRUE ; CloseServiceHandle ( svcHandle ) ; } if ( Result ) { svcHandle = OpenService ( scmHandle , NPFServiceName , SERVICE_START | SERVICE_QUERY_STATUS ) ; if ( svcHandle != NULL ) { QuerySStat = QueryServiceStatus ( svcHandle , & SStat ) ; #if defined ( _DBG ) || defined ( _DEBUG_TO_FILE ) switch ( SStat . dwCurrentState ) { case SERVICE_CONTINUE_PENDING : ODS ( " \n " ) ; break ; case SERVICE_PAUSE_PENDING : ODS ( " \n " ) ; break ; case SERVICE_PAUSED : ODS ( " \n " ) ; break ; case SERVICE_RUNNING : ODS ( " \n " ) ; break ; case SERVICE_START_PENDING : ODS ( " \n " ) ; break ; case SERVICE_STOP_PENDING : ODS ( " \n " ) ; break ; case SERVICE_STOPPED : ODS ( " \n " ) ; break ; default : ODS ( " \n " ) ; break ; } #endif if ( ! QuerySStat || SStat . dwCurrentState != SERVICE_RUNNING ) { ODS ( " \n " ) ; if ( StartService ( svcHandle , 0 , NULL ) == 0 ) { error = GetLastError ( ) ; if ( error != ERROR_SERVICE_ALREADY_RUNNING && error != ERROR_ALREADY_EXISTS ) { SetLastError ( error ) ; if ( scmHandle != NULL ) CloseServiceHandle ( scmHandle ) ; error = GetLastError ( ) ; ODSEx ( " \n " , error ) ; SetLastError ( error ) ; return NULL ; } } } CloseServiceHandle ( svcHandle ) ; svcHandle = NULL ; } else { error = GetLastError ( ) ; ODSEx ( " " , error ) ; SetLastError ( error ) ; } } else { if ( KeyRes == FALSE ) Result = PacketInstallDriver ( ) ; else Result = TRUE ; if ( Result ) { svcHandle = OpenService ( scmHandle , NPFServiceName , SERVICE_START ) ; if ( svcHandle != NULL ) { QuerySStat = QueryServiceStatus ( svcHandle , & SStat ) ; #if defined ( _DBG ) || defined ( _DEBUG_TO_FILE ) switch ( SStat . dwCurrentState ) { case SERVICE_CONTINUE_PENDING : ODS ( " \n " ) ; break ; case SERVICE_PAUSE_PENDING : ODS ( " \n " ) ; break ; case SERVICE_PAUSED : ODS ( " \n " ) ; break ; case SERVICE_RUNNING : ODS ( " \n " ) ; break ; case SERVICE_START_PENDING : ODS ( " \n " ) ; break ; case SERVICE_STOP_PENDING : ODS ( " \n " ) ; break ; case SERVICE_STOPPED : ODS ( " \n " ) ; break ; default : ODS ( " \n " ) ; break ; } #endif if ( ! QuerySStat || SStat . dwCurrentState != SERVICE_RUNNING ) { ODS ( " \n " ) ; if ( StartService ( svcHandle , 0 , NULL ) == 0 ) { error = GetLastError ( ) ; if ( error != ERROR_SERVICE_ALREADY_RUNNING && error != ERROR_ALREADY_EXISTS ) { if ( scmHandle != NULL ) CloseServiceHandle ( scmHandle ) ; ODSEx ( " \n " , error ) ; SetLastError ( error ) ; return NULL ; } } } CloseServiceHandle ( svcHandle ) ; svcHandle = NULL ; } else { error = GetLastError ( ) ; ODSEx ( " " , error ) ; SetLastError ( error ) ; } } } } if ( scmHandle != NULL ) CloseServiceHandle ( scmHandle ) ; lpAdapter = ( LPADAPTER ) GlobalAllocPtr ( GMEM_MOVEABLE | GMEM_ZEROINIT , sizeof ( ADAPTER ) ) ; if ( lpAdapter == NULL ) { ODS ( " \n " ) ; error = GetLastError ( ) ; SetLastError ( error ) ; ODS ( " \n " ) ; return NULL ; } lpAdapter -> NumWrites = 1 ; if ( LOWORD ( GetVersion ( ) ) == 4 ) wsprintf ( SymbolicLink , TEXT ( " \\ \\ \\ " ) , & AdapterName [ 16 ] ) ; else wsprintf ( SymbolicLink , TEXT ( " \\ \\ \\ \\ " ) , & AdapterName [ 16 ] ) ; memcpy ( lpAdapter -> SymbolicLink , ( PCHAR ) SymbolicLink , MAX_LINK_NAME_LENGTH ) ; lpAdapter -> hFile = CreateFile ( SymbolicLink , GENERIC_WRITE | GENERIC_READ , 0 , NULL , OPEN_EXISTING , 0 , 0 ) ; if ( lpAdapter -> hFile != INVALID_HANDLE_VALUE ) { if ( PacketSetReadEvt ( lpAdapter ) == FALSE ) { error = GetLastError ( ) ; ODS ( " \n " ) ; GlobalFreePtr ( lpAdapter ) ; SetLastError ( error ) ; ODSEx ( " \n " , error ) ; return NULL ; } PacketSetMaxLookaheadsize ( lpAdapter ) ; _snprintf ( lpAdapter -> Name , ADAPTER_NAME_LENGTH , " " , AdapterName ) ; return lpAdapter ; } error = GetLastError ( ) ; GlobalFreePtr ( lpAdapter ) ; ODSEx ( " \n " , error ) ; SetLastError ( error ) ; return NULL ; }
\brief Opens an adapter using the NPF device driver.
\ brief Opens an adapter using the NPF device driver .
// check if the NPF registry key is already present
 // this means that the driver is already installed and that we don't need to call PacketInstallDriver
 //set the error to the one on which we failed
 // Copy  only the bytes that fit in the adapter structure.
 // Note that lpAdapter->SymbolicLink is present for backward compatibility but will
 // never be used by the apps
 //try if it is possible to open the adapter immediately
 //set the error to the one on which we failed
 //set the error to the one on which we failed
