Large-scale renaming of all Win32 functions and types to use the
[wine/multimedia.git] / win32 / kernel32.c
blob60b1e1d8e8d8e9abe28814ca6106b28522ff6032
1 /*
2 * KERNEL32 thunks and other undocumented stuff
4 * Copyright 1997-1998 Marcus Meissner
5 * Copyright 1998 Ulrich Weigand
7 * BUG: The GetBinaryType implementation is not complete. See
8 * the function documentation for more details.
9 */
11 #include <string.h>
13 #include "windef.h"
14 #include "winbase.h"
15 #include "wine/winbase16.h"
16 #include "callback.h"
17 #include "task.h"
18 #include "user.h"
19 #include "heap.h"
20 #include "module.h"
21 #include "neexe.h"
22 #include "process.h"
23 #include "stackframe.h"
24 #include "heap.h"
25 #include "selectors.h"
26 #include "task.h"
27 #include "win.h"
28 #include "file.h"
29 #include "debug.h"
30 #include "flatthunk.h"
31 #include "syslevel.h"
32 #include "winerror.h"
35 /***********************************************************************
36 * *
37 * Win95 internal thunks *
38 * *
39 ***********************************************************************/
41 /***********************************************************************
42 * Generates a FT_Prolog call.
44 * 0FB6D1 movzbl edx,cl
45 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
46 * 68xxxxxxxx push FT_Prolog
47 * C3 lret
49 static void _write_ftprolog(LPBYTE relayCode ,DWORD *targetTable) {
50 LPBYTE x;
52 x = relayCode;
53 *x++ = 0x0f;*x++=0xb6;*x++=0xd1; /* movzbl edx,cl */
54 *x++ = 0x8B;*x++=0x14;*x++=0x95;*(DWORD**)x= targetTable;
55 x+=4; /* mov edx, [4*edx + targetTable] */
56 *x++ = 0x68; *(DWORD*)x = (DWORD)GetProcAddress(GetModuleHandleA("KERNEL32"),"FT_Prolog");
57 x+=4; /* push FT_Prolog */
58 *x++ = 0xC3; /* lret */
59 /* fill rest with 0xCC / int 3 */
62 /***********************************************************************
63 * _write_qtthunk (internal)
64 * Generates a QT_Thunk style call.
66 * 33C9 xor ecx, ecx
67 * 8A4DFC mov cl , [ebp-04]
68 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
69 * B8yyyyyyyy mov eax, QT_Thunk
70 * FFE0 jmp eax
72 static void _write_qtthunk(
73 LPBYTE relayCode, /* [in] start of QT_Thunk stub */
74 DWORD *targetTable /* [in] start of thunk (for index lookup) */
75 ) {
76 LPBYTE x;
78 x = relayCode;
79 *x++ = 0x33;*x++=0xC9; /* xor ecx,ecx */
80 *x++ = 0x8A;*x++=0x4D;*x++=0xFC; /* movb cl,[ebp-04] */
81 *x++ = 0x8B;*x++=0x14;*x++=0x8D;*(DWORD**)x= targetTable;
82 x+=4; /* mov edx, [4*ecx + targetTable */
83 *x++ = 0xB8; *(DWORD*)x = (DWORD)GetProcAddress(GetModuleHandleA("KERNEL32"),"QT_Thunk");
84 x+=4; /* mov eax , QT_Thunk */
85 *x++ = 0xFF; *x++ = 0xE0; /* jmp eax */
86 /* should fill the rest of the 32 bytes with 0xCC */
89 /***********************************************************************
90 * _loadthunk
92 static LPVOID _loadthunk(LPCSTR module, LPCSTR func, LPCSTR module32,
93 struct ThunkDataCommon *TD32, DWORD checksum)
95 struct ThunkDataCommon *TD16;
96 HMODULE hmod;
97 int ordinal;
99 if ((hmod = LoadLibrary16(module)) <= 32)
101 ERR(thunk, "(%s, %s, %s): Unable to load '%s', error %d\n",
102 module, func, module32, module, hmod);
103 return 0;
106 if ( !(ordinal = NE_GetOrdinal(hmod, func))
107 || !(TD16 = PTR_SEG_TO_LIN(NE_GetEntryPointEx(hmod, ordinal, FALSE))))
109 ERR(thunk, "(%s, %s, %s): Unable to find '%s'\n",
110 module, func, module32, func);
111 return 0;
114 if (TD32 && memcmp(TD16->magic, TD32->magic, 4))
116 ERR(thunk, "(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
117 module, func, module32,
118 TD16->magic[0], TD16->magic[1], TD16->magic[2], TD16->magic[3],
119 TD32->magic[0], TD32->magic[1], TD32->magic[2], TD32->magic[3]);
120 return 0;
123 if (TD32 && TD16->checksum != TD32->checksum)
125 ERR(thunk, "(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
126 module, func, module32, TD16->checksum, TD32->checksum);
127 return 0;
130 if (!TD32 && checksum && checksum != *(LPDWORD)TD16)
132 ERR(thunk, "(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
133 module, func, module32, *(LPDWORD)TD16, checksum);
134 return 0;
137 return TD16;
140 /***********************************************************************
141 * GetThunkStuff (KERNEL32.53)
143 LPVOID WINAPI GetThunkStuff(LPSTR module, LPSTR func)
145 return _loadthunk(module, func, "<kernel>", NULL, 0L);
148 /***********************************************************************
149 * GetThunkBuff (KERNEL32.52)
150 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
152 LPVOID WINAPI GetThunkBuff(void)
154 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
157 /***********************************************************************
158 * ThunkConnect32 (KERNEL32)
159 * Connects a 32bit and a 16bit thunkbuffer.
161 UINT WINAPI ThunkConnect32(
162 struct ThunkDataCommon *TD, /* [in/out] thunkbuffer */
163 LPSTR thunkfun16, /* [in] win16 thunkfunction */
164 LPSTR module16, /* [in] name of win16 dll */
165 LPSTR module32, /* [in] name of win32 dll */
166 HMODULE hmod32, /* [in] hmodule of win32 dll */
167 DWORD dwReason /* [in] initialisation argument */
169 BOOL directionSL;
171 if (!lstrncmpA(TD->magic, "SL01", 4))
173 directionSL = TRUE;
175 TRACE(thunk, "SL01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
176 module32, (DWORD)TD, module16, thunkfun16, dwReason);
178 else if (!lstrncmpA(TD->magic, "LS01", 4))
180 directionSL = FALSE;
182 TRACE(thunk, "LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
183 module32, (DWORD)TD, module16, thunkfun16, dwReason);
185 else
187 ERR(thunk, "Invalid magic %c%c%c%c\n",
188 TD->magic[0], TD->magic[1], TD->magic[2], TD->magic[3]);
189 return 0;
192 switch (dwReason)
194 case DLL_PROCESS_ATTACH:
196 struct ThunkDataCommon *TD16;
197 if (!(TD16 = _loadthunk(module16, thunkfun16, module32, TD, 0L)))
198 return 0;
200 if (directionSL)
202 struct ThunkDataSL32 *SL32 = (struct ThunkDataSL32 *)TD;
203 struct ThunkDataSL16 *SL16 = (struct ThunkDataSL16 *)TD16;
204 struct SLTargetDB *tdb;
206 if (SL16->fpData == NULL)
208 ERR(thunk, "ThunkConnect16 was not called!\n");
209 return 0;
212 SL32->data = SL16->fpData;
214 tdb = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb));
215 tdb->process = PROCESS_Current();
216 tdb->targetTable = (DWORD *)(thunkfun16 + SL32->offsetTargetTable);
218 tdb->next = SL32->data->targetDB; /* FIXME: not thread-safe! */
219 SL32->data->targetDB = tdb;
221 TRACE(thunk, "Process %08lx allocated TargetDB entry for ThunkDataSL %08lx\n",
222 (DWORD)PROCESS_Current(), (DWORD)SL32->data);
224 else
226 struct ThunkDataLS32 *LS32 = (struct ThunkDataLS32 *)TD;
227 struct ThunkDataLS16 *LS16 = (struct ThunkDataLS16 *)TD16;
229 LS32->targetTable = PTR_SEG_TO_LIN(LS16->targetTable);
231 /* write QT_Thunk and FT_Prolog stubs */
232 _write_qtthunk ((LPBYTE)TD + LS32->offsetQTThunk, LS32->targetTable);
233 _write_ftprolog((LPBYTE)TD + LS32->offsetFTProlog, LS32->targetTable);
235 break;
238 case DLL_PROCESS_DETACH:
239 /* FIXME: cleanup */
240 break;
243 return 1;
246 /**********************************************************************
247 * QT_Thunk (KERNEL32)
249 * The target address is in EDX.
250 * The 16 bit arguments start at ESP+4.
251 * The number of 16bit argumentbytes is EBP-ESP-0x44 (68 Byte thunksetup).
252 * [ok]
254 REGS_ENTRYPOINT(QT_Thunk)
256 CONTEXT context16;
257 DWORD argsize;
258 THDB *thdb = THREAD_Current();
260 memcpy(&context16,context,sizeof(context16));
262 CS_reg(&context16) = HIWORD(EDX_reg(context));
263 IP_reg(&context16) = LOWORD(EDX_reg(context));
264 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
265 + (WORD)&((STACK16FRAME*)0)->bp;
267 argsize = EBP_reg(context)-ESP_reg(context)-0x44;
269 memcpy( ((LPBYTE)THREAD_STACK16(thdb))-argsize,
270 (LPBYTE)ESP_reg(context)+4, argsize );
272 EAX_reg(context) = Callbacks->CallRegisterShortProc( &context16, argsize );
273 EDX_reg(context) = HIWORD(EAX_reg(context));
274 EAX_reg(context) = LOWORD(EAX_reg(context));
278 /**********************************************************************
279 * FT_Prolog (KERNEL32.233)
281 * The set of FT_... thunk routines is used instead of QT_Thunk,
282 * if structures have to be converted from 32-bit to 16-bit
283 * (change of member alignment, conversion of members).
285 * The thunk function (as created by the thunk compiler) calls
286 * FT_Prolog at the beginning, to set up a stack frame and
287 * allocate a 64 byte buffer on the stack.
288 * The input parameters (target address and some flags) are
289 * saved for later use by FT_Thunk.
291 * Input: EDX 16-bit target address (SEGPTR)
292 * CX bits 0..7 target number (in target table)
293 * bits 8..9 some flags (unclear???)
294 * bits 10..15 number of DWORD arguments
296 * Output: A new stackframe is created, and a 64 byte buffer
297 * allocated on the stack. The layout of the stack
298 * on return is as follows:
300 * (ebp+4) return address to caller of thunk function
301 * (ebp) old EBP
302 * (ebp-4) saved EBX register of caller
303 * (ebp-8) saved ESI register of caller
304 * (ebp-12) saved EDI register of caller
305 * (ebp-16) saved ECX register, containing flags
306 * (ebp-20) bitmap containing parameters that are to be converted
307 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
308 * filled in by the thunk code before calling FT_Thunk
309 * (ebp-24)
310 * ... (unclear)
311 * (ebp-44)
312 * (ebp-48) saved EAX register of caller (unclear, never restored???)
313 * (ebp-52) saved EDX register, containing 16-bit thunk target
314 * (ebp-56)
315 * ... (unclear)
316 * (ebp-64)
318 * ESP is EBP-68 on return.
322 REGS_ENTRYPOINT(FT_Prolog)
324 /* Pop return address to thunk code */
325 EIP_reg(context) = STACK32_POP(context);
327 /* Build stack frame */
328 STACK32_PUSH(context, EBP_reg(context));
329 EBP_reg(context) = ESP_reg(context);
331 /* Allocate 64-byte Thunk Buffer */
332 ESP_reg(context) -= 64;
333 memset((char *)ESP_reg(context), '\0', 64);
335 /* Store Flags (ECX) and Target Address (EDX) */
336 /* Save other registers to be restored later */
337 *(DWORD *)(EBP_reg(context) - 4) = EBX_reg(context);
338 *(DWORD *)(EBP_reg(context) - 8) = ESI_reg(context);
339 *(DWORD *)(EBP_reg(context) - 12) = EDI_reg(context);
340 *(DWORD *)(EBP_reg(context) - 16) = ECX_reg(context);
342 *(DWORD *)(EBP_reg(context) - 48) = EAX_reg(context);
343 *(DWORD *)(EBP_reg(context) - 52) = EDX_reg(context);
345 /* Push return address back onto stack */
346 STACK32_PUSH(context, EIP_reg(context));
349 /**********************************************************************
350 * FT_Thunk (KERNEL32.234)
352 * This routine performs the actual call to 16-bit code,
353 * similar to QT_Thunk. The differences are:
354 * - The call target is taken from the buffer created by FT_Prolog
355 * - Those arguments requested by the thunk code (by setting the
356 * corresponding bit in the bitmap at EBP-20) are converted
357 * from 32-bit pointers to segmented pointers (those pointers
358 * are guaranteed to point to structures copied to the stack
359 * by the thunk code, so we always use the 16-bit stack selector
360 * for those addresses).
362 * The bit #i of EBP-20 corresponds here to the DWORD starting at
363 * ESP+4 + 2*i.
365 * FIXME: It is unclear what happens if there are more than 32 WORDs
366 * of arguments, so that the single DWORD bitmap is no longer
367 * sufficient ...
370 REGS_ENTRYPOINT(FT_Thunk)
372 DWORD mapESPrelative = *(DWORD *)(EBP_reg(context) - 20);
373 DWORD callTarget = *(DWORD *)(EBP_reg(context) - 52);
375 CONTEXT context16;
376 DWORD i, argsize;
377 LPBYTE newstack, oldstack;
378 THDB *thdb = THREAD_Current();
380 memcpy(&context16,context,sizeof(context16));
382 CS_reg(&context16) = HIWORD(callTarget);
383 IP_reg(&context16) = LOWORD(callTarget);
384 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
385 + (WORD)&((STACK16FRAME*)0)->bp;
387 argsize = EBP_reg(context)-ESP_reg(context)-0x44;
388 newstack = ((LPBYTE)THREAD_STACK16(thdb))-argsize;
389 oldstack = (LPBYTE)ESP_reg(context)+4;
391 memcpy( newstack, oldstack, argsize );
393 for (i = 0; i < 32; i++) /* NOTE: What about > 32 arguments? */
394 if (mapESPrelative & (1 << i))
396 SEGPTR *arg = (SEGPTR *)(newstack + 2*i);
397 *arg = PTR_SEG_OFF_TO_SEGPTR(SELECTOROF(thdb->cur_stack),
398 OFFSETOF(thdb->cur_stack) - argsize
399 + (*(LPBYTE *)arg - oldstack));
402 EAX_reg(context) = Callbacks->CallRegisterShortProc( &context16, argsize );
403 EDX_reg(context) = HIWORD(EAX_reg(context));
404 EAX_reg(context) = LOWORD(EAX_reg(context));
407 /**********************************************************************
408 * FT_ExitNN (KERNEL32.218 - 232)
410 * One of the FT_ExitNN functions is called at the end of the thunk code.
411 * It removes the stack frame created by FT_Prolog, moves the function
412 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
413 * value, but the thunk code has moved it from EAX to EBX in the
414 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
415 * and perform a return to the CALLER of the thunk code (while removing
416 * the given number of arguments from the caller's stack).
419 static void FT_Exit(CONTEXT *context, int nPopArgs)
421 /* Return value is in EBX */
422 EAX_reg(context) = EBX_reg(context);
424 /* Restore EBX, ESI, and EDI registers */
425 EBX_reg(context) = *(DWORD *)(EBP_reg(context) - 4);
426 ESI_reg(context) = *(DWORD *)(EBP_reg(context) - 8);
427 EDI_reg(context) = *(DWORD *)(EBP_reg(context) - 12);
429 /* Clean up stack frame */
430 ESP_reg(context) = EBP_reg(context);
431 EBP_reg(context) = STACK32_POP(context);
433 /* Pop return address to CALLER of thunk code */
434 EIP_reg(context) = STACK32_POP(context);
435 /* Remove arguments */
436 ESP_reg(context) += nPopArgs;
437 /* Push return address back onto stack */
438 STACK32_PUSH(context, EIP_reg(context));
441 REGS_ENTRYPOINT(FT_Exit0) { FT_Exit(context, 0); }
442 REGS_ENTRYPOINT(FT_Exit4) { FT_Exit(context, 4); }
443 REGS_ENTRYPOINT(FT_Exit8) { FT_Exit(context, 8); }
444 REGS_ENTRYPOINT(FT_Exit12) { FT_Exit(context, 12); }
445 REGS_ENTRYPOINT(FT_Exit16) { FT_Exit(context, 16); }
446 REGS_ENTRYPOINT(FT_Exit20) { FT_Exit(context, 20); }
447 REGS_ENTRYPOINT(FT_Exit24) { FT_Exit(context, 24); }
448 REGS_ENTRYPOINT(FT_Exit28) { FT_Exit(context, 28); }
449 REGS_ENTRYPOINT(FT_Exit32) { FT_Exit(context, 32); }
450 REGS_ENTRYPOINT(FT_Exit36) { FT_Exit(context, 36); }
451 REGS_ENTRYPOINT(FT_Exit40) { FT_Exit(context, 40); }
452 REGS_ENTRYPOINT(FT_Exit44) { FT_Exit(context, 44); }
453 REGS_ENTRYPOINT(FT_Exit48) { FT_Exit(context, 48); }
454 REGS_ENTRYPOINT(FT_Exit52) { FT_Exit(context, 52); }
455 REGS_ENTRYPOINT(FT_Exit56) { FT_Exit(context, 56); }
458 /**********************************************************************
459 * WOWCallback16 (KERNEL32.62)(WOW32.2)
460 * Calls a win16 function with a single DWORD argument.
461 * RETURNS
462 * the return value
464 DWORD WINAPI WOWCallback16(
465 FARPROC16 fproc, /* [in] win16 function to call */
466 DWORD arg /* [in] single DWORD argument to function */
468 DWORD ret;
469 TRACE(thunk,"(%p,0x%08lx)...\n",fproc,arg);
470 ret = Callbacks->CallWOWCallbackProc(fproc,arg);
471 TRACE(thunk,"... returns %ld\n",ret);
472 return ret;
475 /**********************************************************************
476 * WOWCallback16Ex (KERNEL32.55)(WOW32.3)
477 * Calls a function in 16bit code.
478 * RETURNS
479 * TRUE for success
481 BOOL WINAPI WOWCallback16Ex(
482 FARPROC16 vpfn16, /* [in] win16 function to call */
483 DWORD dwFlags, /* [in] flags */
484 DWORD cbArgs, /* [in] nr of arguments */
485 LPVOID pArgs, /* [in] pointer to arguments (LPDWORD) */
486 LPDWORD pdwRetCode /* [out] return value of win16 function */
488 return Callbacks->CallWOWCallback16Ex(vpfn16,dwFlags,cbArgs,pArgs,pdwRetCode);
491 /***********************************************************************
492 * ThunkInitLS (KERNEL32.43)
493 * A thunkbuffer link routine
494 * The thunkbuf looks like:
496 * 00: DWORD length ? don't know exactly
497 * 04: SEGPTR ptr ? where does it point to?
498 * The pointer ptr is written into the first DWORD of 'thunk'.
499 * (probably correct implemented)
500 * [ok probably]
501 * RETURNS
502 * segmented pointer to thunk?
504 DWORD WINAPI ThunkInitLS(
505 LPDWORD thunk, /* [in] win32 thunk */
506 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
507 DWORD len, /* [in] thkbuffer length */
508 LPCSTR dll16, /* [in] name of win16 dll */
509 LPCSTR dll32 /* [in] name of win32 dll (FIXME: not used?) */
511 LPDWORD addr;
513 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
514 return 0;
516 if (!addr[1])
517 return 0;
518 *(DWORD*)thunk = addr[1];
520 return addr[1];
523 /***********************************************************************
524 * Common32ThkLS (KERNEL32.45)
526 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
527 * style thunks. The basic difference is that the parameter conversion
528 * is done completely on the *16-bit* side here. Thus we do not call
529 * the 16-bit target directly, but call a common entry point instead.
530 * This entry function then calls the target according to the target
531 * number passed in the DI register.
533 * Input: EAX SEGPTR to the common 16-bit entry point
534 * CX offset in thunk table (target number * 4)
535 * DX error return value if execution fails (unclear???)
536 * EDX.HI number of DWORD parameters
538 * (Note that we need to move the thunk table offset from CX to DI !)
540 * The called 16-bit stub expects its stack to look like this:
541 * ...
542 * (esp+40) 32-bit arguments
543 * ...
544 * (esp+8) 32 byte of stack space available as buffer
545 * (esp) 8 byte return address for use with 0x66 lret
547 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
548 * and uses the EAX register to return a DWORD return value.
549 * Thus we need to use a special assembly glue routine
550 * (CallRegisterLongProc instead of CallRegisterShortProc).
552 * Finally, we return to the caller, popping the arguments off
553 * the stack.
555 * FIXME: The called function uses EBX to return the number of
556 * arguments that are to be popped off the caller's stack.
557 * This is clobbered by the assembly glue, so we simply use
558 * the original EDX.HI to get the number of arguments.
559 * (Those two values should be equal anyway ...?)
562 REGS_ENTRYPOINT(Common32ThkLS)
564 CONTEXT context16;
565 DWORD argsize;
566 THDB *thdb = THREAD_Current();
568 memcpy(&context16,context,sizeof(context16));
570 DI_reg(&context16) = CX_reg(context);
571 CS_reg(&context16) = HIWORD(EAX_reg(context));
572 IP_reg(&context16) = LOWORD(EAX_reg(context));
573 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
574 + (WORD)&((STACK16FRAME*)0)->bp;
576 argsize = HIWORD(EDX_reg(context)) * 4;
578 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
579 if (EDX_reg(context) == EIP_reg(context))
580 argsize = 6 * 4;
582 memcpy( ((LPBYTE)THREAD_STACK16(thdb))-argsize,
583 (LPBYTE)ESP_reg(context)+4, argsize );
585 EAX_reg(context) = Callbacks->CallRegisterLongProc(&context16, argsize + 32);
587 /* Clean up caller's stack frame */
589 EIP_reg(context) = STACK32_POP(context);
590 ESP_reg(context) += argsize;
591 STACK32_PUSH(context, EIP_reg(context));
594 /***********************************************************************
595 * OT_32ThkLSF (KERNEL32.40)
597 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
598 * argument processing is done on both the 32-bit and the 16-bit side:
599 * The 32-bit side prepares arguments, copying them onto the stack.
601 * When this routine is called, the first word on the stack is the
602 * number of argument bytes prepared by the 32-bit code, and EDX
603 * contains the 16-bit target address.
605 * The called 16-bit routine is another relaycode, doing further
606 * argument processing and then calling the real 16-bit target
607 * whose address is stored at [bp-04].
609 * The call proceeds using a normal CallRegisterShortProc.
610 * After return from the 16-bit relaycode, the arguments need
611 * to be copied *back* to the 32-bit stack, since the 32-bit
612 * relaycode processes output parameters.
614 * Note that we copy twice the number of arguments, since some of the
615 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
616 * arguments of the caller!
618 * (Note that this function seems only to be used for
619 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
621 REGS_ENTRYPOINT(OT_32ThkLSF)
623 CONTEXT context16;
624 DWORD argsize;
625 THDB *thdb = THREAD_Current();
627 memcpy(&context16,context,sizeof(context16));
629 CS_reg(&context16) = HIWORD(EDX_reg(context));
630 IP_reg(&context16) = LOWORD(EDX_reg(context));
631 EBP_reg(&context16) = OFFSETOF( thdb->cur_stack )
632 + (WORD)&((STACK16FRAME*)0)->bp;
634 argsize = 2 * *(WORD *)(ESP_reg(context) + 4) + 2;
636 memcpy( ((LPBYTE)THREAD_STACK16(thdb))-argsize,
637 (LPBYTE)ESP_reg(context)+4, argsize );
639 EAX_reg(context) = Callbacks->CallRegisterShortProc(&context16, argsize);
641 memcpy( (LPBYTE)ESP_reg(context)+4,
642 ((LPBYTE)THREAD_STACK16(thdb))-argsize, argsize );
645 /***********************************************************************
646 * ThunkInitLSF (KERNEL32.41)
647 * A thunk setup routine.
648 * Expects a pointer to a preinitialized thunkbuffer in the first argument
649 * looking like:
650 * 00..03: unknown (pointer, check _41, _43, _46)
651 * 04: EB1E jmp +0x20
653 * 06..23: unknown (space for replacement code, check .90)
655 * 24:>E800000000 call offset 29
656 * 29:>58 pop eax ( target of call )
657 * 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
658 * 2F: BAxxxxxxxx mov edx,xxxxxxxx
659 * 34: 68yyyyyyyy push KERNEL32.90
660 * 39: C3 ret
662 * 3A: EB1E jmp +0x20
663 * 3E ... 59: unknown (space for replacement code?)
664 * 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
665 * 5F: 5A pop edx
666 * 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
667 * 66: 52 push edx
668 * 67: 68xxxxxxxx push xxxxxxxx
669 * 6C: 68yyyyyyyy push KERNEL32.89
670 * 71: C3 ret
671 * 72: end?
672 * This function checks if the code is there, and replaces the yyyyyyyy entries
673 * by the functionpointers.
674 * The thunkbuf looks like:
676 * 00: DWORD length ? don't know exactly
677 * 04: SEGPTR ptr ? where does it point to?
678 * The segpointer ptr is written into the first DWORD of 'thunk'.
679 * [ok probably]
680 * RETURNS
681 * unclear, pointer to win16 thkbuffer?
683 LPVOID WINAPI ThunkInitLSF(
684 LPBYTE thunk, /* [in] win32 thunk */
685 LPCSTR thkbuf, /* [in] thkbuffer name in win16 dll */
686 DWORD len, /* [in] length of thkbuffer */
687 LPCSTR dll16, /* [in] name of win16 dll */
688 LPCSTR dll32 /* [in] name of win32 dll */
690 HMODULE hkrnl32 = GetModuleHandleA("KERNEL32");
691 LPDWORD addr,addr2;
693 /* FIXME: add checks for valid code ... */
694 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
695 *(DWORD*)(thunk+0x35) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)90);
696 *(DWORD*)(thunk+0x6D) = (DWORD)GetProcAddress(hkrnl32,(LPSTR)89);
699 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
700 return 0;
702 addr2 = PTR_SEG_TO_LIN(addr[1]);
703 if (HIWORD(addr2))
704 *(DWORD*)thunk = (DWORD)addr2;
706 return addr2;
709 /***********************************************************************
710 * FT_PrologPrime (KERNEL32.89)
712 * This function is called from the relay code installed by
713 * ThunkInitLSF. It replaces the location from where it was
714 * called by a standard FT_Prolog call stub (which is 'primed'
715 * by inserting the correct target table pointer).
716 * Finally, it calls that stub.
718 * Input: ECX target number + flags (passed through to FT_Prolog)
719 * (ESP) offset of location where target table pointer
720 * is stored, relative to the start of the relay code
721 * (ESP+4) pointer to start of relay code
722 * (this is where the FT_Prolog call stub gets written to)
724 * Note: The two DWORD arguments get popped from the stack.
727 REGS_ENTRYPOINT(FT_PrologPrime)
729 DWORD targetTableOffset = STACK32_POP(context);
730 LPBYTE relayCode = (LPBYTE)STACK32_POP(context);
731 DWORD *targetTable = *(DWORD **)(relayCode+targetTableOffset);
732 DWORD targetNr = LOBYTE(ECX_reg(context));
734 _write_ftprolog(relayCode, targetTable);
736 /* We should actually call the relay code now, */
737 /* but we skip it and go directly to FT_Prolog */
738 EDX_reg(context) = targetTable[targetNr];
739 __regs_FT_Prolog(context);
742 /***********************************************************************
743 * QT_ThunkPrime (KERNEL32.90)
745 * This function corresponds to FT_PrologPrime, but installs a
746 * call stub for QT_Thunk instead.
748 * Input: (EBP-4) target number (passed through to QT_Thunk)
749 * EDX target table pointer location offset
750 * EAX start of relay code
753 REGS_ENTRYPOINT(QT_ThunkPrime)
755 DWORD targetTableOffset = EDX_reg(context);
756 LPBYTE relayCode = (LPBYTE)EAX_reg(context);
757 DWORD *targetTable = *(DWORD **)(relayCode+targetTableOffset);
758 DWORD targetNr = LOBYTE(*(DWORD *)(EBP_reg(context) - 4));
760 _write_qtthunk(relayCode, targetTable);
762 /* We should actually call the relay code now, */
763 /* but we skip it and go directly to QT_Thunk */
764 EDX_reg(context) = targetTable[targetNr];
765 __regs_QT_Thunk(context);
768 /***********************************************************************
769 * (KERNEL32.46)
770 * Another thunkbuf link routine.
771 * The start of the thunkbuf looks like this:
772 * 00: DWORD length
773 * 04: SEGPTR address for thunkbuffer pointer
774 * [ok probably]
776 VOID WINAPI ThunkInitSL(
777 LPBYTE thunk, /* [in] start of thunkbuffer */
778 LPCSTR thkbuf, /* [in] name/ordinal of thunkbuffer in win16 dll */
779 DWORD len, /* [in] length of thunkbuffer */
780 LPCSTR dll16, /* [in] name of win16 dll containing the thkbuf */
781 LPCSTR dll32 /* [in] win32 dll. FIXME: strange, unused */
783 LPDWORD addr;
785 if (!(addr = _loadthunk( dll16, thkbuf, dll32, NULL, len )))
786 return;
788 *(DWORD*)PTR_SEG_TO_LIN(addr[1]) = (DWORD)thunk;
791 /**********************************************************************
792 * SSInit KERNEL.700
793 * RETURNS
794 * TRUE for success.
796 BOOL WINAPI SSInit16()
798 return TRUE;
801 /**********************************************************************
802 * SSOnBigStack KERNEL32.87
803 * Check if thunking is initialized (ss selector set up etc.)
804 * We do that differently, so just return TRUE.
805 * [ok]
806 * RETURNS
807 * TRUE for success.
809 BOOL WINAPI SSOnBigStack()
811 TRACE(thunk, "Yes, thunking is initialized\n");
812 return TRUE;
815 /**********************************************************************
816 * SSCall
817 * One of the real thunking functions. This one seems to be for 32<->32
818 * thunks. It should probably be capable of crossing processboundaries.
820 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
821 * [ok]
823 DWORD WINAPIV SSCall(
824 DWORD nr, /* [in] number of argument bytes */
825 DWORD flags, /* [in] FIXME: flags ? */
826 FARPROC fun, /* [in] function to call */
827 ... /* [in/out] arguments */
829 DWORD i,ret;
830 DWORD *args = ((DWORD *)&fun) + 1;
832 if(TRACE_ON(thunk)){
833 dbg_decl_str(thunk, 256);
834 for (i=0;i<nr/4;i++)
835 dsprintf(thunk,"0x%08lx,",args[i]);
836 TRACE(thunk,"(%ld,0x%08lx,%p,[%s])\n",
837 nr,flags,fun,dbg_str(thunk));
839 switch (nr) {
840 case 0: ret = fun();
841 break;
842 case 4: ret = fun(args[0]);
843 break;
844 case 8: ret = fun(args[0],args[1]);
845 break;
846 case 12: ret = fun(args[0],args[1],args[2]);
847 break;
848 case 16: ret = fun(args[0],args[1],args[2],args[3]);
849 break;
850 case 20: ret = fun(args[0],args[1],args[2],args[3],args[4]);
851 break;
852 case 24: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5]);
853 break;
854 case 28: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
855 break;
856 case 32: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7]);
857 break;
858 case 36: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8]);
859 break;
860 case 40: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9]);
861 break;
862 case 44: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10]);
863 break;
864 case 48: ret = fun(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],args[10],args[11]);
865 break;
866 default:
867 WARN(thunk,"Unsupported nr of arguments, %ld\n",nr);
868 ret = 0;
869 break;
872 TRACE(thunk," returning %ld ...\n",ret);
873 return ret;
876 /**********************************************************************
877 * W32S_BackTo32 (KERNEL32.51)
879 REGS_ENTRYPOINT(W32S_BackTo32)
881 LPDWORD stack = (LPDWORD)ESP_reg( context );
882 FARPROC proc = (FARPROC) stack[0];
884 EAX_reg( context ) = proc( stack[2], stack[3], stack[4], stack[5], stack[6],
885 stack[7], stack[8], stack[9], stack[10], stack[11] );
887 EIP_reg( context ) = stack[1];
890 /**********************************************************************
891 * AllocSLCallback (KERNEL32)
893 * Win95 uses some structchains for callbacks. It allocates them
894 * in blocks of 100 entries, size 32 bytes each, layout:
895 * blockstart:
896 * 0: PTR nextblockstart
897 * 4: entry *first;
898 * 8: WORD sel ( start points to blockstart)
899 * A: WORD unknown
900 * 100xentry:
901 * 00..17: Code
902 * 18: PDB *owning_process;
903 * 1C: PTR blockstart
905 * We ignore this for now. (Just a note for further developers)
906 * FIXME: use this method, so we don't waste selectors...
908 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
909 * the 0x66 prefix switches from word->long registers.
911 * 665A pop edx
912 * 6668x arg2 x pushl <arg2>
913 * 6652 push edx
914 * EAx arg1 x jmpf <arg1>
916 * returns the startaddress of this thunk.
918 * Note, that they look very similair to the ones allocates by THUNK_Alloc.
919 * RETURNS
920 * segmented pointer to the start of the thunk
922 DWORD WINAPI
923 AllocSLCallback(
924 DWORD finalizer, /* [in] finalizer function */
925 DWORD callback /* [in] callback function */
927 LPBYTE x,thunk = HeapAlloc( GetProcessHeap(), 0, 32 );
928 WORD sel;
930 x=thunk;
931 *x++=0x66;*x++=0x5a; /* popl edx */
932 *x++=0x66;*x++=0x68;*(DWORD*)x=finalizer;x+=4; /* pushl finalizer */
933 *x++=0x66;*x++=0x52; /* pushl edx */
934 *x++=0xea;*(DWORD*)x=callback;x+=4; /* jmpf callback */
936 *(PDB**)(thunk+18) = PROCESS_Current();
938 sel = SELECTOR_AllocBlock( thunk , 32, SEGMENT_CODE, FALSE, FALSE );
939 return (sel<<16)|0;
942 /**********************************************************************
943 * FreeSLCallback (KERNEL32.274)
944 * Frees the specified 16->32 callback
946 void WINAPI
947 FreeSLCallback(
948 DWORD x /* [in] 16 bit callback (segmented pointer?) */
950 FIXME(win32,"(0x%08lx): stub\n",x);
954 /**********************************************************************
955 * GetTEBSelectorFS (KERNEL.475)
956 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
958 VOID WINAPI GetTEBSelectorFS16( CONTEXT *context )
960 GET_FS( FS_reg(context) );
963 /**********************************************************************
964 * KERNEL_431 (KERNEL.431)
965 * IsPeFormat (W32SYS.2)
966 * Checks the passed filename if it is a PE format executeable
967 * RETURNS
968 * TRUE, if it is.
969 * FALSE if not.
971 BOOL16 WINAPI IsPeFormat16(
972 LPSTR fn, /* [in] filename to executeable */
973 HFILE16 hf16 /* [in] open file, if filename is NULL */
975 IMAGE_DOS_HEADER mzh;
976 HFILE hf=FILE_GetHandle(hf16);
977 OFSTRUCT ofs;
978 DWORD xmagic;
980 if (fn) {
981 hf = OpenFile(fn,&ofs,OF_READ);
982 if (hf==HFILE_ERROR)
983 return FALSE;
985 _llseek(hf,0,SEEK_SET);
986 if (sizeof(mzh)!=_lread(hf,&mzh,sizeof(mzh))) {
987 _lclose(hf);
988 return FALSE;
990 if (mzh.e_magic!=IMAGE_DOS_SIGNATURE) {
991 WARN(dosmem,"File has not got dos signature!\n");
992 _lclose(hf);
993 return FALSE;
995 _llseek(hf,mzh.e_lfanew,SEEK_SET);
996 if (sizeof(DWORD)!=_lread(hf,&xmagic,sizeof(DWORD))) {
997 _lclose(hf);
998 return FALSE;
1000 _lclose(hf);
1001 return (xmagic == IMAGE_NT_SIGNATURE);
1004 /***********************************************************************
1005 * WOWHandle32 (KERNEL32.57)(WOW32.16)
1006 * Converts a win16 handle of type into the respective win32 handle.
1007 * We currently just return this handle, since most handles are the same
1008 * for win16 and win32.
1009 * RETURNS
1010 * The new handle
1012 HANDLE WINAPI WOWHandle32(
1013 WORD handle, /* [in] win16 handle */
1014 WOW_HANDLE_TYPE type /* [in] handle type */
1016 TRACE(win32,"(0x%04x,%d)\n",handle,type);
1017 return (HANDLE)handle;
1020 /***********************************************************************
1021 * K32Thk1632Prolog (KERNEL32.492)
1023 REGS_ENTRYPOINT(K32Thk1632Prolog)
1025 LPBYTE code = (LPBYTE)EIP_reg(context) - 5;
1027 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1028 of 16->32 thunks instead of using one of the standard methods!
1029 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1030 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1031 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1032 bypassed, which means it will crash the next time the 32-bit OLE
1033 code thunks down again to 16-bit (this *will* happen!).
1035 The following hack tries to recognize this situation.
1036 This is possible since the called stubs in OLECLI32/OLESVR32 all
1037 look exactly the same:
1038 00 E8xxxxxxxx call K32Thk1632Prolog
1039 05 FF55FC call [ebp-04]
1040 08 E8xxxxxxxx call K32Thk1632Epilog
1041 0D 66CB retf
1043 If we recognize this situation, we try to simulate the actions
1044 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1045 to our 32-bit stack, creating a proper STACK16FRAME and
1046 updating thdb->cur_stack. */
1048 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1049 && code[13] == 0x66 && code[14] == 0xCB)
1051 WORD stackSel = NtCurrentTeb()->stack_sel;
1052 DWORD stackBase = GetSelectorBase(stackSel);
1054 THDB *thdb = THREAD_Current();
1055 DWORD argSize = EBP_reg(context) - ESP_reg(context);
1056 char *stack16 = (char *)ESP_reg(context);
1057 char *stack32 = (char *)thdb->cur_stack - argSize;
1058 STACK16FRAME *frame16 = (STACK16FRAME *)stack16 - 1;
1060 TRACE(thunk, "before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1061 EBP_reg(context), ESP_reg(context), thdb->cur_stack);
1063 memset(frame16, '\0', sizeof(STACK16FRAME));
1064 frame16->frame32 = (STACK32FRAME *)thdb->cur_stack;
1065 frame16->ebp = EBP_reg(context);
1067 memcpy(stack32, stack16, argSize);
1068 thdb->cur_stack = PTR_SEG_OFF_TO_SEGPTR(stackSel, (DWORD)frame16 - stackBase);
1070 ESP_reg(context) = (DWORD)stack32;
1071 EBP_reg(context) = ESP_reg(context) + argSize;
1073 TRACE(thunk, "after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1074 EBP_reg(context), ESP_reg(context), thdb->cur_stack);
1077 SYSLEVEL_ReleaseWin16Lock();
1080 /***********************************************************************
1081 * K32Thk1632Epilog (KERNEL32.491)
1083 REGS_ENTRYPOINT(K32Thk1632Epilog)
1085 LPBYTE code = (LPBYTE)EIP_reg(context) - 13;
1087 SYSLEVEL_RestoreWin16Lock();
1089 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1091 if ( code[5] == 0xFF && code[6] == 0x55 && code[7] == 0xFC
1092 && code[13] == 0x66 && code[14] == 0xCB)
1094 THDB *thdb = THREAD_Current();
1095 STACK16FRAME *frame16 = (STACK16FRAME *)PTR_SEG_TO_LIN(thdb->cur_stack);
1096 char *stack16 = (char *)(frame16 + 1);
1097 DWORD argSize = frame16->ebp - (DWORD)stack16;
1098 char *stack32 = (char *)frame16->frame32 - argSize;
1100 DWORD nArgsPopped = ESP_reg(context) - (DWORD)stack32;
1102 TRACE(thunk, "before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1103 EBP_reg(context), ESP_reg(context), thdb->cur_stack);
1105 thdb->cur_stack = (DWORD)frame16->frame32;
1107 ESP_reg(context) = (DWORD)stack16 + nArgsPopped;
1108 EBP_reg(context) = frame16->ebp;
1110 TRACE(thunk, "after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1111 EBP_reg(context), ESP_reg(context), thdb->cur_stack);
1115 /***********************************************************************
1116 * UpdateResource32A (KERNEL32.707)
1118 BOOL WINAPI UpdateResourceA(
1119 HANDLE hUpdate,
1120 LPCSTR lpType,
1121 LPCSTR lpName,
1122 WORD wLanguage,
1123 LPVOID lpData,
1124 DWORD cbData) {
1126 FIXME(win32, ": stub\n");
1127 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1128 return FALSE;
1131 /***********************************************************************
1132 * UpdateResource32W (KERNEL32.708)
1134 BOOL WINAPI UpdateResourceW(
1135 HANDLE hUpdate,
1136 LPCWSTR lpType,
1137 LPCWSTR lpName,
1138 WORD wLanguage,
1139 LPVOID lpData,
1140 DWORD cbData) {
1142 FIXME(win32, ": stub\n");
1143 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1144 return FALSE;
1148 /***********************************************************************
1149 * WaitNamedPipe32A [KERNEL32.725]
1151 BOOL WINAPI WaitNamedPipeA (LPCSTR lpNamedPipeName, DWORD nTimeOut)
1152 { FIXME (win32,"%s 0x%08lx\n",lpNamedPipeName,nTimeOut);
1153 SetLastError(ERROR_PIPE_NOT_CONNECTED);
1154 return FALSE;
1156 /***********************************************************************
1157 * WaitNamedPipe32W [KERNEL32.726]
1159 BOOL WINAPI WaitNamedPipeW (LPCWSTR lpNamedPipeName, DWORD nTimeOut)
1160 { FIXME (win32,"%s 0x%08lx\n",debugstr_w(lpNamedPipeName),nTimeOut);
1161 SetLastError(ERROR_PIPE_NOT_CONNECTED);
1162 return FALSE;
1165 /***********************************************************************
1166 * GetBinaryType32A [KERNEL32.280]
1168 * The GetBinaryType function determines whether a file is executable
1169 * or not and if it is it returns what type of executable it is.
1170 * The type of executable is a property that determines in which
1171 * subsystem an executable file runs under.
1173 * lpApplicationName: points to a fully qualified path of the file to test
1174 * lpBinaryType: points to a variable that will receive the binary type info
1176 * Binary types returned:
1177 * SCS_32BIT_BINARY: A win32 based application
1178 * SCS_DOS_BINARY: An MS-Dos based application
1179 * SCS_WOW_BINARY: A 16bit OS/2 based application
1180 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app ( Not implemented )
1181 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
1182 * SCS_OS216_BINARY: A 16bit Windows based application ( Not implemented )
1184 * Returns TRUE if the file is an executable in which case
1185 * the value pointed by lpBinaryType is set.
1186 * Returns FALSE if the file is not an executable or if the function fails.
1188 * This function is not complete. It can only determine if a file
1189 * is a DOS, 32bit/16bit Windows executable. Also .COM file support
1190 * is not complete.
1191 * To do so it opens the file and reads in the header information
1192 * if the extended header information is not presend it will
1193 * assume that that the file is a DOS executable.
1194 * If the extended header information is present it will
1195 * determine if the file is an 16 or 32 bit Windows executable
1196 * by check the flags in the header.
1198 BOOL WINAPI GetBinaryTypeA (LPCSTR lpApplicationName, LPDWORD lpBinaryType)
1200 BOOL ret = FALSE;
1201 HFILE hfile;
1202 OFSTRUCT ofs;
1203 IMAGE_DOS_HEADER mz_header;
1204 char magic[4];
1206 TRACE (win32,"%s\n",lpApplicationName);
1208 /* Sanity check.
1210 if( lpApplicationName == NULL || lpBinaryType == NULL )
1212 return FALSE;
1215 /* Open the file indicated by lpApplicationName for reading.
1217 hfile = OpenFile( lpApplicationName, &ofs, OF_READ );
1219 /* If we cannot read the file return failed.
1221 if( hfile == HFILE_ERROR )
1223 return FALSE;
1226 /* Seek to the start of the file and read the DOS header information.
1228 if( _llseek( hfile, 0, SEEK_SET ) >= 0 &&
1229 _lread( hfile, &mz_header, sizeof(mz_header) ) == sizeof(mz_header) )
1231 /* Now that we have the header check the e_magic field
1232 * to see if this is a dos image.
1234 if( mz_header.e_magic == IMAGE_DOS_SIGNATURE )
1236 BOOL lfanewValid = FALSE;
1237 /* We do have a DOS image so we will now try to seek into
1238 * the file by the amount indicated by the field
1239 * "Offset to extended header" and read in the
1240 * "magic" field information at that location.
1241 * This will tell us if there is more header information
1242 * to read or not.
1245 /* But before we do we will make sure that header
1246 * structure encompasses the "Offset to extended header"
1247 * field.
1249 if( (mz_header.e_cparhdr<<4) >= sizeof(IMAGE_DOS_HEADER) )
1251 if( ( mz_header.e_crlc == 0 && mz_header.e_lfarlc == 0 ) ||
1252 ( mz_header.e_lfarlc >= sizeof(IMAGE_DOS_HEADER) ) )
1254 if( mz_header.e_lfanew >= sizeof(IMAGE_DOS_HEADER) &&
1255 _llseek( hfile, mz_header.e_lfanew, SEEK_SET ) >= 0 &&
1256 _lread( hfile, magic, sizeof(magic) ) == sizeof(magic) )
1258 lfanewValid = TRUE;
1263 if( lfanewValid == FALSE )
1265 /* If we cannot read this "extended header" we will
1266 * assume that we have a simple DOS executable.
1268 FIXME( win32, "Determine if this check is complete enough\n" );
1269 *lpBinaryType = SCS_DOS_BINARY;
1270 ret = TRUE;
1272 else
1274 /* Reading the magic field succeeded so
1275 * we will not try to determine what type it is.
1277 if( *(DWORD*)magic == IMAGE_NT_SIGNATURE )
1279 /* This is an NT signature.
1281 *lpBinaryType = SCS_32BIT_BINARY;
1282 ret = TRUE;
1284 else if( *(WORD*)magic == IMAGE_OS2_SIGNATURE )
1286 /* The IMAGE_OS2_SIGNATURE indicates that the
1287 * "extended header is a Windows executable (NE)
1288 * header. This is a bit misleading, but it is
1289 * documented in the SDK. ( for more details see
1290 * the neexe.h file )
1293 /* Now we know that it is a Windows executable
1294 * we will read in the Windows header and
1295 * determine if it is a 16/32bit Windows executable.
1297 IMAGE_OS2_HEADER ne_header;
1298 if( _lread( hfile, &ne_header, sizeof(ne_header) ) == sizeof(ne_header) )
1300 /* Check the format flag to determine if it is
1301 * Win32 or not.
1303 if( ne_header.format_flags & NE_FFLAGS_WIN32 )
1305 *lpBinaryType = SCS_32BIT_BINARY;
1306 ret = TRUE;
1308 else
1310 /* We will assume it is a 16bit Windows executable.
1311 * I'm not sure if this check is sufficient.
1313 FIXME( win32, "Determine if this check is complete enough\n" );
1314 *lpBinaryType = SCS_WOW_BINARY;
1315 ret = TRUE;
1323 /* Close the file.
1325 CloseHandle( hfile );
1327 return ret;
1331 /***********************************************************************
1332 * GetBinaryType32W [KERNEL32.281]
1334 * See GetBinaryType32A.
1336 BOOL WINAPI GetBinaryTypeW (LPCWSTR lpApplicationName, LPDWORD lpBinaryType)
1338 BOOL ret = FALSE;
1339 LPSTR strNew = NULL;
1341 TRACE (win32,"%s\n",debugstr_w(lpApplicationName));
1343 /* Sanity check.
1345 if( lpApplicationName == NULL || lpBinaryType == NULL )
1347 return FALSE;
1351 /* Convert the wide string to a ascii string.
1353 strNew = HEAP_strdupWtoA( GetProcessHeap(), 0, lpApplicationName );
1355 if( strNew != NULL )
1357 ret = GetBinaryTypeA( strNew, lpBinaryType );
1359 /* Free the allocated string.
1361 HeapFree( GetProcessHeap(), 0, strNew );
1364 return ret;
1367 /*********************************************************************
1368 * PK16FNF [KERNEL32.91]
1370 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1371 * string buffer with the 8.3 filename of a recently loaded 16-bit
1372 * module. It is unknown exactly what modules trigger this
1373 * mechanism or what purpose this serves. Win98 Explorer (and
1374 * probably also Win95 with IE 4 shell integration) calls this
1375 * several times during initialization.
1377 * FIXME: find out what this really does and make it work.
1379 void WINAPI PK16FNF(LPSTR strPtr)
1381 FIXME(win32, "(%p): stub\n", strPtr);
1383 /* fill in a fake filename that'll be easy to recognize */
1384 lstrcpyA(strPtr, "WINESTUB.FIX");