1 static char RCSId
[] = "$Id: resource.c,v 1.4 1993/07/04 04:04:21 root Exp root $";
2 static char Copyright
[] = "Copyright Robert J. Amstadt, 1993";
13 #include "prototypes.h"
21 /* #define DEBUG_RESOURCE */
23 #define MIN(a,b) ((a) < (b) ? (a) : (b))
25 typedef struct resource_s
27 struct resource_s
*next
;
30 struct resource_nameinfo_s nameinfo
;
34 static int ResourceFd
= -1;
35 static HANDLE ResourceInst
= 0;
36 static struct w_files
*ResourceFileInfo
= NULL
;
37 static RESOURCE
*Top
= NULL
;
38 extern HINSTANCE hSysRes
;
40 extern HBITMAP
BITMAP_LoadOEMBitmap( WORD id
); /* objects/bitmap.c */
42 HANDLE
RSC_LoadResource(int instance
, char *rsc_name
, int type
,
44 void RSC_LoadNameTable(void);
46 extern char *ProgramName
;
49 /**********************************************************************
56 struct resource_typeinfo_s typeinfo
;
57 struct resource_nameinfo_s nameinfo
;
58 unsigned short size_shift
;
70 * Move to beginning of resource table.
72 rtoff
= (ResourceFileInfo
->mz_header
->ne_offset
+
73 ResourceFileInfo
->ne_header
->resource_tab_offset
);
74 lseek(ResourceFd
, rtoff
, SEEK_SET
);
79 if (read(ResourceFd
, &size_shift
, sizeof(size_shift
)) !=
84 size_shift
= CONV_SHORT(size_shift
);
89 typeinfo
.type_id
= 0xffff;
90 while (typeinfo
.type_id
!= 0)
92 if (!load_typeinfo (ResourceFd
, &typeinfo
))
95 if (typeinfo
.type_id
== 0)
97 if (typeinfo
.type_id
== 0x800f)
99 for (i
= 0; i
< typeinfo
.count
; i
++)
101 if (read(ResourceFd
, &nameinfo
, sizeof(nameinfo
)) !=
107 saved_pos
= lseek(ResourceFd
, 0, SEEK_CUR
);
108 lseek(ResourceFd
, (long) nameinfo
.offset
<< size_shift
,
110 read(ResourceFd
, &len
, sizeof(len
));
113 new = (RESNAMTAB
*) GlobalQuickAlloc(sizeof(*new));
117 read(ResourceFd
, &new->type_ord
, 2);
118 read(ResourceFd
, &new->id_ord
, 2);
119 read(ResourceFd
, read_buf
, len
- 6);
121 p
= read_buf
+ strlen(read_buf
) + 1;
122 strncpy(new->id
, p
, MAX_NAME_LENGTH
);
123 new->id
[MAX_NAME_LENGTH
- 1] = '\0';
125 read(ResourceFd
, &len
, sizeof(len
));
128 lseek(ResourceFd
, saved_pos
, SEEK_SET
);
134 lseek(ResourceFd
, (typeinfo
.count
* sizeof(nameinfo
)), SEEK_CUR
);
138 ResourceFileInfo
->resnamtab
= top
;
142 /**********************************************************************
146 OpenResourceFile(HANDLE instance
)
151 if (ResourceInst
== instance
)
154 w
= GetFileInfo(instance
);
157 ResourceFileInfo
= w
;
158 res_file
= w
->filename
;
163 ResourceInst
= instance
;
164 ResourceFd
= open (res_file
, O_RDONLY
);
167 if (w
->resnamtab
== (RESNAMTAB
*) -1)
174 #ifdef DEBUG_RESOURCE
175 printf("OpenResourceFile(%04X) // file='%s' hFile=%04X !\n",
176 instance
, w
->filename
, ResourceFd
);
181 /**********************************************************************
185 ConvertCoreBitmap( HDC hdc
, BITMAPCOREHEADER
* image
)
187 BITMAPINFO
* bmpInfo
;
190 int i
, size
, n_colors
;
192 n_colors
= 1 << image
->bcBitCount
;
194 if (image
->bcBitCount
< 24)
196 size
= sizeof(BITMAPINFOHEADER
) + n_colors
* sizeof(RGBQUAD
);
197 bits
= (char *) (image
+ 1) + (n_colors
* sizeof(RGBTRIPLE
));
201 size
= sizeof(BITMAPINFOHEADER
);
202 bits
= (char *) (image
+ 1);
204 bmpInfo
= (BITMAPINFO
*) malloc( size
);
206 bmpInfo
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
207 bmpInfo
->bmiHeader
.biWidth
= image
->bcWidth
;
208 bmpInfo
->bmiHeader
.biHeight
= image
->bcHeight
;
209 bmpInfo
->bmiHeader
.biPlanes
= image
->bcPlanes
;
210 bmpInfo
->bmiHeader
.biBitCount
= image
->bcBitCount
;
211 bmpInfo
->bmiHeader
.biCompression
= 0;
212 bmpInfo
->bmiHeader
.biSizeImage
= 0;
213 bmpInfo
->bmiHeader
.biXPelsPerMeter
= 0;
214 bmpInfo
->bmiHeader
.biYPelsPerMeter
= 0;
215 bmpInfo
->bmiHeader
.biClrUsed
= 0;
216 bmpInfo
->bmiHeader
.biClrImportant
= 0;
218 if (image
->bcBitCount
< 24)
220 RGBTRIPLE
* oldMap
= (RGBTRIPLE
*)(image
+ 1);
221 RGBQUAD
* newMap
= bmpInfo
->bmiColors
;
222 for (i
= 0; i
< n_colors
; i
++, oldMap
++, newMap
++)
224 newMap
->rgbRed
= oldMap
->rgbtRed
;
225 newMap
->rgbGreen
= oldMap
->rgbtGreen
;
226 newMap
->rgbBlue
= oldMap
->rgbtBlue
;
227 newMap
->rgbReserved
= 0;
231 hbitmap
= CreateDIBitmap( hdc
, &bmpInfo
->bmiHeader
, CBM_INIT
,
232 bits
, bmpInfo
, DIB_RGB_COLORS
);
237 /**********************************************************************
241 ConvertInfoBitmap( HDC hdc
, BITMAPINFO
* image
)
243 char * bits
= ((char *)image
) + DIB_BitmapInfoSize(image
, DIB_RGB_COLORS
);
244 return CreateDIBitmap( hdc
, &image
->bmiHeader
, CBM_INIT
,
245 bits
, image
, DIB_RGB_COLORS
);
249 load_typeinfo (int fd
, struct resource_typeinfo_s
*typeinfo
)
251 return read (fd
, typeinfo
, sizeof (*typeinfo
)) == sizeof (*typeinfo
);
256 type_match(int type_id1
, int type_id2
, int fd
, off_t off
)
265 if ((type_id1
& 0xffff0000) == 0) {
266 if ((type_id2
& 0x8000) == 0)
268 return (type_id1
& 0x000f) == (type_id2
& 0x000f);
270 if ((type_id2
& 0x8000) != 0)
272 #ifdef DEBUG_RESOURCE
273 printf("type_compare: type_id2=%04X !\n", type_id2
);
275 old_pos
= lseek(fd
, 0, SEEK_CUR
);
276 lseek(fd
, off
+ type_id2
, SEEK_SET
);
278 nbytes
= CONV_CHAR_TO_LONG (c
);
279 #ifdef DEBUG_RESOURCE
280 printf("type_compare: namesize=%d\n", nbytes
);
282 read(fd
, name
, nbytes
);
283 lseek(fd
, old_pos
, SEEK_SET
);
285 #ifdef DEBUG_RESOURCE
286 printf("type_compare: name=`%s'\n", name
);
288 return strcasecmp((char *) type_id1
, name
) == 0;
291 /**********************************************************************
292 * FindResourceByNumber
295 FindResourceByNumber(struct resource_nameinfo_s
*result_p
,
296 int type_id
, int resource_id
)
298 struct resource_typeinfo_s typeinfo
;
299 struct resource_nameinfo_s nameinfo
;
300 unsigned short size_shift
;
305 * Move to beginning of resource table.
307 rtoff
= (ResourceFileInfo
->mz_header
->ne_offset
+
308 ResourceFileInfo
->ne_header
->resource_tab_offset
);
309 lseek(ResourceFd
, rtoff
, SEEK_SET
);
314 if (read(ResourceFd
, &size_shift
, sizeof(size_shift
)) !=
317 printf("FindResourceByNumber (%s) bad block size !\n", resource_id
);
320 size_shift
= CONV_SHORT(size_shift
);
325 if (!load_typeinfo (ResourceFd
, &typeinfo
)){
326 printf("FindResourceByNumber (%X) bad typeinfo size !\n", resource_id
);
329 #ifdef DEBUG_RESOURCE
330 printf("FindResourceByNumber type=%X count=%d searched=%d \n",
331 typeinfo
.type_id
, typeinfo
.count
, type_id
);
333 if (typeinfo
.type_id
== 0) break;
334 if (type_match(type_id
, typeinfo
.type_id
, ResourceFd
, rtoff
)) {
336 for (i
= 0; i
< typeinfo
.count
; i
++) {
338 if (read(ResourceFd
, &nameinfo
, sizeof(nameinfo
)) !=
341 if (!load_nameinfo (ResourceFd
, &nameinfo
))
344 printf("FindResourceByNumber (%X) bad nameinfo size !\n", resource_id
);
347 #ifdef DEBUG_RESOURCE
348 printf("FindResource: search type=%X id=%X // type=%X id=%X\n",
349 type_id
, resource_id
, typeinfo
.type_id
, nameinfo
.id
);
351 if (nameinfo
.id
== resource_id
) {
352 memcpy(result_p
, &nameinfo
, sizeof(nameinfo
));
358 lseek(ResourceFd
, (typeinfo
.count
* sizeof(nameinfo
)), SEEK_CUR
);
364 /**********************************************************************
368 FindResourceByName(struct resource_nameinfo_s
*result_p
,
369 int type_id
, char *resource_name
)
371 struct resource_typeinfo_s typeinfo
;
372 struct resource_nameinfo_s nameinfo
;
373 unsigned short size_shift
;
374 off_t old_pos
, new_pos
;
375 unsigned char nbytes
;
381 * Check for loaded name table.
383 if (ResourceFileInfo
->resnamtab
!= NULL
)
387 for (e
= ResourceFileInfo
->resnamtab
; e
!= NULL
; e
= e
->next
)
389 if (e
->type_ord
== (type_id
& 0x000f) &&
390 strcasecmp(e
->id
, resource_name
) == 0)
392 return FindResourceByNumber(result_p
, type_id
, e
->id_ord
);
400 * Move to beginning of resource table.
402 rtoff
= (ResourceFileInfo
->mz_header
->ne_offset
+
403 ResourceFileInfo
->ne_header
->resource_tab_offset
);
404 lseek(ResourceFd
, rtoff
, SEEK_SET
);
409 if (read(ResourceFd
, &size_shift
, sizeof(size_shift
)) !=
412 printf("FindResourceByName (%s) bad block size !\n", resource_name
);
415 size_shift
= CONV_SHORT (size_shift
);
422 if (!load_typeinfo (ResourceFd
, &typeinfo
))
424 printf("FindResourceByName (%s) bad typeinfo size !\n", resource_name
);
427 #ifdef DEBUG_RESOURCE
428 printf("FindResourceByName typeinfo.type_id=%X count=%d type_id=%X\n",
429 typeinfo
.type_id
, typeinfo
.count
, type_id
);
431 if (typeinfo
.type_id
== 0) break;
432 if (type_match(type_id
, typeinfo
.type_id
, ResourceFd
, rtoff
))
434 for (i
= 0; i
< typeinfo
.count
; i
++)
437 if (read(ResourceFd
, &nameinfo
, sizeof(nameinfo
)) !=
440 if (!load_nameinfo (ResourceFd
, &nameinfo
))
443 printf("FindResourceByName (%s) bad nameinfo size !\n", resource_name
);
447 if ((nameinfo.id & 0x8000) != 0) continue;
449 #ifdef DEBUG_RESOURCE
450 printf("FindResourceByName // nameinfo.id=%04X !\n", nameinfo
.id
);
452 old_pos
= lseek(ResourceFd
, 0, SEEK_CUR
);
453 new_pos
= rtoff
+ nameinfo
.id
;
454 lseek(ResourceFd
, new_pos
, SEEK_SET
);
455 read(ResourceFd
, &nbytes
, 1);
456 #ifdef DEBUG_RESOURCE
457 printf("FindResourceByName // namesize=%d !\n", nbytes
);
459 nbytes
= CONV_CHAR_TO_LONG (nbytes
);
460 read(ResourceFd
, name
, nbytes
);
461 lseek(ResourceFd
, old_pos
, SEEK_SET
);
463 #ifdef DEBUG_RESOURCE
464 printf("FindResourceByName type_id=%X (%d of %d) name='%s' resource_name='%s'\n",
465 typeinfo
.type_id
, i
+ 1, typeinfo
.count
,
466 name
, resource_name
);
468 if (strcasecmp(name
, resource_name
) == 0)
470 memcpy(result_p
, &nameinfo
, sizeof(nameinfo
));
476 lseek(ResourceFd
, (typeinfo
.count
* sizeof(nameinfo
)), SEEK_CUR
);
483 /**********************************************************************
484 * GetRsrcCount [internal]
486 int GetRsrcCount(HINSTANCE hInst
, int type_id
)
488 struct resource_typeinfo_s typeinfo
;
489 struct resource_nameinfo_s nameinfo
;
490 unsigned short size_shift
;
491 /* off_t old_pos, new_pos;
492 unsigned char nbytes;
496 if (hInst
== 0) return 0;
497 #ifdef DEBUG_RESOURCE
498 printf("GetRsrcCount hInst=%04X typename=%08X\n", hInst
, type_id
);
500 if (OpenResourceFile(hInst
) < 0) return 0;
503 * Move to beginning of resource table.
505 rtoff
= (ResourceFileInfo
->mz_header
->ne_offset
+
506 ResourceFileInfo
->ne_header
->resource_tab_offset
);
507 lseek(ResourceFd
, rtoff
, SEEK_SET
);
511 if (read(ResourceFd
, &size_shift
, sizeof(size_shift
)) != sizeof(size_shift
)) {
512 printf("GetRsrcCount // bad block size !\n");
515 size_shift
= CONV_SHORT (size_shift
);
517 if (!load_typeinfo (ResourceFd
, &typeinfo
)) {
518 printf("GetRsrcCount // bad typeinfo size !\n");
521 #ifdef DEBUG_RESOURCE
522 printf("GetRsrcCount // typeinfo.type_id=%X count=%d type_id=%X\n",
523 typeinfo
.type_id
, typeinfo
.count
, type_id
);
525 if (typeinfo
.type_id
== 0) break;
526 if (type_match(type_id
, typeinfo
.type_id
, ResourceFd
, rtoff
)) {
527 return typeinfo
.count
;
530 lseek(ResourceFd
, (typeinfo
.count
* sizeof(nameinfo
)), SEEK_CUR
);
537 /**********************************************************************
538 * LoadIcon [USER.174]
540 HICON
LoadIcon(HANDLE instance
, LPSTR icon_name
)
545 ICONDESCRIP
*lpicodesc
;
549 BITMAPINFOHEADER
*bih
;
555 int i
, j
, image_size
;
556 #ifdef DEBUG_RESOURCE
557 printf("LoadIcon: instance = %04x, name = %08x\n",
558 instance
, icon_name
);
561 if (!(hdc
= GetDC(GetDesktopWindow()))) return 0;
562 if (instance
== (HANDLE
)NULL
) instance
= hSysRes
;
563 rsc_mem
= RSC_LoadResource(instance
, icon_name
, NE_RSCTYPE_GROUP_ICON
,
565 if (rsc_mem
== (HANDLE
)NULL
) {
566 printf("LoadIcon / Icon %04X not Found !\n", icon_name
);
567 ReleaseDC(GetDesktopWindow(), hdc
);
570 lp
= (WORD
*)GlobalLock(rsc_mem
);
573 ReleaseDC(GetDesktopWindow(), hdc
);
576 lpicodesc
= (ICONDESCRIP
*)(lp
+ 3);
577 hIcon
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(ICONALLOC
) + 1024);
578 if (hIcon
== (HICON
)NULL
) {
580 ReleaseDC(GetDesktopWindow(), hdc
);
583 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
584 lpico
->descriptor
= *lpicodesc
;
585 width
= lpicodesc
->Width
;
586 height
= lpicodesc
->Height
;
587 GlobalUnlock(rsc_mem
);
589 rsc_mem
= RSC_LoadResource(instance
,
590 MAKEINTRESOURCE(lpicodesc
->icoDIBOffset
),
591 NE_RSCTYPE_ICON
, &image_size
);
592 if (rsc_mem
== (HANDLE
)NULL
) {
593 printf("LoadIcon / Icon %04X Bitmaps not Found !\n", icon_name
);
594 ReleaseDC(GetDesktopWindow(), hdc
);
597 lp
= (WORD
*)GlobalLock(rsc_mem
);
600 ReleaseDC(GetDesktopWindow(), hdc
);
603 bmi
= (BITMAPINFO
*)lp
;
604 bih
= (BITMAPINFOHEADER
*)lp
;
605 rgbq
= &bmi
->bmiColors
[0];
606 bih
->biHeight
= bih
->biHeight
/ 2;
608 printf("LoadIcon / image_size=%d width=%d height=%d bih->biBitCount=%d bih->biSizeImage=%ld\n",
609 image_size, width, height, bih->biBitCount, bih->biSizeImage);
611 if (bih
->biSize
== sizeof(BITMAPINFOHEADER
))
612 lpico
->hBitmap
= ConvertInfoBitmap(hdc
, (BITMAPINFO
*)bih
);
616 bih
->biClrUsed
= bih
->biClrImportant
= 2;
617 rgbq
[0].rgbBlue
= 0xFF;
618 rgbq
[0].rgbGreen
= 0xFF;
619 rgbq
[0].rgbRed
= 0xFF;
620 rgbq
[0].rgbReserved
= 0x00;
621 rgbq
[1].rgbBlue
= 0x00;
622 rgbq
[1].rgbGreen
= 0x00;
623 rgbq
[1].rgbRed
= 0x00;
624 rgbq
[1].rgbReserved
= 0x00;
625 if (bih
->biSizeImage
== 0) {
626 if (bih
->biCompression
!= BI_RGB
) {
627 printf("Unknown size for compressed Icon bitmap.\n");
629 ReleaseDC(GetDesktopWindow(), hdc
);
632 bih
->biSizeImage
= (bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
635 lpico
->hBitMask
= CreateDIBitmap(hdc
, bih
, CBM_INIT
,
636 (LPSTR
)lp
+ bih
->biSizeImage
- sizeof(BITMAPINFOHEADER
) / 2 - 4,
637 (BITMAPINFO
*)bih
, DIB_RGB_COLORS
);
638 GlobalUnlock(rsc_mem
);
640 hMemDC
= CreateCompatibleDC(hdc
);
641 hMemDC2
= CreateCompatibleDC(hdc
);
642 SelectObject(hMemDC
, lpico
->hBitmap
);
643 SelectObject(hMemDC2
, lpico
->hBitMask
);
644 BitBlt(hMemDC
, 0, 0, bih
->biWidth
, bih
->biHeight
, hMemDC2
, 0, 0, SRCINVERT
);
647 ReleaseDC(GetDesktopWindow(), hdc
);
649 #ifdef DEBUG_RESOURCE
650 printf("LoadIcon Alloc hIcon=%X\n", hIcon
);
656 /**********************************************************************
657 * DestroyIcon [USER.457]
659 BOOL
DestroyIcon(HICON hIcon
)
662 if (hIcon
== (HICON
)NULL
) return FALSE
;
663 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
664 if (lpico
->hBitmap
!= (HBITMAP
)NULL
) DeleteObject(lpico
->hBitmap
);
670 /**********************************************************************
671 * LoadAccelerators [USER.177]
673 HANDLE
LoadAccelerators(HANDLE instance
, LPSTR lpTableName
)
678 ACCELHEADER
*lpAccelTbl
;
679 int i
, image_size
, n
;
681 if (((LONG
)lpTableName
& 0xFFFF0000L
) == 0L)
682 printf("LoadAccelerators: instance = %04X, name = %08X\n",
683 instance
, lpTableName
);
685 printf("LoadAccelerators: instance = %04X, name = '%s'\n",
686 instance
, lpTableName
);
688 if (instance
== (HANDLE
)NULL
) instance
= hSysRes
;
689 rsc_mem
= RSC_LoadResource(instance
, lpTableName
, NE_RSCTYPE_ACCELERATOR
,
691 if (rsc_mem
== (HANDLE
)NULL
) {
692 printf("LoadAccelerators / AccelTable %04X not Found !\n", lpTableName
);
695 lp
= (BYTE
*)GlobalLock(rsc_mem
);
701 printf("LoadAccelerators / image_size=%d\n", image_size
);
704 hAccel
= GlobalAlloc(GMEM_MOVEABLE
,
705 sizeof(ACCELHEADER
) + (n
+ 1)*sizeof(ACCELENTRY
));
706 lpAccelTbl
= (LPACCELHEADER
)GlobalLock(hAccel
);
707 lpAccelTbl
->wCount
= 0;
708 for (i
= 0; i
< n
; i
++) {
709 lpAccelTbl
->tbl
[i
].type
= *(lp
++);
710 lpAccelTbl
->tbl
[i
].wEvent
= *((WORD
*)lp
);
712 lpAccelTbl
->tbl
[i
].wIDval
= *((WORD
*)lp
);
714 if (lpAccelTbl
->tbl
[i
].wEvent
== 0) break;
716 printf("Accelerator #%u / event=%04X id=%04X type=%02X \n",
717 i
, lpAccelTbl
->tbl
[i
].wEvent
, lpAccelTbl
->tbl
[i
].wIDval
,
718 lpAccelTbl
->tbl
[i
].type
);
720 lpAccelTbl
->wCount
++;
722 GlobalUnlock(hAccel
);
723 GlobalUnlock(rsc_mem
);
728 /**********************************************************************
729 * TranslateAccelerator [USER.178]
731 int TranslateAccelerator(HWND hWnd
, HANDLE hAccel
, LPMSG msg
)
733 ACCELHEADER
*lpAccelTbl
;
735 if (hAccel
== 0 || msg
== NULL
) return 0;
736 if (msg
->message
!= WM_KEYDOWN
&&
737 msg
->message
!= WM_KEYUP
&&
738 msg
->message
!= WM_CHAR
) return 0;
740 printf("TranslateAccelerators hAccel=%04X !\n", hAccel
);
742 lpAccelTbl
= (LPACCELHEADER
)GlobalLock(hAccel
);
743 for (i
= 0; i
< lpAccelTbl
->wCount
; i
++) {
744 if (lpAccelTbl
->tbl
[i
].type
& VIRTKEY_ACCEL
) {
745 if (msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
&&
746 msg
->message
== WM_KEYDOWN
) {
747 if ((lpAccelTbl
->tbl
[i
].type
& SHIFT_ACCEL
) &&
748 !(GetKeyState(VK_SHIFT
) & 0xf)) {
749 GlobalUnlock(hAccel
);
752 if ((lpAccelTbl
->tbl
[i
].type
& CONTROL_ACCEL
) &&
753 !(GetKeyState(VK_CONTROL
) & 0xf)) {
754 GlobalUnlock(hAccel
);
757 if ((lpAccelTbl
->tbl
[i
].type
& ALT_ACCEL
) &&
758 !(GetKeyState(VK_MENU
) & 0xf)) {
759 GlobalUnlock(hAccel
);
762 SendMessage(hWnd
, WM_COMMAND
, lpAccelTbl
->tbl
[i
].wIDval
, 0x00010000L
);
763 GlobalUnlock(hAccel
);
766 if (msg
->message
== WM_KEYUP
) return 1;
769 if (msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
&&
770 msg
->message
== WM_CHAR
) {
771 SendMessage(hWnd
, WM_COMMAND
, lpAccelTbl
->tbl
[i
].wIDval
, 0x00010000L
);
772 GlobalUnlock(hAccel
);
777 GlobalUnlock(hAccel
);
781 /**********************************************************************
782 * FindResource [KERNEL.60]
784 HANDLE
FindResource(HANDLE instance
, LPSTR resource_name
, LPSTR type_name
)
793 #ifdef DEBUG_RESOURCE
794 printf("FindResource hInst=%04X typename=%08X resname=%08X\n",
795 instance
, type_name
, resource_name
);
797 if (OpenResourceFile(instance
) < 0)
800 rh
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(*r
));
803 r
= (RESOURCE
*)GlobalLock(rh
);
810 if (((int) type_name
& 0xffff0000) == 0)
812 type
= (int) type_name
;
814 else if (type_name
[0] == '\0')
818 else if (type_name
[0] == '#')
820 type
= atoi(type_name
+ 1);
824 type
= (int) type_name
;
826 if (((int) resource_name
& 0xffff0000) == 0)
828 r
->size_shift
= FindResourceByNumber(&r
->nameinfo
, type
,
829 (int) resource_name
| 0x8000);
831 else if (resource_name
[0] == '\0')
833 r
->size_shift
= FindResourceByNumber(&r
->nameinfo
, type
, -1);
835 else if (resource_name
[0] == '#')
837 r
->size_shift
= FindResourceByNumber(&r
->nameinfo
, type
,
838 atoi(resource_name
+ 1));
842 r
->size_shift
= FindResourceByName(&r
->nameinfo
, type
, resource_name
);
845 if (r
->size_shift
== -1)
847 printf("FindResource hInst=%04X typename=%08X resname=%08X not found!\n",
848 instance
, type_name
, resource_name
);
856 /**********************************************************************
857 * AllocResource [KERNEL.66]
859 HANDLE
AllocResource(HANDLE instance
, HANDLE hResInfo
, DWORD dwSize
)
867 if (OpenResourceFile(instance
) < 0)
870 r
= (RESOURCE
*)GlobalLock(hResInfo
);
874 image_size
= r
->nameinfo
.length
<< r
->size_shift
;
877 r
->rsc_mem
= GlobalAlloc(GMEM_MOVEABLE
, image_size
);
879 r
->rsc_mem
= GlobalAlloc(GMEM_MOVEABLE
, dwSize
);
881 GlobalUnlock(hResInfo
);
886 /**********************************************************************
887 * LoadResource [KERNEL.61]
889 HANDLE
LoadResource(HANDLE instance
, HANDLE hResInfo
)
899 if (OpenResourceFile(instance
) < 0)
902 r
= (RESOURCE
*)GlobalLock(hResInfo
);
906 image_size
= r
->nameinfo
.length
<< r
->size_shift
;
908 h
= r
->rsc_mem
= GlobalAlloc(GMEM_MOVEABLE
, image_size
);
909 image
= GlobalLock(h
);
911 lseek(ResourceFd
, ((int) r
->nameinfo
.offset
<< r
->size_shift
), SEEK_SET
);
913 if (image
== NULL
|| read(ResourceFd
, image
, image_size
) != image_size
)
916 GlobalUnlock(hResInfo
);
921 GlobalUnlock(hResInfo
);
925 /**********************************************************************
926 * LockResource [KERNEL.62]
928 LPSTR
LockResource(HANDLE hResData
)
930 return GlobalLock(hResData
);
933 /**********************************************************************
934 * FreeResource [KERNEL.63]
936 HANDLE
FreeResource(HANDLE hResData
)
940 for (r
= rp
= Top
; r
!= NULL
; r
= r
->next
)
942 if (r
->info_mem
== hResData
)
949 GlobalFree(r
->rsc_mem
);
950 GlobalFree(r
->info_mem
);
959 /**********************************************************************
960 * AccessResource [KERNEL.64]
962 int AccessResource(HANDLE instance
, HANDLE hResInfo
)
964 int resfile
, image_size
;
967 /* #ifdef DEBUG_RESOURCE */
968 printf("AccessResource(%04X, %04X);\n", instance
, hResInfo
);
974 if ((resfile
= OpenResourceFile(instance
)) < 0)
977 if ((r
= (RESOURCE
*)GlobalLock(hResInfo
)) == NULL
)
980 lseek(resfile
, ((int) r
->nameinfo
.offset
<< r
->size_shift
), SEEK_SET
);
981 GlobalUnlock(hResInfo
);
986 /**********************************************************************
987 * SizeofResource [KERNEL.65]
989 WORD
SizeofResource(HANDLE instance
, HANDLE hResInfo
)
994 /* #ifdef DEBUG_RESOURCE */
995 printf("SizeofResource(%04X, %04X);\n", instance
, hResInfo
);
1001 if ((r
= (RESOURCE
*)GlobalLock(hResInfo
)) == NULL
)
1004 image_size
= r
->nameinfo
.length
<< r
->size_shift
;
1006 GlobalUnlock(hResInfo
);
1008 /* #ifdef DEBUG_RESOURCE */
1009 printf("SizeofResource return %d\n", image_size
);
1015 /**********************************************************************
1019 RSC_LoadResource(int instance
, char *rsc_name
, int type
, int *image_size_ret
)
1021 struct resource_nameinfo_s nameinfo
;
1028 * Built-in resources
1034 else if (OpenResourceFile(instance
) < 0)
1038 * Get resource by ordinal
1040 if (((int) rsc_name
& 0xffff0000) == 0)
1042 size_shift
= FindResourceByNumber(&nameinfo
, type
,
1043 (int) rsc_name
| 0x8000);
1046 * Get resource by name
1050 size_shift
= FindResourceByName(&nameinfo
, type
, rsc_name
);
1052 if (size_shift
== -1) {
1053 if ((LONG
)rsc_name
>= 0x00010000L
)
1054 printf("RSC_LoadResource inst (%x)/ Resource '%s' not Found !\n", instance
, rsc_name
);
1056 printf("RSC_LoadResource inst (%x)/ Resource '0x%X' not Found !\n", instance
, rsc_name
);
1062 lseek(ResourceFd
, ((int) nameinfo
.offset
<< size_shift
), SEEK_SET
);
1064 image_size
= nameinfo
.length
<< size_shift
;
1065 if (image_size_ret
!= NULL
)
1066 *image_size_ret
= image_size
;
1067 hmem
= GlobalAlloc(GMEM_MOVEABLE
, image_size
);
1068 image
= GlobalLinearLock(hmem
);
1069 if (image
== NULL
|| read(ResourceFd
, image
, image_size
) != image_size
)
1075 GlobalLinearUnlock(hmem
);
1079 /**********************************************************************
1083 LoadString(HANDLE instance
, WORD resource_id
, LPSTR buffer
, int buflen
)
1091 #ifdef DEBUG_RESOURCE
1092 printf("LoadString: instance = %04x, id = %d, "
1093 "buffer = %08x, length = %d\n",
1094 instance
, resource_id
, buffer
, buflen
);
1097 hmem
= RSC_LoadResource(instance
, (char *) ((resource_id
>> 4) + 1),
1098 NE_RSCTYPE_STRING
, &rsc_size
);
1102 p
= GlobalLock(hmem
);
1103 string_num
= resource_id
& 0x000f;
1104 for (i
= 0; i
< string_num
; i
++)
1107 i
= MIN(buflen
- 1, *p
);
1109 memcpy(buffer
, p
+ 1, i
);
1117 printf("LoadString // I dont know why , but caller give buflen=%d *p=%d !\n", buflen
, *p
);
1118 printf("LoadString // and try to obtain string '%s'\n", p
+ 1);
1122 #ifdef DEBUG_RESOURCE
1123 printf("LoadString // '%s' copied !\n", buffer
);
1128 /**********************************************************************
1132 RSC_LoadMenu(HANDLE instance
, LPSTR menu_name
)
1134 #ifdef DEBUG_RESOURCE
1135 printf("RSC_LoadMenu: instance = %04x, name = '%s'\n",
1136 instance
, menu_name
);
1138 return RSC_LoadResource(instance
, menu_name
, NE_RSCTYPE_MENU
, NULL
);
1141 /**********************************************************************
1145 LoadBitmap(HANDLE instance
, LPSTR bmp_name
)
1154 #ifdef DEBUG_RESOURCE
1155 printf("LoadBitmap: instance = %04x, name = %08x\n",
1156 instance
, bmp_name
);
1160 /* Try to create an OEM bitmap */
1161 hbitmap
= BITMAP_LoadOEMBitmap( ((int)bmp_name
) & 0xffff );
1162 if (hbitmap
) return hbitmap
;
1163 /* Failed -> load it from sysres.dll */
1167 rsc_mem
= RSC_LoadResource(instance
, bmp_name
, NE_RSCTYPE_BITMAP
,
1169 if (rsc_mem
== (HANDLE
)NULL
) {
1170 printf("LoadBitmap / BitMap %04X not Found !\n", bmp_name
);
1173 lp
= (long *) GlobalLinearLock(rsc_mem
);
1174 if (!(hdc
= GetDC(0))) lp
= NULL
;
1177 GlobalFree(rsc_mem
);
1180 size
= CONV_LONG (*lp
);
1181 if (size
== sizeof(BITMAPCOREHEADER
)){
1182 CONV_BITMAPCOREHEADER (lp
);
1183 hbitmap
= ConvertCoreBitmap( hdc
, (BITMAPCOREHEADER
*) lp
);
1184 } else if (size
== sizeof(BITMAPINFOHEADER
)){
1185 CONV_BITMAPINFO (lp
);
1186 hbitmap
= ConvertInfoBitmap( hdc
, (BITMAPINFO
*) lp
);
1188 GlobalFree(rsc_mem
);
1189 ReleaseDC( 0, hdc
);
1193 /**********************************************************************
1194 * CreateIcon [USER.407]
1196 HICON
CreateIcon(HANDLE hInstance
, int nWidth
, int nHeight
,
1197 BYTE nPlanes
, BYTE nBitsPixel
, LPSTR lpANDbits
,
1203 #ifdef DEBUG_RESOURCE
1204 printf("CreateIcon: hInstance = %04x, nWidth = %08x, nHeight = %08x \n",
1205 hInstance
, nWidth
, nHeight
);
1206 printf(" nPlanes = %04x, nBitsPixel = %04x,",nPlanes
, nBitsPixel
);
1207 printf(" lpANDbits= %04x, lpXORbits = %04x, \n",lpANDbits
, lpXORbits
);
1210 if (hInstance
== (HANDLE
)NULL
) {
1211 printf("CreateIcon / hInstance %04x not Found!\n",hInstance
);
1214 hIcon
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(ICONALLOC
) + 1024);
1215 if (hIcon
== (HICON
)NULL
) {
1216 printf("Can't allocate memory for Icon in CreateIcon\n");
1219 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
1221 lpico
->descriptor
.Width
=nWidth
;
1222 lpico
->descriptor
.Height
=nHeight
;
1223 lpico
->descriptor
.ColorCount
=16; /* Dummy Value */
1224 lpico
->descriptor
.Reserved1
=0;
1225 lpico
->descriptor
.Reserved2
=nPlanes
;
1226 lpico
->descriptor
.Reserved3
=nWidth
*nHeight
;
1228 /* either nPlanes and/or nBitCount is set to one */
1229 lpico
->descriptor
.icoDIBSize
=nWidth
*nHeight
*nPlanes
*nBitsPixel
;
1230 lpico
->descriptor
.icoDIBOffset
=0;
1232 if( !(lpico
->hBitmap
=CreateBitmap(nWidth
, nHeight
, nPlanes
, nBitsPixel
,
1234 printf("CreateIcon: couldn't create the XOR bitmap\n");
1238 /* the AND BitMask is always monochrome */
1239 if( !(lpico
->hBitMask
=CreateBitmap(nWidth
, nHeight
, 1, 1, lpANDbits
)) ) {
1240 printf("CreateIcon: couldn't create the AND bitmap\n");
1244 GlobalUnlock(hIcon
);
1245 #ifdef DEBUG_RESOURCE
1246 printf("CreateIcon Alloc hIcon=%X\n", hIcon
);