/// <summary> /// Demonstrates how to read and write to/from a key's /// file at a certain file position /// </summary> public void ReadWritePosDemo(Hasp hasp, HaspFileId fileId) { // sanity check if ((null == hasp) || !hasp.IsLoggedIn()) { return; } Verbose("GetFileSize/FilePos Demo"); // firstly get a file object to a key's file. HaspFile file = hasp.GetFile(fileId); if (!file.IsLoggedIn()) { // Not logged into key - nothing left to do. Verbose("Failed to get file object\r\n"); return; } Verbose("Reading contents of file: " + file.FileId.ToString()); Verbose("Retrieving the size of the file"); // we want to write an int at the end of the file. // therefore we are going to // - get the file's size // - set the object's read and write position to // the appropriate offset. int size = 0; HaspStatus status = file.FileSize(ref size); ReportStatus(status); if (HaspStatus.StatusOk != status) { Verbose(""); return; } Verbose("Size of the file is: " + size.ToString() + " Bytes"); Verbose("Setting file position to last int and reading value"); // set the file pos to the end minus the size of int file.FilePos = size - HaspFile.TypeSize(typeof(int)); // now read what's there int aValue = 0; status = file.Read(ref aValue); ReportStatus(status); if (HaspStatus.StatusOk != status) { Verbose(""); return; } Verbose("Writing to file: 0x" + int.MaxValue.ToString("X2")); // write some data. status = file.Write(int.MaxValue); ReportStatus(status); if (HaspStatus.StatusOk != status) { Verbose(""); return; } // read back the written value. int newValue = 0; Verbose("Reading written data"); status = file.Read(ref newValue); ReportStatus(status); if (HaspStatus.StatusOk == status) { Verbose("Data read: 0x" + newValue.ToString("X2")); } // restore the original data. file.Write(aValue); Verbose(""); }
public void ReadDynamicMemory(Hasp hasp, int fileId) { // sanity check if ((null == hasp) || !hasp.IsLoggedIn()) { return; } HaspFile file = hasp.GetFile(fileId); if (!file.IsLoggedIn()) { return; } string fileIdhex = fileId.ToString("X"); int size = 0; HaspStatus status = file.FileSize(ref size); ReportStatus(status); if (HaspStatus.InvalidFile == status) { return; } if (HaspStatus.StatusOk != status) { return; } if (size != 0) // skip if no dynamic memory exist or is of size zero { if (size > dynamicMemoryBufferSize) { size = dynamicMemoryBufferSize; } // read the contents of the file into a buffer byte[] bytes = new byte[size]; status = file.Read(bytes, 0, bytes.Length); if (HaspStatus.StatusOk != status) { return; } DumpBytes(bytes); //Verbose("Writing data"); //// now let's write some data into the file //byte[] newBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7 }; //status = file.Write(newBytes, 0, newBytes.Length); //ReportStatus(status); //if (HaspStatus.StatusOk != status) //{ // Verbose(""); // return; //} //DumpBytes(newBytes); //// and read them again //Verbose("Reading written data"); //status = file.Read(newBytes, 0, newBytes.Length); //ReportStatus(status); //if (HaspStatus.StatusOk == status) // DumpBytes(newBytes); //// restore the original contents //file.Write(bytes, 0, bytes.Length); //Verbose(""); } }
/// <summary> /// Demonstrates how to perform read and write /// operations on a key's file /// </summary> public void ReadWriteDemo(Hasp hasp, HaspFileId fileId) { // sanity check if ((null == hasp) || !hasp.IsLoggedIn()) { Debug.Log("llllllllllllllllllllll"); return; } Verbose("Read/Write Demo"); // Get a file object to a key's memory file. // please note: the file object is tightly connected // to its key object. logging out from a key also // invalidates the file object. // doing the following will result in an invalid // file object: // hasp.login(...) // HaspFile file = hasp.GetFile(); // hasp.logout(); // Debug.Assert(file.IsValid()); will assert HaspFile file = hasp.GetFile(fileId); if (!file.IsLoggedIn()) { // Not logged into a key - nothing left to do. Verbose("Failed to get file object\r\n"); return; } Verbose("Reading contents of file: " + file.FileId.ToString()); Verbose("Retrieving the size of the file"); // get the file size int size = 0; HaspStatus status = file.FileSize(ref size); ReportStatus(status); if (HaspStatus.StatusOk != status) { Verbose(""); return; } Verbose("Size of the file is: " + size.ToString() + " Bytes"); // read the contents of the file into a buffer byte[] bytes = new byte[size]; Verbose("Reading data"); status = file.Read(bytes, 0, bytes.Length); ReportStatus(status); if (HaspStatus.StatusOk != status) { Verbose(""); return; } DumpBytes(bytes); Verbose("Writing to file"); // now let's write some data into the file byte[] newBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7 }; status = file.Write(newBytes, 0, newBytes.Length); ReportStatus(status); if (HaspStatus.StatusOk != status) { Verbose(""); return; } DumpBytes(newBytes); // and read them again Verbose("Reading written data"); status = file.Read(newBytes, 0, newBytes.Length); ReportStatus(status); if (HaspStatus.StatusOk == status) { DumpBytes(newBytes); } // restore the original contents file.Write(bytes, 0, bytes.Length); Verbose(""); }
/// <summary> /// Demonstrates how to access Dynamic memory /// available in Sentinel HL (Driverless configuration) /// key. Use the defined Dynamic memory's file id to access it. /// </summary> public void ReadWriteDynamicMemory(Hasp hasp, int fileId) { // sanity check if ((null == hasp) || !hasp.IsLoggedIn()) { return; } Verbose("Read/Write Dynamic Memory Demo"); // Get a file object to a key's memory file. // please note: the file object is tightly connected // to its key object. logging out from a key also // invalidates the file object. // doing the following will result in an invalid // file object: // hasp.login(...) // HaspFile file = hasp.GetFile(); // hasp.logout(); // Debug.Assert(file.IsValid()); will assert HaspFile file = hasp.GetFile(fileId); if (!file.IsLoggedIn()) { // Not logged into a key - nothing left to do. Verbose("Failed to get file object\r\n"); return; } string fileIdhex = fileId.ToString("X"); Verbose("Reading contents of dynamic memory fileid: 0x" + fileIdhex); Verbose("Retrieving the size "); // get the file size int size = 0; HaspStatus status = file.FileSize(ref size); ReportStatus(status); if (HaspStatus.InvalidFile == status) { // The specified dynamic memory fileid doesn't exists. Verbose("dynamic memory fileid 0x" + fileIdhex + "doesn't exists on the key"); Verbose(""); return; } if (HaspStatus.StatusOk != status) { Verbose(""); return; } Verbose("Size of the dynamic memory fileid is: " + size.ToString() + " Bytes"); if (size != 0) // skip if no dynamic memory exist or is of size zero { if (size > dynamicMemoryBufferSize) { size = dynamicMemoryBufferSize; } // read the contents of the file into a buffer byte[] bytes = new byte[size]; Verbose("Reading data"); status = file.Read(bytes, 0, bytes.Length); ReportStatus(status); if (HaspStatus.StatusOk != status) { Verbose(""); return; } DumpBytes(bytes); Verbose("Writing data"); // now let's write some data into the file byte[] newBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7 }; status = file.Write(newBytes, 0, newBytes.Length); ReportStatus(status); if (HaspStatus.StatusOk != status) { Verbose(""); return; } DumpBytes(newBytes); // and read them again Verbose("Reading written data"); status = file.Read(newBytes, 0, newBytes.Length); ReportStatus(status); if (HaspStatus.StatusOk == status) { DumpBytes(newBytes); } // restore the original contents file.Write(bytes, 0, bytes.Length); Verbose(""); } }