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 HANDLE
RSC_LoadResource(int instance
, char *rsc_name
, int type
,
42 void RSC_LoadNameTable(void);
44 extern char *ProgramName
;
47 /**********************************************************************
54 struct resource_typeinfo_s typeinfo
;
55 struct resource_nameinfo_s nameinfo
;
56 unsigned short size_shift
;
68 * Move to beginning of resource table.
70 rtoff
= (ResourceFileInfo
->mz_header
->ne_offset
+
71 ResourceFileInfo
->ne_header
->resource_tab_offset
);
72 lseek(ResourceFd
, rtoff
, SEEK_SET
);
77 if (read(ResourceFd
, &size_shift
, sizeof(size_shift
)) !=
82 size_shift
= CONV_SHORT(size_shift
);
87 typeinfo
.type_id
= 0xffff;
88 while (typeinfo
.type_id
!= 0)
90 if (!load_typeinfo (ResourceFd
, &typeinfo
))
93 if (typeinfo
.type_id
== 0)
95 if (typeinfo
.type_id
== 0x800f)
97 for (i
= 0; i
< typeinfo
.count
; i
++)
99 if (read(ResourceFd
, &nameinfo
, sizeof(nameinfo
)) !=
105 saved_pos
= lseek(ResourceFd
, 0, SEEK_CUR
);
106 lseek(ResourceFd
, (long) nameinfo
.offset
<< size_shift
,
108 read(ResourceFd
, &len
, sizeof(len
));
111 new = (RESNAMTAB
*) GlobalQuickAlloc(sizeof(*new));
115 read(ResourceFd
, &new->type_ord
, 2);
116 read(ResourceFd
, &new->id_ord
, 2);
117 read(ResourceFd
, read_buf
, len
- 6);
119 p
= read_buf
+ strlen(read_buf
) + 1;
120 strncpy(new->id
, p
, MAX_NAME_LENGTH
);
121 new->id
[MAX_NAME_LENGTH
- 1] = '\0';
123 read(ResourceFd
, &len
, sizeof(len
));
126 lseek(ResourceFd
, saved_pos
, SEEK_SET
);
132 lseek(ResourceFd
, (typeinfo
.count
* sizeof(nameinfo
)), SEEK_CUR
);
136 ResourceFileInfo
->resnamtab
= top
;
140 /**********************************************************************
144 OpenResourceFile(HANDLE instance
)
149 if (ResourceInst
== instance
)
152 w
= GetFileInfo(instance
);
155 ResourceFileInfo
= w
;
156 res_file
= w
->filename
;
161 ResourceInst
= instance
;
162 ResourceFd
= open (res_file
, O_RDONLY
);
165 if (w
->resnamtab
== (RESNAMTAB
*) -1)
172 #ifdef DEBUG_RESOURCE
173 printf("OpenResourceFile(%04X) // file='%s' hFile=%04X !\n",
174 instance
, w
->filename
, ResourceFd
);
179 /**********************************************************************
183 ConvertCoreBitmap( HDC hdc
, BITMAPCOREHEADER
* image
)
185 BITMAPINFO
* bmpInfo
;
188 int i
, size
, n_colors
;
190 n_colors
= 1 << image
->bcBitCount
;
192 if (image
->bcBitCount
< 24)
194 size
= sizeof(BITMAPINFOHEADER
) + n_colors
* sizeof(RGBQUAD
);
195 bits
= (char *) (image
+ 1) + (n_colors
* sizeof(RGBTRIPLE
));
199 size
= sizeof(BITMAPINFOHEADER
);
200 bits
= (char *) (image
+ 1);
202 bmpInfo
= (BITMAPINFO
*) malloc( size
);
204 bmpInfo
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
205 bmpInfo
->bmiHeader
.biWidth
= image
->bcWidth
;
206 bmpInfo
->bmiHeader
.biHeight
= image
->bcHeight
;
207 bmpInfo
->bmiHeader
.biPlanes
= image
->bcPlanes
;
208 bmpInfo
->bmiHeader
.biBitCount
= image
->bcBitCount
;
209 bmpInfo
->bmiHeader
.biCompression
= 0;
210 bmpInfo
->bmiHeader
.biSizeImage
= 0;
211 bmpInfo
->bmiHeader
.biXPelsPerMeter
= 0;
212 bmpInfo
->bmiHeader
.biYPelsPerMeter
= 0;
213 bmpInfo
->bmiHeader
.biClrUsed
= 0;
214 bmpInfo
->bmiHeader
.biClrImportant
= 0;
216 if (image
->bcBitCount
< 24)
218 RGBTRIPLE
* oldMap
= (RGBTRIPLE
*)(image
+ 1);
219 RGBQUAD
* newMap
= bmpInfo
->bmiColors
;
220 for (i
= 0; i
< n_colors
; i
++, oldMap
++, newMap
++)
222 newMap
->rgbRed
= oldMap
->rgbtRed
;
223 newMap
->rgbGreen
= oldMap
->rgbtGreen
;
224 newMap
->rgbBlue
= oldMap
->rgbtBlue
;
225 newMap
->rgbReserved
= 0;
229 hbitmap
= CreateDIBitmap( hdc
, &bmpInfo
->bmiHeader
, CBM_INIT
,
230 bits
, bmpInfo
, DIB_RGB_COLORS
);
235 /**********************************************************************
239 ConvertInfoBitmap( HDC hdc
, BITMAPINFO
* image
)
241 char * bits
= ((char *)image
) + DIB_BitmapInfoSize(image
, DIB_RGB_COLORS
);
242 return CreateDIBitmap( hdc
, &image
->bmiHeader
, CBM_INIT
,
243 bits
, image
, DIB_RGB_COLORS
);
247 load_typeinfo (int fd
, struct resource_typeinfo_s
*typeinfo
)
249 return read (fd
, typeinfo
, sizeof (*typeinfo
)) == sizeof (*typeinfo
);
254 type_match(int type_id1
, int type_id2
, int fd
, off_t off
)
263 if ((type_id1
& 0xffff0000) == 0) {
264 if ((type_id2
& 0x8000) == 0)
266 return (type_id1
& 0x000f) == (type_id2
& 0x000f);
268 if ((type_id2
& 0x8000) != 0)
270 #ifdef DEBUG_RESOURCE
271 printf("type_compare: type_id2=%04X !\n", type_id2
);
273 old_pos
= lseek(fd
, 0, SEEK_CUR
);
274 lseek(fd
, off
+ type_id2
, SEEK_SET
);
276 nbytes
= CONV_CHAR_TO_LONG (c
);
277 #ifdef DEBUG_RESOURCE
278 printf("type_compare: namesize=%d\n", nbytes
);
280 read(fd
, name
, nbytes
);
281 lseek(fd
, old_pos
, SEEK_SET
);
283 #ifdef DEBUG_RESOURCE
284 printf("type_compare: name=`%s'\n", name
);
286 return strcasecmp((char *) type_id1
, name
) == 0;
289 /**********************************************************************
290 * FindResourceByNumber
293 FindResourceByNumber(struct resource_nameinfo_s
*result_p
,
294 int type_id
, int resource_id
)
296 struct resource_typeinfo_s typeinfo
;
297 struct resource_nameinfo_s nameinfo
;
298 unsigned short size_shift
;
303 * Move to beginning of resource table.
305 rtoff
= (ResourceFileInfo
->mz_header
->ne_offset
+
306 ResourceFileInfo
->ne_header
->resource_tab_offset
);
307 lseek(ResourceFd
, rtoff
, SEEK_SET
);
312 if (read(ResourceFd
, &size_shift
, sizeof(size_shift
)) !=
315 printf("FindResourceByNumber (%s) bad block size !\n", resource_id
);
318 size_shift
= CONV_SHORT(size_shift
);
323 if (!load_typeinfo (ResourceFd
, &typeinfo
)){
324 printf("FindResourceByNumber (%X) bad typeinfo size !\n", resource_id
);
327 #ifdef DEBUG_RESOURCE
328 printf("FindResourceByNumber type=%X count=%d searched=%d \n",
329 typeinfo
.type_id
, typeinfo
.count
, type_id
);
331 if (typeinfo
.type_id
== 0) break;
332 if (type_match(type_id
, typeinfo
.type_id
, ResourceFd
, rtoff
)) {
334 for (i
= 0; i
< typeinfo
.count
; i
++) {
336 if (read(ResourceFd
, &nameinfo
, sizeof(nameinfo
)) !=
339 if (!load_nameinfo (ResourceFd
, &nameinfo
))
342 printf("FindResourceByNumber (%X) bad nameinfo size !\n", resource_id
);
345 #ifdef DEBUG_RESOURCE
346 printf("FindResource: search type=%X id=%X // type=%X id=%X\n",
347 type_id
, resource_id
, typeinfo
.type_id
, nameinfo
.id
);
349 if (nameinfo
.id
== resource_id
) {
350 memcpy(result_p
, &nameinfo
, sizeof(nameinfo
));
356 lseek(ResourceFd
, (typeinfo
.count
* sizeof(nameinfo
)), SEEK_CUR
);
362 /**********************************************************************
366 FindResourceByName(struct resource_nameinfo_s
*result_p
,
367 int type_id
, char *resource_name
)
369 struct resource_typeinfo_s typeinfo
;
370 struct resource_nameinfo_s nameinfo
;
371 unsigned short size_shift
;
372 off_t old_pos
, new_pos
;
373 unsigned char nbytes
;
379 * Check for loaded name table.
381 if (ResourceFileInfo
->resnamtab
!= NULL
)
385 for (e
= ResourceFileInfo
->resnamtab
; e
!= NULL
; e
= e
->next
)
387 if (e
->type_ord
== (type_id
& 0x000f) &&
388 strcasecmp(e
->id
, resource_name
) == 0)
390 return FindResourceByNumber(result_p
, type_id
, e
->id_ord
);
398 * Move to beginning of resource table.
400 rtoff
= (ResourceFileInfo
->mz_header
->ne_offset
+
401 ResourceFileInfo
->ne_header
->resource_tab_offset
);
402 lseek(ResourceFd
, rtoff
, SEEK_SET
);
407 if (read(ResourceFd
, &size_shift
, sizeof(size_shift
)) !=
410 printf("FindResourceByName (%s) bad block size !\n", resource_name
);
413 size_shift
= CONV_SHORT (size_shift
);
420 if (!load_typeinfo (ResourceFd
, &typeinfo
))
422 printf("FindResourceByName (%s) bad typeinfo size !\n", resource_name
);
425 #ifdef DEBUG_RESOURCE
426 printf("FindResourceByName typeinfo.type_id=%X count=%d type_id=%X\n",
427 typeinfo
.type_id
, typeinfo
.count
, type_id
);
429 if (typeinfo
.type_id
== 0) break;
430 if (type_match(type_id
, typeinfo
.type_id
, ResourceFd
, rtoff
))
432 for (i
= 0; i
< typeinfo
.count
; i
++)
435 if (read(ResourceFd
, &nameinfo
, sizeof(nameinfo
)) !=
438 if (!load_nameinfo (ResourceFd
, &nameinfo
))
441 printf("FindResourceByName (%s) bad nameinfo size !\n", resource_name
);
445 if ((nameinfo.id & 0x8000) != 0) continue;
447 #ifdef DEBUG_RESOURCE
448 printf("FindResourceByName // nameinfo.id=%04X !\n", nameinfo
.id
);
450 old_pos
= lseek(ResourceFd
, 0, SEEK_CUR
);
451 new_pos
= rtoff
+ nameinfo
.id
;
452 lseek(ResourceFd
, new_pos
, SEEK_SET
);
453 read(ResourceFd
, &nbytes
, 1);
454 #ifdef DEBUG_RESOURCE
455 printf("FindResourceByName // namesize=%d !\n", nbytes
);
457 nbytes
= CONV_CHAR_TO_LONG (nbytes
);
458 read(ResourceFd
, name
, nbytes
);
459 lseek(ResourceFd
, old_pos
, SEEK_SET
);
461 #ifdef DEBUG_RESOURCE
462 printf("FindResourceByName type_id=%X (%d of %d) name='%s' resource_name='%s'\n",
463 typeinfo
.type_id
, i
+ 1, typeinfo
.count
,
464 name
, resource_name
);
466 if (strcasecmp(name
, resource_name
) == 0)
468 memcpy(result_p
, &nameinfo
, sizeof(nameinfo
));
474 lseek(ResourceFd
, (typeinfo
.count
* sizeof(nameinfo
)), SEEK_CUR
);
481 /**********************************************************************
482 * GetRsrcCount [internal]
484 int GetRsrcCount(HINSTANCE hInst
, int type_id
)
486 struct resource_typeinfo_s typeinfo
;
487 struct resource_nameinfo_s nameinfo
;
488 unsigned short size_shift
;
489 /* off_t old_pos, new_pos;
490 unsigned char nbytes;
494 if (hInst
== 0) return 0;
495 #ifdef DEBUG_RESOURCE
496 printf("GetRsrcCount hInst=%04X typename=%08X\n", hInst
, type_name
);
498 if (OpenResourceFile(hInst
) < 0) return 0;
501 * Move to beginning of resource table.
503 rtoff
= (ResourceFileInfo
->mz_header
->ne_offset
+
504 ResourceFileInfo
->ne_header
->resource_tab_offset
);
505 lseek(ResourceFd
, rtoff
, SEEK_SET
);
509 if (read(ResourceFd
, &size_shift
, sizeof(size_shift
)) != sizeof(size_shift
)) {
510 printf("GetRsrcCount // bad block size !\n");
513 size_shift
= CONV_SHORT (size_shift
);
515 if (!load_typeinfo (ResourceFd
, &typeinfo
)) {
516 printf("GetRsrcCount // bad typeinfo size !\n");
519 #ifdef DEBUG_RESOURCE
520 printf("GetRsrcCount // typeinfo.type_id=%X count=%d type_id=%X\n",
521 typeinfo
.type_id
, typeinfo
.count
, type_id
);
523 if (typeinfo
.type_id
== 0) break;
524 if (type_match(type_id
, typeinfo
.type_id
, ResourceFd
, rtoff
)) {
525 return typeinfo
.count
;
528 lseek(ResourceFd
, (typeinfo
.count
* sizeof(nameinfo
)), SEEK_CUR
);
535 /**********************************************************************
536 * LoadIcon [USER.174]
538 HICON
LoadIcon(HANDLE instance
, LPSTR icon_name
)
543 ICONDESCRIP
*lpicodesc
;
547 BITMAPINFOHEADER
*bih
;
553 int i
, j
, image_size
;
554 #ifdef DEBUG_RESOURCE
555 printf("LoadIcon: instance = %04x, name = %08x\n",
556 instance
, icon_name
);
559 if (!(hdc
= GetDC(GetDesktopWindow()))) return 0;
560 if (instance
== (HANDLE
)NULL
) instance
= hSysRes
;
561 rsc_mem
= RSC_LoadResource(instance
, icon_name
, NE_RSCTYPE_GROUP_ICON
,
563 if (rsc_mem
== (HANDLE
)NULL
) {
564 printf("LoadIcon / Icon %04X not Found !\n", icon_name
);
565 ReleaseDC(GetDesktopWindow(), hdc
);
568 lp
= (WORD
*)GlobalLock(rsc_mem
);
571 ReleaseDC(GetDesktopWindow(), hdc
);
574 lpicodesc
= (ICONDESCRIP
*)(lp
+ 3);
575 hIcon
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(ICONALLOC
) + 1024);
576 if (hIcon
== (HICON
)NULL
) {
578 ReleaseDC(GetDesktopWindow(), hdc
);
581 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
582 lpico
->descriptor
= *lpicodesc
;
583 width
= lpicodesc
->Width
;
584 height
= lpicodesc
->Height
;
585 GlobalUnlock(rsc_mem
);
587 rsc_mem
= RSC_LoadResource(instance
,
588 MAKEINTRESOURCE(lpicodesc
->icoDIBOffset
),
589 NE_RSCTYPE_ICON
, &image_size
);
590 if (rsc_mem
== (HANDLE
)NULL
) {
591 printf("LoadIcon / Icon %04X Bitmaps not Found !\n", icon_name
);
592 ReleaseDC(GetDesktopWindow(), hdc
);
595 lp
= (WORD
*)GlobalLock(rsc_mem
);
598 ReleaseDC(GetDesktopWindow(), hdc
);
601 bmi
= (BITMAPINFO
*)lp
;
602 bih
= (BITMAPINFOHEADER
*)lp
;
603 rgbq
= &bmi
->bmiColors
[0];
604 bih
->biHeight
= bih
->biHeight
/ 2;
606 printf("LoadIcon / image_size=%d width=%d height=%d bih->biBitCount=%d bih->biSizeImage=%ld\n",
607 image_size, width, height, bih->biBitCount, bih->biSizeImage);
609 if (bih
->biSize
== sizeof(BITMAPINFOHEADER
))
610 lpico
->hBitmap
= ConvertInfoBitmap(hdc
, (BITMAPINFO
*)bih
);
614 bih
->biClrUsed
= bih
->biClrImportant
= 2;
615 rgbq
[0].rgbBlue
= 0xFF;
616 rgbq
[0].rgbGreen
= 0xFF;
617 rgbq
[0].rgbRed
= 0xFF;
618 rgbq
[0].rgbReserved
= 0x00;
619 rgbq
[1].rgbBlue
= 0x00;
620 rgbq
[1].rgbGreen
= 0x00;
621 rgbq
[1].rgbRed
= 0x00;
622 rgbq
[1].rgbReserved
= 0x00;
623 if (bih
->biSizeImage
== 0) {
624 if (bih
->biCompression
!= BI_RGB
) {
625 printf("Unknown size for compressed Icon bitmap.\n");
627 ReleaseDC(GetDesktopWindow(), hdc
);
630 bih
->biSizeImage
= (bih
->biWidth
* bih
->biHeight
* bih
->biBitCount
633 lpico
->hBitMask
= CreateDIBitmap(hdc
, bih
, CBM_INIT
,
634 (LPSTR
)lp
+ bih
->biSizeImage
- sizeof(BITMAPINFOHEADER
) / 2 - 4,
635 (BITMAPINFO
*)bih
, DIB_RGB_COLORS
);
636 GlobalUnlock(rsc_mem
);
638 hMemDC
= CreateCompatibleDC(hdc
);
639 hMemDC2
= CreateCompatibleDC(hdc
);
640 SelectObject(hMemDC
, lpico
->hBitmap
);
641 SelectObject(hMemDC2
, lpico
->hBitMask
);
642 BitBlt(hMemDC
, 0, 0, bih
->biWidth
, bih
->biHeight
, hMemDC2
, 0, 0, SRCINVERT
);
645 ReleaseDC(GetDesktopWindow(), hdc
);
647 #ifdef DEBUG_RESOURCE
648 printf("LoadIcon Alloc hIcon=%X\n", hIcon
);
654 /**********************************************************************
655 * DestroyIcon [USER.457]
657 BOOL
DestroyIcon(HICON hIcon
)
660 if (hIcon
== (HICON
)NULL
) return FALSE
;
661 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
662 if (lpico
->hBitmap
!= (HBITMAP
)NULL
) DeleteObject(lpico
->hBitmap
);
668 /**********************************************************************
669 * LoadAccelerators [USER.177]
671 HANDLE
LoadAccelerators(HANDLE instance
, LPSTR lpTableName
)
676 ACCELHEADER
*lpAccelTbl
;
677 int i
, image_size
, n
;
679 if (((LONG
)lpTableName
& 0xFFFF0000L
) == 0L)
680 printf("LoadAccelerators: instance = %04X, name = %08X\n",
681 instance
, lpTableName
);
683 printf("LoadAccelerators: instance = %04X, name = '%s'\n",
684 instance
, lpTableName
);
686 if (instance
== (HANDLE
)NULL
) instance
= hSysRes
;
687 rsc_mem
= RSC_LoadResource(instance
, lpTableName
, NE_RSCTYPE_ACCELERATOR
,
689 if (rsc_mem
== (HANDLE
)NULL
) {
690 printf("LoadAccelerators / AccelTable %04X not Found !\n", lpTableName
);
693 lp
= (BYTE
*)GlobalLock(rsc_mem
);
699 printf("LoadAccelerators / image_size=%d\n", image_size
);
702 hAccel
= GlobalAlloc(GMEM_MOVEABLE
,
703 sizeof(ACCELHEADER
) + (n
+ 1)*sizeof(ACCELENTRY
));
704 lpAccelTbl
= (LPACCELHEADER
)GlobalLock(hAccel
);
705 lpAccelTbl
->wCount
= 0;
706 for (i
= 0; i
< n
; i
++) {
707 lpAccelTbl
->tbl
[i
].type
= *(lp
++);
708 lpAccelTbl
->tbl
[i
].wEvent
= *((WORD
*)lp
);
710 lpAccelTbl
->tbl
[i
].wIDval
= *((WORD
*)lp
);
712 if (lpAccelTbl
->tbl
[i
].wEvent
== 0) break;
714 printf("Accelerator #%u / event=%04X id=%04X type=%02X \n",
715 i
, lpAccelTbl
->tbl
[i
].wEvent
, lpAccelTbl
->tbl
[i
].wIDval
,
716 lpAccelTbl
->tbl
[i
].type
);
718 lpAccelTbl
->wCount
++;
720 GlobalUnlock(hAccel
);
721 GlobalUnlock(rsc_mem
);
726 /**********************************************************************
727 * TranslateAccelerator [USER.178]
729 int TranslateAccelerator(HWND hWnd
, HANDLE hAccel
, LPMSG msg
)
731 ACCELHEADER
*lpAccelTbl
;
733 if (hAccel
== 0 || msg
== NULL
) return 0;
734 if (msg
->message
!= WM_KEYDOWN
&&
735 msg
->message
!= WM_KEYUP
&&
736 msg
->message
!= WM_CHAR
) return 0;
738 printf("TranslateAccelerators hAccel=%04X !\n", hAccel
);
740 lpAccelTbl
= (LPACCELHEADER
)GlobalLock(hAccel
);
741 for (i
= 0; i
< lpAccelTbl
->wCount
; i
++) {
742 if (lpAccelTbl
->tbl
[i
].type
& VIRTKEY_ACCEL
) {
743 if (msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
&&
744 msg
->message
== WM_KEYDOWN
) {
745 if ((lpAccelTbl
->tbl
[i
].type
& SHIFT_ACCEL
) &&
746 !(GetKeyState(VK_SHIFT
) & 0xf)) {
747 GlobalUnlock(hAccel
);
750 if ((lpAccelTbl
->tbl
[i
].type
& CONTROL_ACCEL
) &&
751 !(GetKeyState(VK_CONTROL
) & 0xf)) {
752 GlobalUnlock(hAccel
);
755 if ((lpAccelTbl
->tbl
[i
].type
& ALT_ACCEL
) &&
756 !(GetKeyState(VK_MENU
) & 0xf)) {
757 GlobalUnlock(hAccel
);
760 SendMessage(hWnd
, WM_COMMAND
, lpAccelTbl
->tbl
[i
].wIDval
, 0x00010000L
);
761 GlobalUnlock(hAccel
);
764 if (msg
->message
== WM_KEYUP
) return 1;
767 if (msg
->wParam
== lpAccelTbl
->tbl
[i
].wEvent
&&
768 msg
->message
== WM_CHAR
) {
769 SendMessage(hWnd
, WM_COMMAND
, lpAccelTbl
->tbl
[i
].wIDval
, 0x00010000L
);
770 GlobalUnlock(hAccel
);
775 GlobalUnlock(hAccel
);
779 /**********************************************************************
780 * FindResource [KERNEL.60]
782 HANDLE
FindResource(HANDLE instance
, LPSTR resource_name
, LPSTR type_name
)
791 #ifdef DEBUG_RESOURCE
792 printf("FindResource hInst=%04X typename=%08X resname=%08X\n",
793 instance
, type_name
, resource_name
);
795 if (OpenResourceFile(instance
) < 0)
798 rh
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(*r
));
801 r
= (RESOURCE
*)GlobalLock(rh
);
808 if (((int) type_name
& 0xffff0000) == 0)
810 type
= (int) type_name
;
812 else if (type_name
[0] == '\0')
816 else if (type_name
[0] == '#')
818 type
= atoi(type_name
+ 1);
822 type
= (int) type_name
;
824 if (((int) resource_name
& 0xffff0000) == 0)
826 r
->size_shift
= FindResourceByNumber(&r
->nameinfo
, type
,
827 (int) resource_name
| 0x8000);
829 else if (resource_name
[0] == '\0')
831 r
->size_shift
= FindResourceByNumber(&r
->nameinfo
, type
, -1);
833 else if (resource_name
[0] == '#')
835 r
->size_shift
= FindResourceByNumber(&r
->nameinfo
, type
,
836 atoi(resource_name
+ 1));
840 r
->size_shift
= FindResourceByName(&r
->nameinfo
, type
, resource_name
);
843 if (r
->size_shift
== -1)
845 printf("FindResource hInst=%04X typename=%08X resname=%08X not found!\n",
846 instance
, type_name
, resource_name
);
854 /**********************************************************************
855 * AllocResource [KERNEL.66]
857 HANDLE
AllocResource(HANDLE instance
, HANDLE hResInfo
, DWORD dwSize
)
865 if (OpenResourceFile(instance
) < 0)
868 r
= (RESOURCE
*)GlobalLock(hResInfo
);
872 image_size
= r
->nameinfo
.length
<< r
->size_shift
;
875 r
->rsc_mem
= GlobalAlloc(GMEM_MOVEABLE
, image_size
);
877 r
->rsc_mem
= GlobalAlloc(GMEM_MOVEABLE
, dwSize
);
879 GlobalUnlock(hResInfo
);
884 /**********************************************************************
885 * LoadResource [KERNEL.61]
887 HANDLE
LoadResource(HANDLE instance
, HANDLE hResInfo
)
897 if (OpenResourceFile(instance
) < 0)
900 r
= (RESOURCE
*)GlobalLock(hResInfo
);
904 image_size
= r
->nameinfo
.length
<< r
->size_shift
;
906 h
= r
->rsc_mem
= GlobalAlloc(GMEM_MOVEABLE
, image_size
);
907 image
= GlobalLock(h
);
909 lseek(ResourceFd
, ((int) r
->nameinfo
.offset
<< r
->size_shift
), SEEK_SET
);
911 if (image
== NULL
|| read(ResourceFd
, image
, image_size
) != image_size
)
914 GlobalUnlock(hResInfo
);
919 GlobalUnlock(hResInfo
);
923 /**********************************************************************
924 * LockResource [KERNEL.62]
926 LPSTR
LockResource(HANDLE hResData
)
928 return GlobalLock(hResData
);
931 /**********************************************************************
932 * FreeResource [KERNEL.63]
934 HANDLE
FreeResource(HANDLE hResData
)
938 for (r
= rp
= Top
; r
!= NULL
; r
= r
->next
)
940 if (r
->info_mem
== hResData
)
947 GlobalFree(r
->rsc_mem
);
948 GlobalFree(r
->info_mem
);
957 /**********************************************************************
958 * AccessResource [KERNEL.64]
960 int AccessResource(HANDLE instance
, HANDLE hResInfo
)
962 int resfile
, image_size
;
965 /* #ifdef DEBUG_RESOURCE */
966 printf("AccessResource(%04X, %04X);\n", instance
, hResInfo
);
972 if ((resfile
= OpenResourceFile(instance
)) < 0)
975 if ((r
= (RESOURCE
*)GlobalLock(hResInfo
)) == NULL
)
978 lseek(resfile
, ((int) r
->nameinfo
.offset
<< r
->size_shift
), SEEK_SET
);
979 GlobalUnlock(hResInfo
);
984 /**********************************************************************
985 * SizeofResource [KERNEL.65]
987 WORD
SizeofResource(HANDLE instance
, HANDLE hResInfo
)
992 /* #ifdef DEBUG_RESOURCE */
993 printf("SizeofResource(%04X, %04X);\n", instance
, hResInfo
);
999 if ((r
= (RESOURCE
*)GlobalLock(hResInfo
)) == NULL
)
1002 image_size
= r
->nameinfo
.length
<< r
->size_shift
;
1004 GlobalUnlock(hResInfo
);
1006 /* #ifdef DEBUG_RESOURCE */
1007 printf("SizeofResource return %d\n", image_size
);
1013 /**********************************************************************
1017 RSC_LoadResource(int instance
, char *rsc_name
, int type
, int *image_size_ret
)
1019 struct resource_nameinfo_s nameinfo
;
1026 * Built-in resources
1032 else if (OpenResourceFile(instance
) < 0)
1036 * Get resource by ordinal
1038 if (((int) rsc_name
& 0xffff0000) == 0)
1040 size_shift
= FindResourceByNumber(&nameinfo
, type
,
1041 (int) rsc_name
| 0x8000);
1044 * Get resource by name
1048 size_shift
= FindResourceByName(&nameinfo
, type
, rsc_name
);
1050 if (size_shift
== -1) {
1051 if ((LONG
)rsc_name
>= 0x00010000L
)
1052 printf("RSC_LoadResource / Resource '%s' not Found !\n", rsc_name
);
1054 printf("RSC_LoadResource / Resource '%X' not Found !\n", rsc_name
);
1060 lseek(ResourceFd
, ((int) nameinfo
.offset
<< size_shift
), SEEK_SET
);
1062 image_size
= nameinfo
.length
<< size_shift
;
1063 if (image_size_ret
!= NULL
)
1064 *image_size_ret
= image_size
;
1065 hmem
= GlobalAlloc(GMEM_MOVEABLE
, image_size
);
1066 image
= GlobalLinearLock(hmem
);
1067 if (image
== NULL
|| read(ResourceFd
, image
, image_size
) != image_size
)
1073 GlobalLinearUnlock(hmem
);
1077 /**********************************************************************
1081 LoadString(HANDLE instance
, WORD resource_id
, LPSTR buffer
, int buflen
)
1089 #ifdef DEBUG_RESOURCE
1090 printf("LoadString: instance = %04x, id = %d, "
1091 "buffer = %08x, length = %d\n",
1092 instance
, resource_id
, buffer
, buflen
);
1095 hmem
= RSC_LoadResource(instance
, (char *) ((resource_id
>> 4) + 1),
1096 NE_RSCTYPE_STRING
, &rsc_size
);
1100 p
= GlobalLock(hmem
);
1101 string_num
= resource_id
& 0x000f;
1102 for (i
= 0; i
< string_num
; i
++)
1105 i
= MIN(buflen
- 1, *p
);
1107 memcpy(buffer
, p
+ 1, i
);
1115 printf("LoadString // I dont know why , but caller give buflen=%d *p=%d !\n", buflen
, *p
);
1116 printf("LoadString // and try to obtain string '%s'\n", p
+ 1);
1120 #ifdef DEBUG_RESOURCE
1121 printf("LoadString // '%s' copied !\n", buffer
);
1126 /**********************************************************************
1130 RSC_LoadMenu(HANDLE instance
, LPSTR menu_name
)
1132 #ifdef DEBUG_RESOURCE
1133 printf("RSC_LoadMenu: instance = %04x, name = '%s'\n",
1134 instance
, menu_name
);
1136 return RSC_LoadResource(instance
, menu_name
, NE_RSCTYPE_MENU
, NULL
);
1139 /**********************************************************************
1143 LoadBitmap(HANDLE instance
, LPSTR bmp_name
)
1152 #ifdef DEBUG_RESOURCE
1153 printf("LoadBitmap: instance = %04x, name = %08x\n",
1154 instance
, bmp_name
);
1156 if (instance
== (HANDLE
)NULL
) instance
= hSysRes
;
1157 if (!(hdc
= GetDC(GetDesktopWindow()))) return 0;
1159 rsc_mem
= RSC_LoadResource(instance
, bmp_name
, NE_RSCTYPE_BITMAP
,
1161 if (rsc_mem
== (HANDLE
)NULL
) {
1162 printf("LoadBitmap / BitMap %04X not Found !\n", bmp_name
);
1165 lp
= (long *) GlobalLinearLock(rsc_mem
);
1168 GlobalFree(rsc_mem
);
1171 size
= CONV_LONG (*lp
);
1172 if (size
== sizeof(BITMAPCOREHEADER
)){
1173 CONV_BITMAPCOREHEADER (lp
);
1174 hbitmap
= ConvertCoreBitmap( hdc
, (BITMAPCOREHEADER
*) lp
);
1175 } else if (size
== sizeof(BITMAPINFOHEADER
)){
1176 CONV_BITMAPINFO (lp
);
1177 hbitmap
= ConvertInfoBitmap( hdc
, (BITMAPINFO
*) lp
);
1179 GlobalFree(rsc_mem
);
1180 ReleaseDC( 0, hdc
);
1184 /**********************************************************************
1185 * CreateIcon [USER.407]
1187 HICON
CreateIcon(HANDLE hInstance
, int nWidth
, int nHeight
,
1188 BYTE nPlanes
, BYTE nBitsPixel
, LPSTR lpANDbits
,
1194 #ifdef DEBUG_RESOURCE
1195 printf("CreateIcon: hInstance = %04x, nWidth = %08x, nHeight = %08x \n",
1196 hInstance
, nWidth
, nHeight
);
1197 printf(" nPlanes = %04x, nBitsPixel = %04x,",nPlanes
, nBitsPixel
);
1198 printf(" lpANDbits= %04x, lpXORbits = %04x, \n",lpANDbits
, lpXORbits
);
1201 if (hInstance
== (HANDLE
)NULL
) {
1202 printf("CreateIcon / hInstance %04x not Found!\n",hInstance
);
1205 hIcon
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(ICONALLOC
) + 1024);
1206 if (hIcon
== (HICON
)NULL
) {
1207 printf("Can't allocate memory for Icon in CreateIcon\n");
1210 lpico
= (ICONALLOC
*)GlobalLock(hIcon
);
1212 lpico
->descriptor
.Width
=nWidth
;
1213 lpico
->descriptor
.Height
=nHeight
;
1214 lpico
->descriptor
.ColorCount
=16; /* Dummy Value */
1215 lpico
->descriptor
.Reserved1
=0;
1216 lpico
->descriptor
.Reserved2
=nPlanes
;
1217 lpico
->descriptor
.Reserved3
=nWidth
*nHeight
;
1219 /* either nPlanes and/or nBitCount is set to one */
1220 lpico
->descriptor
.icoDIBSize
=nWidth
*nHeight
*nPlanes
*nBitsPixel
;
1221 lpico
->descriptor
.icoDIBOffset
=0;
1223 if( !(lpico
->hBitmap
=CreateBitmap(nWidth
, nHeight
, nPlanes
, nBitsPixel
,
1225 printf("CreateIcon: couldn't create the XOR bitmap\n");
1229 /* the AND BitMask is always monochrome */
1230 if( !(lpico
->hBitMask
=CreateBitmap(nWidth
, nHeight
, 1, 1, lpANDbits
)) ) {
1231 printf("CreateIcon: couldn't create the AND bitmap\n");
1235 GlobalUnlock(hIcon
);
1236 #ifdef DEBUG_RESOURCE
1237 printf("CreateIcon Alloc hIcon=%X\n", hIcon
);