www.pudn.com > pcmcia_sample.rar > Test_pcmcia.cpp
// Test_pcmcia.cpp // // Generated by DriverWizard version DriverStudio 2.7.0 (Build 562) // // This console application demonstrates how to open a handle // to a device in your driver, and communicate with the driver // using Read, Write, and DeviceIoControl calls, as appropriate. // // This test program attempts to open the device using the // GUID defined in "..\PcmciaDeviceinterface.h" #include#include #include #include "..\PcmciaDeviceinterface.h" // Has class GUID definition // This function is found in module OpenByIntf.cpp HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError); typedef void VOIDFUNC(); // Prototypes void Usage(void); void CloseIfOpen(void); void doRead(int i); void doWrite(int i); // Global data // Handle to device opened in driver. // HANDLE hDevice = INVALID_HANDLE_VALUE; // Class GUID used to open device // GUID ClassGuid = PcmciaDevice_CLASS_GUID; //////////////////////////////////////////////////////////////////////// // Exit // // Print a message and exit // void Exit(int res) { printf("Exiting...\n\n"); CloseIfOpen(); exit(res); } //////////////////////////////////////////////////////////////////////// // Main entry point // // int __cdecl main(int argc, char *argv[]) { int nArgIndex; // Walk through command line arguments int nArgIncrement = 0; int val; DWORD Error; printf("Test application Test_pcmcia starting...\n"); hDevice = OpenByInterface( &ClassGuid, 0, &Error); if (hDevice == INVALID_HANDLE_VALUE) { printf("ERROR opening device: (%0x) returned from CreateFile\n", GetLastError()); Exit(1); } else { printf("Device found, handle open.\n"); } // Parse the command line if (argc < 2) Usage(); nArgIndex = 1; while (nArgIndex < argc) { // Parse ahead to determine numeric value of argument if (nArgIndex+1 >= argc) Usage(); if (!isdigit(argv[nArgIndex+1][0])) Usage(); val = atoi(argv[nArgIndex+1]); switch (argv[nArgIndex][0]) { case 'r': case 'R': doRead(val); nArgIncrement = 2; break; case 'w': case 'W': doWrite(val); nArgIncrement = 2; break; case 'i': case 'I': printf("No IO Control Codes defined\n"); Exit(1); case '?': case 'h': default: Usage(); } nArgIndex += nArgIncrement; } return 0; } //////////////////////////////////////////////////////////////////////// // CloseIfOpen // // Close the device if we previously opened a handle to it. // void CloseIfOpen(void) { if (hDevice != INVALID_HANDLE_VALUE) { // Close the handle to the driver if (!CloseHandle(hDevice)) { printf("ERROR: CloseHandle returns %0x.\n", GetLastError()); } hDevice = INVALID_HANDLE_VALUE; } } //////////////////////////////////////////////////////////////////////// // doRead // // Read 'n' bytes of data from the device // // Note: This simple test app reads data from the device and displays the // data as characters. This behavior can be modified as appropriate // for your device. // void doRead(int n) { char *buf; ULONG nRead; int i; int j; buf = (char *) malloc(n); if (buf == NULL) { printf("Failed to allocate buffer for read"); Exit(1); } // Read data from driver printf("Reading from device - "); ReadFile(hDevice, buf, n, &nRead, NULL); printf("%d bytes read from device (%d requested).\n", nRead, n); // Print what was read i = 0; while(i < n) { j = min((i+26),n); for(; i < j; i++) { printf("%c, ", buf[i]); } printf("\n"); } free(buf); } //////////////////////////////////////////////////////////////////////// // doWrite // // Write 'n' bytes of data to the device // // Note: This simple test app writes sequential characters to the // device. This behavior can be modified as appropriate // for your device. // void doWrite(int n) { char *buf; ULONG nWritten; int i; int j; buf = (char *) malloc(n); if (buf == NULL) { printf("Failed to allocate buffer for write"); Exit(1); } // start with the mod26 letter of the number of bytes to write j = (n % 26); // load buffer with dummy data (abcdefg...) for (i=0; i