mstask: Actually run a test and fix the expected behaviour.
[wine] / dlls / ddeml.dll16 / ddeml.c
1 /*
2  * DDEML library
3  *
4  * Copyright 1997 Alexandre Julliard
5  * Copyright 1997 Len White
6  * Copyright 1999 Keith Matthews
7  * Copyright 2000 Corel
8  * Copyright 2001,2002,2009 Eric Pouech
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdarg.h>
29 #include <string.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wine/windef16.h"
33 #include "wine/winbase16.h"
34 #include "wownt32.h"
35 #include "dde.h"
36 #include "ddeml.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ddeml);
40
41
42 typedef HDDEDATA (CALLBACK *PFNCALLBACK16)(UINT16,UINT16,HCONV,HSZ,HSZ,HDDEDATA,
43                                            DWORD,DWORD);
44
45 typedef struct
46 {
47     UINT16  cb;
48     UINT16  wFlags;
49     UINT16  wCountryID;
50     INT16   iCodePage;
51     DWORD   dwLangID;
52     DWORD   dwSecurity;
53 } CONVCONTEXT16, *LPCONVCONTEXT16;
54
55 typedef struct
56 {
57     DWORD          cb;
58     DWORD          hUser;
59     HCONV          hConvPartner;
60     HSZ            hszSvcPartner;
61     HSZ            hszServiceReq;
62     HSZ            hszTopic;
63     HSZ            hszItem;
64     UINT16         wFmt;
65     UINT16         wType;
66     UINT16         wStatus;
67     UINT16         wConvst;
68     UINT16         wLastError;
69     HCONVLIST      hConvList;
70     CONVCONTEXT16  ConvCtxt;
71 } CONVINFO16, *LPCONVINFO16;
72
73 static void map1632_conv_context(CONVCONTEXT* cc32, const CONVCONTEXT16* cc16)
74 {
75     cc32->cb = sizeof(*cc32);
76     cc32->wFlags = cc16->wFlags;
77     cc32->wCountryID = cc16->wCountryID;
78     cc32->iCodePage = cc16->iCodePage;
79     cc32->dwLangID = cc16->dwLangID;
80     cc32->dwSecurity = cc16->dwSecurity;
81 }
82
83 static void map3216_conv_context(CONVCONTEXT16* cc16, const CONVCONTEXT* cc32)
84 {
85     cc16->cb = sizeof(*cc16);
86     cc16->wFlags = cc32->wFlags;
87     cc16->wCountryID = cc32->wCountryID;
88     cc16->iCodePage = cc32->iCodePage;
89     cc16->dwLangID = cc32->dwLangID;
90     cc16->dwSecurity = cc32->dwSecurity;
91 }
92
93 /******************************************************************
94  *              WDML_InvokeCallback16
95  *
96  *
97  */
98 static HDDEDATA CALLBACK WDML_InvokeCallback16(DWORD pfn16, UINT uType, UINT uFmt,
99                                                HCONV hConv, HSZ hsz1, HSZ hsz2,
100                                                HDDEDATA hdata, ULONG_PTR dwData1, ULONG_PTR dwData2)
101 {
102     DWORD               d1 = 0;
103     HDDEDATA            ret;
104     CONVCONTEXT16       cc16;
105     WORD args[16];
106
107     switch (uType)
108     {
109     case XTYP_CONNECT:
110     case XTYP_WILDCONNECT:
111         if (dwData1)
112         {
113             map3216_conv_context(&cc16, (const CONVCONTEXT*)dwData1);
114             d1 = MapLS(&cc16);
115         }
116         break;
117     default:
118         d1 = dwData1;
119         break;
120     }
121     args[15] = HIWORD(uType);
122     args[14] = LOWORD(uType);
123     args[13] = HIWORD(uFmt);
124     args[12] = LOWORD(uFmt);
125     args[11] = HIWORD(hConv);
126     args[10] = LOWORD(hConv);
127     args[9]  = HIWORD(hsz1);
128     args[8]  = LOWORD(hsz1);
129     args[7]  = HIWORD(hsz2);
130     args[6]  = LOWORD(hsz2);
131     args[5]  = HIWORD(hdata);
132     args[4]  = LOWORD(hdata);
133     args[3]  = HIWORD(d1);
134     args[2]  = LOWORD(d1);
135     args[1]  = HIWORD(dwData2);
136     args[0]  = LOWORD(dwData2);
137     WOWCallback16Ex(pfn16, WCB16_PASCAL, sizeof(args), args, (DWORD *)&ret);
138
139     switch (uType)
140     {
141     case XTYP_CONNECT:
142     case XTYP_WILDCONNECT:
143         if (d1 != 0) UnMapLS(d1);
144         break;
145     }
146     return ret;
147 }
148
149 #define MAX_THUNKS      32
150 /* As DDEML doesn't provide a way to get back to an InstanceID when
151  * a callback is run, we use thunk in order to implement simply the
152  * 32bit->16bit callback mechanism.
153  * For each 16bit instance, we create a thunk, which will be passed as
154  * a 32bit callback. This thunk also stores (in the code!) the 16bit
155  * address of the 16bit callback, and passes it back to
156  * WDML_InvokeCallback16.
157  * The code below is mainly to create the thunks themselves
158  */
159 #include "pshpack1.h"
160 static struct ddeml_thunk
161 {
162     BYTE        popl_eax;        /* popl  %eax (return address) */
163     BYTE        pushl_func;      /* pushl $pfn16 (16bit callback function) */
164     SEGPTR      pfn16;
165     BYTE        pushl_eax;       /* pushl %eax */
166     BYTE        jmp;             /* ljmp WDML_InvokeCallback16 */
167     DWORD       callback;
168     DWORD       instId;          /* instance ID */
169 } *DDEML16_Thunks;
170 #include "poppack.h"
171
172 static CRITICAL_SECTION ddeml_cs;
173 static CRITICAL_SECTION_DEBUG critsect_debug =
174 {
175     0, 0, &ddeml_cs,
176     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
177       0, 0, { (DWORD_PTR)(__FILE__ ": ddeml_cs") }
178 };
179 static CRITICAL_SECTION ddeml_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
180
181 static struct ddeml_thunk*      DDEML_AddThunk(DWORD instId, DWORD pfn16)
182 {
183     struct ddeml_thunk* thunk;
184
185     if (!DDEML16_Thunks)
186     {
187         DDEML16_Thunks = VirtualAlloc(NULL, MAX_THUNKS * sizeof(*DDEML16_Thunks), MEM_COMMIT,
188                                       PAGE_EXECUTE_READWRITE);
189         if (!DDEML16_Thunks) return NULL;
190         for (thunk = DDEML16_Thunks; thunk < &DDEML16_Thunks[MAX_THUNKS]; thunk++)
191         {
192             thunk->popl_eax     = 0x58;   /* popl  %eax */
193             thunk->pushl_func   = 0x68;   /* pushl $pfn16 */
194             thunk->pfn16        = 0;
195             thunk->pushl_eax    = 0x50;   /* pushl %eax */
196             thunk->jmp          = 0xe9;   /* jmp WDML_InvokeCallback16 */
197             thunk->callback     = (char *)WDML_InvokeCallback16 - (char *)(&thunk->callback + 1);
198             thunk->instId       = 0;
199         }
200     }
201     for (thunk = DDEML16_Thunks; thunk < &DDEML16_Thunks[MAX_THUNKS]; thunk++)
202     {
203         /* either instId is 0, and we're looking for an empty slot, or
204          * instId is an already existing instance, and we should find its thunk
205          */
206         if (thunk->instId == instId)
207         {
208             thunk->pfn16 = pfn16;
209             return thunk;
210         }
211     }
212     FIXME("Out of ddeml-thunks. Bump MAX_THUNKS\n");
213     return NULL;
214 }
215
216 /******************************************************************************
217  *            DdeInitialize   (DDEML.2)
218  */
219 UINT16 WINAPI DdeInitialize16(LPDWORD pidInst, PFNCALLBACK16 pfnCallback,
220                               DWORD afCmd, DWORD ulRes)
221 {
222     UINT16 ret;
223     struct ddeml_thunk* thunk;
224
225     EnterCriticalSection(&ddeml_cs);
226     if ((thunk = DDEML_AddThunk(*pidInst, (DWORD)pfnCallback)))
227     {
228         ret = DdeInitializeA(pidInst, (PFNCALLBACK)thunk, afCmd, ulRes);
229         if (ret == DMLERR_NO_ERROR) thunk->instId = *pidInst;
230     }
231     else ret = DMLERR_SYS_ERROR;
232     LeaveCriticalSection(&ddeml_cs);
233     return ret;
234 }
235
236 /*****************************************************************
237  *            DdeUninitialize   (DDEML.3)
238  */
239 BOOL16 WINAPI DdeUninitialize16(DWORD idInst)
240 {
241     struct ddeml_thunk* thunk;
242     BOOL16              ret = FALSE;
243
244     if (!DdeUninitialize(idInst)) return FALSE;
245     EnterCriticalSection(&ddeml_cs);
246     for (thunk = DDEML16_Thunks; thunk < &DDEML16_Thunks[MAX_THUNKS]; thunk++)
247     {
248         if (thunk->instId == idInst)
249         {
250             thunk->instId = 0;
251             ret = TRUE;
252             break;
253         }
254     }
255     LeaveCriticalSection(&ddeml_cs);
256     if (!ret) FIXME("Should never happen\n");
257     return ret;
258 }
259
260 /*****************************************************************
261  * DdeConnectList [DDEML.4]
262  */
263
264 HCONVLIST WINAPI DdeConnectList16(DWORD idInst, HSZ hszService, HSZ hszTopic,
265                                   HCONVLIST hConvList, LPCONVCONTEXT16 pCC16)
266 {
267     CONVCONTEXT         cc;
268     CONVCONTEXT*        pCC = NULL;
269
270     if (pCC16)
271         map1632_conv_context(pCC = &cc, pCC16);
272     return DdeConnectList(idInst, hszService, hszTopic, hConvList, pCC);
273 }
274
275 /*****************************************************************
276  * DdeQueryNextServer [DDEML.5]
277  */
278 HCONV WINAPI DdeQueryNextServer16(HCONVLIST hConvList, HCONV hConvPrev)
279 {
280     return DdeQueryNextServer(hConvList, hConvPrev);
281 }
282
283 /*****************************************************************
284  *            DdeDisconnectList (DDEML.6)
285  */
286 BOOL16 WINAPI DdeDisconnectList16(HCONVLIST hConvList)
287 {
288     return (BOOL16)DdeDisconnectList(hConvList);
289 }
290
291
292 /*****************************************************************
293  *              DdeQueryString (DDEML.23)
294  */
295 DWORD WINAPI DdeQueryString16(DWORD idInst, HSZ hsz, LPSTR lpsz, DWORD cchMax,
296                               INT16 codepage)
297 {
298     return DdeQueryStringA(idInst, hsz, lpsz, cchMax, codepage);
299 }
300
301 /*****************************************************************
302  *            DdeConnect   (DDEML.7)
303  */
304 HCONV WINAPI DdeConnect16(DWORD idInst, HSZ hszService, HSZ hszTopic,
305                           LPCONVCONTEXT16 pCC16)
306 {
307     CONVCONTEXT         cc;
308     CONVCONTEXT*        pCC = NULL;
309
310     if (pCC16)
311         map1632_conv_context(pCC = &cc, pCC16);
312     return DdeConnect(idInst, hszService, hszTopic, pCC);
313 }
314
315 /*****************************************************************
316  *            DdeDisconnect   (DDEML.8)
317  */
318 BOOL16 WINAPI DdeDisconnect16(HCONV hConv)
319 {
320     return (BOOL16)DdeDisconnect(hConv);
321 }
322
323 /*****************************************************************
324  *            DdeSetUserHandle (DDEML.10)
325  */
326 BOOL16 WINAPI DdeSetUserHandle16(HCONV hConv, DWORD id, DWORD hUser)
327 {
328     return DdeSetUserHandle(hConv, id, hUser);
329 }
330
331 /*****************************************************************
332  *            DdeCreateDataHandle (DDEML.14)
333  */
334 HDDEDATA WINAPI DdeCreateDataHandle16(DWORD idInst, LPBYTE pSrc, DWORD cb,
335                                       DWORD cbOff, HSZ hszItem, UINT16 wFmt,
336                                       UINT16 afCmd)
337 {
338     return DdeCreateDataHandle(idInst, pSrc, cb, cbOff, hszItem, wFmt, afCmd);
339 }
340
341 /*****************************************************************
342  *            DdeCreateStringHandle   (DDEML.21)
343  */
344 HSZ WINAPI DdeCreateStringHandle16(DWORD idInst, LPCSTR str, INT16 codepage)
345 {
346     if  (codepage)
347     {
348         return DdeCreateStringHandleA(idInst, str, codepage);
349     }
350     else
351     {
352         TRACE("Default codepage supplied\n");
353         return DdeCreateStringHandleA(idInst, str, CP_WINANSI);
354     }
355 }
356
357 /*****************************************************************
358  *            DdeFreeStringHandle   (DDEML.22)
359  */
360 BOOL16 WINAPI DdeFreeStringHandle16(DWORD idInst, HSZ hsz)
361 {
362     return (BOOL16)DdeFreeStringHandle(idInst, hsz);
363 }
364
365 /*****************************************************************
366  *            DdeFreeDataHandle   (DDEML.19)
367  */
368 BOOL16 WINAPI DdeFreeDataHandle16(HDDEDATA hData)
369 {
370     return (BOOL16)DdeFreeDataHandle(hData);
371 }
372
373 /*****************************************************************
374  *            DdeKeepStringHandle   (DDEML.24)
375  */
376 BOOL16 WINAPI DdeKeepStringHandle16(DWORD idInst, HSZ hsz)
377 {
378     return DdeKeepStringHandle(idInst, hsz);
379 }
380
381 /*****************************************************************
382  *            DdeClientTransaction  (DDEML.11)
383  */
384 HDDEDATA WINAPI DdeClientTransaction16(LPVOID pData, DWORD cbData, HCONV hConv,
385                                        HSZ hszItem, UINT16 wFmt, UINT16 wType,
386                                        DWORD dwTimeout, LPDWORD pdwResult)
387 {
388     if (cbData != (DWORD)-1)
389     {
390         /* pData is not a pointer if cbData is -1, so we linearize the address
391          * here rather than in the calling code. */
392         pData = MapSL((SEGPTR)pData);
393     }
394     return DdeClientTransaction(pData, cbData, hConv, hszItem,
395                                 wFmt, wType, dwTimeout, pdwResult);
396 }
397
398 /*****************************************************************
399  *
400  *            DdeAbandonTransaction (DDEML.12)
401  *
402  */
403 BOOL16 WINAPI DdeAbandonTransaction16(DWORD idInst, HCONV hConv, DWORD idTransaction)
404 {
405     return (BOOL16)DdeAbandonTransaction(idInst, hConv, idTransaction);
406 }
407
408 /*****************************************************************
409  * DdePostAdvise [DDEML.13]
410  */
411 BOOL16 WINAPI DdePostAdvise16(DWORD idInst, HSZ hszTopic, HSZ hszItem)
412 {
413     return (BOOL16)DdePostAdvise(idInst, hszTopic, hszItem);
414 }
415
416 /*****************************************************************
417  *            DdeAddData (DDEML.15)
418  */
419 HDDEDATA WINAPI DdeAddData16(HDDEDATA hData, LPBYTE pSrc, DWORD cb, DWORD cbOff)
420 {
421     return DdeAddData(hData, pSrc, cb, cbOff);
422 }
423
424 /*****************************************************************
425  * DdeGetData [DDEML.16]
426  */
427 DWORD WINAPI DdeGetData16(HDDEDATA hData, LPBYTE pDst, DWORD cbMax, DWORD cbOff)
428 {
429     return DdeGetData(hData, pDst, cbMax, cbOff);
430 }
431
432 /*****************************************************************
433  *            DdeAccessData (DDEML.17)
434  */
435 LPBYTE WINAPI DdeAccessData16(HDDEDATA hData, LPDWORD pcbDataSize)
436 {
437     FIXME("expect trouble\n");
438     /* FIXME: there's a memory leak here... */
439     return (LPBYTE)MapLS(DdeAccessData(hData, pcbDataSize));
440 }
441
442 /*****************************************************************
443  *            DdeUnaccessData (DDEML.18)
444  */
445 BOOL16 WINAPI DdeUnaccessData16(HDDEDATA hData)
446 {
447     return DdeUnaccessData(hData);
448 }
449
450 /*****************************************************************
451  *            DdeEnableCallback (DDEML.26)
452  */
453 BOOL16 WINAPI DdeEnableCallback16(DWORD idInst, HCONV hConv, UINT16 wCmd)
454 {
455     return DdeEnableCallback(idInst, hConv, wCmd);
456 }
457
458 /*****************************************************************
459  *            DdeNameService  (DDEML.27)
460  */
461 HDDEDATA WINAPI DdeNameService16(DWORD idInst, HSZ hsz1, HSZ hsz2, UINT16 afCmd)
462 {
463     return DdeNameService(idInst, hsz1, hsz2, afCmd);
464 }
465
466 /*****************************************************************
467  *            DdeGetLastError  (DDEML.20)
468  */
469 UINT16 WINAPI DdeGetLastError16(DWORD idInst)
470 {
471     return (UINT16)DdeGetLastError(idInst);
472 }
473
474 /*****************************************************************
475  *            DdeCmpStringHandles (DDEML.36)
476  */
477 INT16 WINAPI DdeCmpStringHandles16(HSZ hsz1, HSZ hsz2)
478 {
479     return DdeCmpStringHandles(hsz1, hsz2);
480 }
481
482 /******************************************************************
483  *              DdeQueryConvInfo (DDEML.9)
484  *
485  */
486 UINT16 WINAPI DdeQueryConvInfo16(HCONV hConv, DWORD idTransaction,
487                                  LPCONVINFO16 lpConvInfo)
488 {
489     CONVINFO    ci32;
490     CONVINFO16  ci16;
491     UINT        ret;
492
493     ci32.cb = sizeof(ci32);
494     ci32.ConvCtxt.cb = sizeof(ci32.ConvCtxt);
495
496     ret = DdeQueryConvInfo(hConv, idTransaction, &ci32);
497     if (ret == 0) return 0;
498
499     ci16.hUser = ci32.hUser;
500     ci16.hConvPartner = ci32.hConvPartner;
501     ci16.hszSvcPartner = ci32.hszSvcPartner;
502     ci16.hszServiceReq = ci32.hszServiceReq;
503     ci16.hszTopic = ci32.hszTopic;
504     ci16.hszItem = ci32.hszItem;
505     ci16.wFmt = ci32.wFmt;
506     ci16.wType = ci32.wType;
507     ci16.wStatus = ci32.wStatus;
508     ci16.wConvst = ci32.wConvst;
509     ci16.wLastError = ci32.wLastError;
510     ci16.hConvList = ci32.hConvList;
511
512     map3216_conv_context(&ci16.ConvCtxt, &ci32.ConvCtxt);
513
514     memcpy(lpConvInfo, &ci16, lpConvInfo->cb);
515     return lpConvInfo->cb;
516 }