Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Основная";
Текущий архив: 2004.07.25;
Скачать: [xml.tar.bz2];

Вниз

Помогите перевести код из Си в Делфи.   Найти похожие ветки 

 
Tahion2   (2004-07-10 14:20) [0]

помогите перевести этот код в Делфийское представление:


typedef struct
{
WORD idReserved;
WORD idType;
WORD idCount;
ICONDIRENTRY idEntries[1];
}ICONDIR, *LPICONDIR;

typedef struct
{
BYTE bWidth;
BYTE bHeight;
BYTE bColorCount;
BYTE bReserved;
WORD wPlanes;
WORD wBitCount;
DWORD dwBytesInRes;
DWORD dwImageOffset;
}ICONDIRENTRY, *LPICONDIRENTRY;

DWORD dwBytesRead;
HANDLE hf = ::CreateFile(Form1 -> OpenDialog1 -> FileName.c_str(),
GENERIC_READ, (DWORD)0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
icdir = (ICONDIR*) malloc (sizeof(ICONDIR));
::ReadFile (hf, icdir, 3 * sizeof(WORD), &dwBytesRead, NULL);
icdir = (ICONDIR*) realloc (icdir, (3 * sizeof(WORD)+sizeof(ICONDIRENTRY)*
icdir->idCount));
::ReadFile (hf,icdir->idEntries, sizeof (ICONDIRENTRY) *icdir->idCount,  
&dwBytesRead, NULL);
CloseHandle (hf);
hf = NULL;
Label1 -> Caption = "This file contains " + (String)icdir -> idCount + " icons.";


 
KilkennyCat ©   (2004-07-10 14:48) [1]

Начинайте, будут ошибки - поправим.


 
y-soft ©   (2004-07-10 15:24) [2]

Редактор файлов иконок пишем? :)


 
Fay   (2004-07-10 15:46) [3]

1) Перевод дословный. В Паскале array[0..0] не принесёт тебе счастья. 8)
2) Код - барахло. Перепишешь по людски.

type
 ICONDIRENTRY = record
   bWidth : Byte;
   bHeight : Byte;
   bColorCount : Byte;
   bReserved : Byte;
   wPlanes : WORD;
   wBitCount : WORD;
   dwBytesInRes : DWORD;
   dwImageOffset : DWORD;
 end;
 LPICONDIRENTRY = ^ICONDIRENTRY;

 ICONDIR = record
   idReserved : WORD;
   idType : WORD;
   idCount : WORD;
   idEntries : array [0..0] of ICONDIRENTRY;
 end;
 LPICONDIR = ^ICONDIR;
var
 hf, dwBytesRead : DWORD;
 icdir : LPICONDIR;
begin
 GetMem(icdir, SizeOf(ICONDIR));
 hf := CreateFile(PChar(Form1.OpenDialog1.FileName), GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
 ReadFile(hf, icdir^, 3 * SizeOf(WORD), dwBytesRead, nil);
 ReallocMem(icdir, 3 * SizeOf(WORD) + SizeOf(ICONDIRENTRY) * icdir^.idCount);
 ReadFile(hf, icdir^.idEntries, SizeOf(ICONDIRENTRY) * icdir^.idCount, dwBytesRead, nil);
 Label1.Caption := "This file contains " + IntToStr(icdir^.idCount) + " icons.";
 CloseHandle(hf);
 hf := 0;


 
y-soft ©   (2004-07-10 16:09) [4]

>Fay   (10.07.04 15:46) [3]

2) Код - барахло. Перепишешь по людски

Вот код примера из MSDN:

// We need an ICONDIR to hold the data
pIconDir = malloc( sizeof( ICONDIR ) );
// Read the Reserved word
ReadFile( hFile, &(pIconDir->idReserved), sizeof( WORD ), &dwBytesRead, NULL );
// Read the Type word - make sure it is 1 for icons
ReadFile( hFile, &(pIconDir->idType), sizeof( WORD ), &dwBytesRead, NULL );
// Read the count - how many images in this file?
ReadFile( hFile, &(pIconDir->idCount), sizeof( WORD ), &dwBytesRead, NULL );
// Reallocate IconDir so that idEntries has enough room for idCount elements
pIconDir = realloc( pIconDir, ( sizeof( WORD ) * 3 ) +
                             ( sizeof( ICONDIRENTRY ) * pIconDir->idCount ) );
// Read the ICONDIRENTRY elements
ReadFile( hFile, pIconDir->idEntries, pIconDir->idCount * sizeof(ICONDIRENTRY),
         &dwBytesRead, NULL );
// Loop through and read in each image
for(i=0;i<pIconDir->idCount;i++)
{
 // Allocate memory to hold the image
 pIconImage = malloc( pIconDir->idEntries[i].dwBytesInRes );
 // Seek to the location in the file that has the image
 SetFilePointer( hFile, pIconDir->idEntries[i].dwImageOffset,
                 NULL, FILE_BEGIN );
 // Read the image data
 ReadFile( hFile, pIconImage, pIconDir->idEntries[i].dwBytesInRes,
           &dwBytesRead, NULL );
 // Here, pIconImage is an ICONIMAGE structure. Party on it :)
 // Then, free the associated memory
 free( pIconImage );
}
// Clean up the ICONDIR memory
free( pIconDir );


Не правда ли - очень похоже?  Тоже никаких проверок и исключений :)
Правда там есть приписка:

The following is an incomplete code fragment for reading an .ICO file :)



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

Форум: "Основная";
Текущий архив: 2004.07.25;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.46 MB
Время: 0.038 c
8-1084014356
Sonic_gd
2004-05-08 15:05
2004.07.25
Контекстное меню


3-1088765847
Nikolai_S
2004-07-02 14:57
2004.07.25
Как записать в поле binary файл и потом считать его обратно?


3-1088412936
MORA
2004-06-28 12:55
2004.07.25
dbExpress


14-1089111495
wnew
2004-07-06 14:58
2004.07.25
Скрытые функции в Delphi


14-1089174057
leonidus
2004-07-07 08:20
2004.07.25
Идея





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский