Главная страница
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.48 MB
Время: 0.035 c
8-1083954460
NightDevil
2004-05-07 22:27
2004.07.25
Вопрос по медиа плееру...


3-1088721442
Daniil
2004-07-02 02:37
2004.07.25
DBGrid


3-1088586235
maximus_
2004-06-30 13:03
2004.07.25
Не получается с запросом IBDataSet


8-1083819071
Kerget Ruslan
2004-05-06 08:51
2004.07.25
Проблемы при работе с TShockwaveFlash


1-1089478690
Delphi5.01
2004-07-10 20:58
2004.07.25
Prostoi vopros po povodu FileStream, CopyFrom! :-)