PDA

View Full Version : خواندن و نوشتن سکتورها در میکرو SD متصل با RAM Reader



amir_mhdi
دوشنبه 04 آبان 1394, 14:02 عصر
با سلام. من میخوام تو یک میکرو SD که با رم ریدر به کامپیوتر وصل هست سکتوری بخونم و بنویسم. ویندوزم هم سون هست.
تو چند تا سایت خارجی یکسری کد بود که موقع اجرا خطا میده . درست موقعی که میخواد با CreateFile کار کنه میگه handel مورد داره. چندین نوع مختلف رو چک کردم ولی هیچکدم جواب نداد. این کار رو قبلا تو ویندوز XP چندین سال پیش انجام داده بودم ولی با همون توابع اینجا جواب نمیده.

amir_mhdi
سه شنبه 05 آبان 1394, 08:53 صبح
اینا توابعی هست که جواب نداد. البته چندین نوع دیگه هم بود که بی نتیجه بودن.



using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Security;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Program_uSD
{
class uSDFunctions
{
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;

private const uint FILE_SHARE_READ = 0x00000001;
private const uint FILE_SHARE_WRITE = 0x00000002;

private const uint OPEN_EXISTING = 3;

[DllImport("kernel32.dll", SetLastError = true)]
private static extern SafeFileHandle CreateFileA(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);


private System.IO.FileStream _DriverStream;
private long _SectorLength = 0;
private SafeFileHandle _DriverHandle;

/// <summary>
/// The number of sectors
/// </summary>
public long SectorLength { get { return _SectorLength; } }


/// <summary>
/// Access to the disk sector information
/// </summary>
/// <param name="DriverName">G:</param>
public bool uSD_Init(string DriverName)
{

if (DriverName == null && DriverName.Trim().Length == 0) return false;
_DriverHandle = CreateFileA("\\\\.\\" + DriverName.Trim(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

_DriverStream = new System.IO.FileStream(_DriverHandle, System.IO.FileAccess.ReadWrite);

GetSectorCount();
return true;
}
/// <summary>
/// Gets the number of sectors
/// </summary>
private void GetSectorCount()
{
if (_DriverStream == null) return;
_DriverStream.Position = 0;

byte[] ReturnByte = new byte[512];
_DriverStream.Read(ReturnByte, 0, 512); //Gets the first sector

if (ReturnByte[0] == 0xEB && ReturnByte[1] == 0x58) //DOS seemed to be 32
{
_SectorLength = (long)BitConverter.ToInt32(new byte[] { ReturnByte[32], ReturnByte[33], ReturnByte[34], ReturnByte[35] }, 0);
}
if (ReturnByte[0] == 0xEB && ReturnByte[1] == 0x52) //NTFS seems to be 64
{
_SectorLength = BitConverter.ToInt64(new byte[] { ReturnByte[40], ReturnByte[41], ReturnByte[42], ReturnByte[43], ReturnByte[44], ReturnByte[45], ReturnByte[46], ReturnByte[47] }, 0);
}

}
public byte[] uSD_ReadSector(long sectornumber)
{
byte[] SecBuffer = new byte[520];
_DriverStream.Seek(sectornumber * 512, SeekOrigin.Begin);
_DriverStream.Read(SecBuffer, 0, 512);
return SecBuffer;
}
public bool uSD_WriteSector(long sectornumber,byte[] buffer)
{
try
{
byte[] rdbuf = new byte[1000];
int i;
_DriverStream.Seek(sectornumber * 512, SeekOrigin.Begin);
_DriverStream.Write(buffer, 0, 512);
_DriverStream.Seek(sectornumber * 512, SeekOrigin.Begin);
_DriverStream.Read(rdbuf, 0, 512);
for (i = 0; i < 512; i++)
if (buffer[i] != rdbuf[i])
{
string ge = "Error in sector :" + sectornumber.ToString();
MessageBox.Show(ge);
return false;
}
return true;
}
catch
{
return false;
}
}
}
}

amir_mhdi
سه شنبه 05 آبان 1394, 14:57 عصر
آقا راه حل رو پیدا کردم. این dll رو که نامش RAW.dll هست دانلود کنید و Add Reference کنید.
http://www.dreamincode.net/forums/index.php?app=core&module=attach&section=attach&attach_id=33751&

نمونه کد هم اینجوریه :

RAW.DISK.streamer mystream;
mystream = RAW.DISK.CreateStream("H:",System.IO.FileAccess.ReadWrite);
if (mystream.isERROR == true)
{
MessageBox.Show("Error!");
Application.Exit();
}
int i;
byte[] ff = new byte[1000];
ff = RAW.DISK.ReadSector(0,512, mystream);