본문 바로가기
코드/c/c++

Guid를 이용한 Creatfile 사용..

by WeZZ 2008. 10. 7.


App부분..

 

 #include <windows.h>

#include <initguid.h>
#include <setupapi.h>

DEFINE_GUID(SampleGuid, 0x5665dec0, 0xa40a, 0x11d1, 0xb9, 0x84, 0x0, 0x20, 0xaf, 0xd7, 0x97, 0x78);

#define MAXDEVICENUMBER 10
int main(int argc, char* argv[])
{
          SP_INTERFACE_DEVICE_DATA interfaceData;
          PSP_INTERFACE_DEVICE_DETAIL_DATA pData;
          DWORD index=0;
          HANDLE handle[MAXDEVICENUMBER];
          char DeviceName[MAXDEVICENUMBER][256];
          HDEVINFO Info = SetupDiGetClassDevs( (struct _GUID *)&SampleGuid,
                                  0, 0,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE );

           if( Info == (HANDLE) -1 )
                return 0;
           interfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
 
           while( 1 )
          {
                DWORD size;
                BOOLEAN bl;
               bl = SetupDiEnumDeviceInterfaces( Info, 0, (struct _GUID *)&SampleGuid,
                                                                    index,&interfaceData );
               if( bl == FALSE )
                    break;

              SetupDiGetDeviceInterfaceDetail( Info, &interfaceData, 0, 0, &size, 0 );
              pData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc( size );
              pData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
              SetupDiGetDeviceInterfaceDetail( Info, &interfaceData, pData, size, 0, 0 );

               // CreateFile을 할 수 있는 이름이 구해지는 부분 
               memset( &DeviceName[index][0], 0, 256 );
               strcpy( &DeviceName[index][0], pData->DevicePath );
                free( pData );
                index++;
            }

            SetupDiDestroyDeviceInfoList( Info );

  int count;
 for( count = 0; count < (int)index ; count ++ )
 {
        handle[count] = CreateFile( &DeviceName[count][0], GENERIC_READ|GENERIC_WRITE
                 , 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
        CloseHandle( handle[count] );
 }

 return 0;
}

 

 

 



드라이버 부분

 DEFINE_GUID(GUID_SAMPLE, 0x5665dec0, 0xa40a, 0x11d1, 0xb9, 0x84, 0x0, 0x20, 0xaf, 0xd7, 0x97, 0x78);


 Addevice나 Entry시..
 IoRegisterDeviceInterface( phyDeviceObject, &GUID_SAMPLE, NULL, &dex->UnicodeString );
 
 Remove나 Unload시..
IoSetDeviceInterfaceState( &dex->UnicodeString, FALSE );
RtlFreeUnicodeString( &dex->UnicodeString );