Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2006.09.10;
Скачать: CL | DM;

Вниз

Запись в MBR   Найти похожие ветки 

 
ArtemESC ©   (2006-08-17 12:38) [0]

Пытаюсь написать маленькую утилиту для сабжа, но
она почему-то не работает...
var
 FS    : TFileStream;
 Handle: THandle;
 FB, EB: Integer;
 arr   : array of byte;
 i     : integer;
begin
Writeln(" "Disc" - is utility to write file in sectors of a disc");
Writeln;
Writeln("          1st parameter - name of the file");
Writeln("          2nd - Disc(For example - A:)");
Writeln("          3d  - BootDisk? (y - make boot disc, n - don""t make boot)");
Writeln("Next parametrs may don""t write...");
Writeln("          4 - First byte of the file...");
Writeln("          5 - End byte of the file...");
 If ParamCount < 3 then
    Writeln("Error. {Count of param}") else
      begin
       If FileExists(ParamStr(1)) then
          begin
          try
           FS := TFileStream.Create(ParamStr(1), fmOpenReadWrite);
           Handle := FileOpen("\\.\"+ParamStr(2), fmOpenReadWrite);
           If (Handle = -1) then Writeln("Error of access to disc...")
           else begin
                 If ParamCount > 3 then
                  FB := StrToInt(ParamStr(4)) else FB := 1;
                 If ParamCount > 4 then
                  EB := StrToInt(ParamStr(4)) else EB := FS.Size;
                  SetLength(arr, EB - FB + 1);
                  FS.Position := FB - 1;
                  FS.Read(arr, EB - FB);
                  FileWrite(Handle, arr, EB - FB);
                  FileClose(Handle);
                end;
          except
            end;
          end else Writeln("Error. File was not found...");
      end;
  Readln;
end.

Что я делаю не так...


 
Ketmar ©   (2006-08-17 12:40) [1]

всё. %-)


 
ArtemESC ©   (2006-08-17 12:42) [2]

Ketmar ©   (17.08.06 12:40) [1]
Может покажешь, как правильно?


 
isasa ©   (2006-08-17 12:42) [3]

Тут эта. А разве MBR в рамках файловой структуры?


 
ArtemESC ©   (2006-08-17 12:46) [4]

isasa ©   (17.08.06 12:42) [3]
Мне сказали, что если
используеться такая вещь "\\.\", то ты как раз работаешь с первыми секторами


 
Ketmar ©   (2006-08-17 12:48) [5]

> [3] isasa ©   (17.08.06 12:42)
всё нормально, CreateFile() на NT позволяет работать с дисками таким образом. но именно CreateFile(), а не TFileStream.

> [2] ArtemESC ©   (17.08.06 12:42)
используй CreateFile(). и не забудь проверить, есть ли у юзера права на чтение/запись секторов таким образом.


 
Ketmar ©   (2006-08-17 12:49) [6]

> [4] ArtemESC ©   (17.08.06 12:46)
а ты не полашайся на "мне сказали". ты справку почитай. по CreateFile(). там всё ражёвано.


 
ArtemESC ©   (2006-08-17 12:50) [7]

Ketmar ©   (17.08.06 12:48) [5]
используй CreateFile(). и не забудь проверить, есть ли у юзера права на чтение/запись секторов таким образом.

А как?


 
tesseract ©   (2006-08-17 12:52) [8]


> ArtemESC ©   (17.08.06 12:50) [7]


CreateFile не сработает на открытии с флагом чтения/записи. GetLastError вернёт по моему ERROR_ACCESS_DENIED.


 
Ketmar ©   (2006-08-17 12:59) [9]

> [7] ArtemESC ©   (17.08.06 12:50)
а справку почитай.


 
ArtemESC ©   (2006-08-17 13:36) [10]

Немного исправил - где я опять ошибся?
var
 FS    : TFileStream;
 Handle: THandle;
 FB, EB: Integer;
 arr   : array of byte;
 i     : integer;
