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

Вниз

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

 
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;
Скачать: CL | DM;

Наверх




Память: 0.46 MB
Время: 0.04 c
1-1089310072
Shc
2004-07-08 22:07
2004.07.25
Работа с XML (и Schema)


1-1089753501
RedLord
2004-07-14 01:18
2004.07.25
сохранение объекта в файле


14-1088889684
Baron
2004-07-04 01:21
2004.07.25
Моя девушка смерти моей хочит.


6-1085381014
Werewolf
2004-05-24 10:43
2004.07.25
Как работать через прокси?


14-1088768593
AlexG
2004-07-02 15:43
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
Английский Французский Немецкий Итальянский Португальский Русский Испанский