begin
Writeln(" "Disc" - is utility to write file in first sectors of a disc");
Writeln;
Writeln("          1st parameter - name of the file");
Writeln("          2nd - Disc(For example - A:)");
Writeln("Next parametrs may don""t write...");
Writeln("          3 - First byte of the file...");
Writeln("          4 - End byte of the file...");
 If ParamCount < 2 then
    Writeln("Error. {Count of parametrs}") else
      begin
       If FileExists(ParamStr(1)) then
          begin
          try
           FS := TFileStream.Create(ParamStr(1), fmOpenReadWrite);
           Handle := CreateFile(PChar("\\.\"+ParamStr(2)), GENERIC_WRITE, 0,
                         nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

           If (Handle = 0) then Writeln("Error of access to disc...")
           else begin
                 If ParamCount > 2 then
                  FB := StrToInt(ParamStr(3)) else FB := 1;
                 If ParamCount > 3 then
                  EB := StrToInt(ParamStr(4)) else EB := FS.Size;
                  SetLength(arr, EB - FB + 1);
                  FS.Position := FB - 1;
                  FS.Read(arr[0], EB - FB);
                  FileWrite(Handle, arr[0], EB - FB);
                  FileClose(Handle);
                end;
          except
            end;
          end else Writeln("Error. File was not found...");
       end;
  Readln;
end.


 
Чапаев ©   (2006-08-17 13:40) [11]


> If (Handle = 0) then Writeln("Error of access to disc...
> ")

А справку почитать?


 
Ketmar ©   (2006-08-17 13:49) [12]

> [11] Чапаев ©   (17.08.06 13:40)
а в чём проблема?


 
Чапаев ©   (2006-08-17 13:53) [13]


> Ketmar ©   (17.08.06 13:49) [12]

If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.


 
Ketmar ©   (2006-08-17 13:57) [14]

> [13] Чапаев ©   (17.08.06 13:53)
упс. пардон, я был не прав. ангелов склероз!


 
ArtemESC ©   (2006-08-17 14:06) [15]

Вместо массива стал использовать string - все вроде работает, кроме  
FileWrite(Handle, Pchar(str)^, EB - FB);


 
Чапаев ©   (2006-08-17 14:10) [16]

Откуда ты этих древних функций набрал? FileOpen, FileWrite...


 
tesseract ©   (2006-08-17 14:11) [17]


> ArtemESC ©   (17.08.06 14:06) [15]

лучше перед записью в Pchar перегони - дожно сработать


 
ArtemESC ©   (2006-08-17 14:11) [18]

Чапаев ©   (17.08.06 14:10) [16]
А что предложете?


 
tesseract ©   (2006-08-17 14:14) [19]


> ArtemESC ©   (17.08.06 14:11) [18]


WriteFile наверное


 
ArtemESC ©   (2006-08-17 14:24) [20]

tesseract ©   (17.08.06 14:14) [19]
Не помогает - WriteFile возвращает False...


 
Чапаев ©   (2006-08-17 14:25) [21]


> WriteFile наверное

Ты знал! Ты знал!


 
tesseract ©   (2006-08-17 14:28) [22]


> ArtemESC ©   (17.08.06 14:24) [20]


значит ошибка в коде.


 
ArtemESC ©   (2006-08-17 14:31) [23]

tesseract ©   (17.08.06 14:28) [22]
Делаю так

                 b := WriteFile(Handle,
                                  Pchar(arr)^,
                                  EB - FB, temp, nil);


 
ArtemESC ©   (2006-08-17 14:34) [24]

ArtemESC ©   (17.08.06 14:31) [23]
arr - это строка


 
Ketmar ©   (2006-08-17 14:35) [25]

> [20] ArtemESC ©   (17.08.06 14:24)
дык ручка, наверное, не от той двери...


 
ArtemESC ©   (2006-08-17 14:36) [26]

Ketmar ©   (17.08.06 14:35) [25]
О чем ты?


 
tesseract ©   (2006-08-17 14:39) [27]


> ArtemESC ©   (17.08.06 14:34) [24]


вот избавься от неё, передавай именно PChar.


 
ArtemESC ©   (2006-08-17 14:45) [28]

tesseract ©   (17.08.06 14:39) [27]
Не помогает
Пишу так, (P - Pchar, в ней точно есть нужное содержимое)                  
b := WriteFile(Handle, P, EB - FB, temp, nil);


 
Ketmar ©   (2006-08-17 14:46) [29]

> [26] ArtemESC ©   (17.08.06 14:36)
о handle.


 
ArtemESC ©   (2006-08-17 14:51) [30]

Ketmar ©   (17.08.06 14:46) [29]
Handle добываю так:
            Handle := CreateFile(PChar("\\.\"+ParamStr(2)), GENERIC_WRITE, 0,
                         nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
и вполне валидный, флоппи-дисковод издает соответствующий звук...


 
Ketmar ©   (2006-08-17 15:06) [31]

> [30] ArtemESC ©   (17.08.06 14:51)
не забыл, что писать надо порциями, кратными размеру сектора?


 
ArtemESC ©   (2006-08-17 15:13) [32]

Ketmar ©   (17.08.06 15:06) [31]
Pchar я сделал размером 512, не помагло...


 
Ketmar ©   (2006-08-17 15:17) [33]

> [32] ArtemESC ©   (17.08.06 15:13)
чего-чего размер? куда-куда? %-)
EB-FB кратно 512?

и: что сообщает RaiseLastWin32Error()?


 
ArtemESC ©   (2006-08-17 15:23) [34]

>> Ketmar ©   (17.08.06 15:17) [33]
>> чего-чего размер? куда-куда? %-)
>> EB-FB кратно 512?
УПС!!! пора мне на кладбище. Теперь все работает... ОГРОМНОЕ СПАСИБО!!!


 
tesseract ©   (2006-08-17 15:26) [35]


>  Handle := CreateFile(PChar("\\.\"+ParamStr(2)), GENERIC_WRITE,
>  0,                          nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
>  0);

SectorSize=512;
попробуй так :

CreateFile(PChar("\\.\"+ParamStr(2)), GENERIC_WRITE, FILE_SHARE_WRITE,  nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,  0);

SetFilePointer(handle,(номерсектора-1)*SectorSize,nil,FILE_BEGIN);

result:= WriteFile(handle,p,SectorSize,temp,nil) and (SectorSize=512);


 
tesseract ©   (2006-08-17 15:26) [36]


> and (SectorSize=512);


не то and (temp=SectorSize);


 
Ketmar ©   (2006-08-17 15:29) [37]

> [35] tesseract ©   (17.08.06 15:26)
да пофигу, как проверять потом. главное, чтобы костюмчик был кратен размеру сектора. %-)


 
ArtemESC ©   (2006-08-17 16:39) [38]

tesseract ©   (17.08.06 15:26) [35]
Ужа разобрался, спасибо...


 
tesseract ©   (2006-08-17 16:43) [39]


> ArtemESC ©   (17.08.06 16:39) [38]


Печатал долго :-)


 
ArtemESC ©   (2006-08-17 16:47) [40]

сожелею...



Страницы: 1 2 вся ветка

Текущий архив: 2006.09.10;
Скачать: CL | DM;

Наверх




Память: 0.56 MB
Время: 0.044 c
15-1155618462
Ega23
2006-08-15 09:07
2006.09.10
С Днём рождения! 15 августа


2-1155803333
Ega23
2006-08-17 12:28
2006.09.10
Надо реализовать свои часы


1-1153902373
Yozch1
2006-07-26 12:26
2006.09.10
Пропали вкладки палитры компонентов


15-1155808965
sleept
2006-08-17 14:02
2006.09.10
еще раз про пхп


2-1155997184
viktoras
2006-08-19 18:19
2006.09.10
Поиск слов по шаблону