From f93d65a6e5dc87784e064444e3245d0504cdd9ea Mon Sep 17 00:00:00 2001
From: Sebastian Lackner <sebastian@fds-team.de>
Date: Sun, 15 Nov 2015 04:25:27 +0100
Subject: oleaut32: Initial preparation to make marshalling compatible with
 x86_64.

---
 dlls/oleaut32/tmarshal.c | 94 ++++++++++++++++++++++++++----------------------
 dlls/oleaut32/typelib.c  |  8 ++---
 dlls/oleaut32/typelib.h  |  2 +-
 3 files changed, 57 insertions(+), 47 deletions(-)

diff --git a/dlls/oleaut32/tmarshal.c b/dlls/oleaut32/tmarshal.c
index f4ce311..c5aa374 100644
--- a/dlls/oleaut32/tmarshal.c
+++ b/dlls/oleaut32/tmarshal.c
@@ -633,42 +633,51 @@ static const IRpcProxyBufferVtbl tmproxyvtable = {
     TMProxyImpl_Disconnect
 };
 
-/* how much space do we use on stack in DWORD steps. */
+/* how much space do we use on stack in DWORD_PTR steps. */
 static int
 _argsize(TYPEDESC *tdesc, ITypeInfo *tinfo) {
+    DWORD ret;
     switch (tdesc->vt) {
     case VT_I8:
     case VT_UI8:
-	return 8/sizeof(DWORD);
+        ret = 8;
+        break;
     case VT_R8:
-        return sizeof(double)/sizeof(DWORD);
+        ret = sizeof(double);
+        break;
     case VT_CY:
-        return sizeof(CY)/sizeof(DWORD);
+        ret = sizeof(CY);
+        break;
     case VT_DATE:
-	return sizeof(DATE)/sizeof(DWORD);
+        ret = sizeof(DATE);
+        break;
     case VT_DECIMAL:
-        return (sizeof(DECIMAL)+3)/sizeof(DWORD);
+        ret = sizeof(DECIMAL);
+        break;
     case VT_VARIANT:
-	return (sizeof(VARIANT)+3)/sizeof(DWORD);
+        ret = sizeof(VARIANT);
+        break;
     case VT_USERDEFINED:
     {
         ITypeInfo *tinfo2;
         TYPEATTR *tattr;
         HRESULT hres;
-        DWORD ret;
 
         hres = ITypeInfo_GetRefTypeInfo(tinfo,tdesc->u.hreftype,&tinfo2);
         if (FAILED(hres))
             return 0; /* should fail critically in serialize_param */
         ITypeInfo_GetTypeAttr(tinfo2,&tattr);
-        ret = (tattr->cbSizeInstance+3)/sizeof(DWORD);
+        ret = tattr->cbSizeInstance;
         ITypeInfo_ReleaseTypeAttr(tinfo2, tattr);
         ITypeInfo_Release(tinfo2);
-        return ret;
+        break;
     }
     default:
-	return 1;
+        ret = sizeof(DWORD_PTR);
+        break;
     }
+
+    return (ret + sizeof(DWORD_PTR) - 1) / sizeof(DWORD_PTR);
 }
 
 /* how much space do we use on the heap (in bytes) */
@@ -717,7 +726,7 @@ _xsize(const TYPEDESC *td, ITypeInfo *tinfo) {
         return ret;
     }
     default:
-	return 4;
+	return sizeof(DWORD_PTR);
     }
 }
 
@@ -736,7 +745,7 @@ serialize_param(
     BOOL		debugout,
     BOOL		dealloc,
     TYPEDESC		*tdesc,
-    DWORD		*arg,
+    DWORD_PTR		*arg,
     marshal_state	*buf)
 {
     HRESULT hres = S_OK;
@@ -755,7 +764,7 @@ serialize_param(
     case VT_R8:
     case VT_CY:
 	hres = S_OK;
-	if (debugout) TRACE_(olerelay)("%x%x\n",arg[0],arg[1]);
+	if (debugout) TRACE_(olerelay)("%s\n", wine_dbgstr_longlong(*(ULONGLONG *)arg));
 	if (writeit)
 	    hres = xbuf_add(buf,(LPBYTE)arg,8);
 	return hres;
@@ -766,7 +775,7 @@ serialize_param(
     case VT_R4:
     case VT_UI4:
 	hres = S_OK;
-	if (debugout) TRACE_(olerelay)("%x\n",*arg);
+	if (debugout) TRACE_(olerelay)("%x\n", *(DWORD *)arg);
 	if (writeit)
 	    hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
 	return hres;
@@ -774,14 +783,14 @@ serialize_param(
     case VT_UI2:
     case VT_BOOL:
 	hres = S_OK;
-	if (debugout) TRACE_(olerelay)("%04x\n",*arg & 0xffff);
+	if (debugout) TRACE_(olerelay)("%04x\n", *(WORD *)arg);
 	if (writeit)
 	    hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
 	return hres;
     case VT_I1:
     case VT_UI1:
 	hres = S_OK;
-	if (debugout) TRACE_(olerelay)("%02x\n",*arg & 0xff);
+	if (debugout) TRACE_(olerelay)("%02x\n", *(BYTE *)arg);
 	if (writeit)
 	    hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
 	return hres;
@@ -881,19 +890,19 @@ serialize_param(
 	    if (debugout) TRACE_(olerelay)("NULL");
 	    return S_OK;
 	}
-	hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD*)*arg,buf);
+	hres = serialize_param(tinfo,writeit,debugout,dealloc,tdesc->u.lptdesc,(DWORD_PTR *)*arg,buf);
 	if (derefhere && dealloc) HeapFree(GetProcessHeap(),0,(LPVOID)*arg);
 	return hres;
     }
     case VT_UNKNOWN:
-	if (debugout) TRACE_(olerelay)("unk(0x%x)",*arg);
+	if (debugout) TRACE_(olerelay)("unk(0x%lx)", *arg);
 	if (writeit)
 	    hres = _marshal_interface(buf,&IID_IUnknown,(LPUNKNOWN)*arg);
 	if (dealloc && *(IUnknown **)arg)
 	    IUnknown_Release((LPUNKNOWN)*arg);
 	return hres;
     case VT_DISPATCH:
-	if (debugout) TRACE_(olerelay)("idisp(0x%x)",*arg);
+	if (debugout) TRACE_(olerelay)("idisp(0x%lx)", *arg);
 	if (writeit)
 	    hres = _marshal_interface(buf,&IID_IDispatch,(LPUNKNOWN)*arg);
 	if (dealloc && *(IUnknown **)arg)
@@ -941,7 +950,7 @@ serialize_param(
 		    debugout,
 		    dealloc,
 		    tdesc2,
-		    (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
+		    (DWORD_PTR *)(((LPBYTE)arg)+vdesc->u.oInst),
 		    buf
 		);
                 ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
@@ -958,7 +967,7 @@ serialize_param(
 	    break;
 	case TKIND_ENUM:
 	    hres = S_OK;
-	    if (debugout) TRACE_(olerelay)("%x",*arg);
+	    if (debugout) TRACE_(olerelay)("%x", *(DWORD *)arg);
 	    if (writeit)
 	        hres = xbuf_add(buf,(LPBYTE)arg,sizeof(DWORD));
 	    break;
@@ -984,7 +993,7 @@ serialize_param(
 	if (debugout) TRACE_(olerelay)("[");
 	for (i=0;i<arrsize;i++) {
             LPBYTE base = _passbyref(&adesc->tdescElem, tinfo) ? (LPBYTE) *arg : (LPBYTE) arg;
-	    hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD*)((LPBYTE)base+i*_xsize(&adesc->tdescElem, tinfo)), buf);
+	    hres = serialize_param(tinfo, writeit, debugout, dealloc, &adesc->tdescElem, (DWORD_PTR *)((LPBYTE)base+i*_xsize(&adesc->tdescElem, tinfo)), buf);
 	    if (hres)
 		return hres;
 	    if (debugout && (i<arrsize-1)) TRACE_(olerelay)(",");
@@ -1023,7 +1032,7 @@ deserialize_param(
     BOOL		debugout,
     BOOL		alloc,
     TYPEDESC		*tdesc,
-    DWORD		*arg,
+    DWORD_PTR		*arg,
     marshal_state	*buf)
 {
     HRESULT hres = S_OK;
@@ -1056,7 +1065,7 @@ deserialize_param(
 		hres = xbuf_get(buf,(LPBYTE)arg,8);
 		if (hres) ERR("Failed to read integer 8 byte\n");
 	    }
-	    if (debugout) TRACE_(olerelay)("%x%x",arg[0],arg[1]);
+	    if (debugout) TRACE_(olerelay)("%s", wine_dbgstr_longlong(*(ULONGLONG *)arg));
 	    return hres;
         case VT_ERROR:
         case VT_I4:
@@ -1068,7 +1077,7 @@ deserialize_param(
 		hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
 		if (hres) ERR("Failed to read integer 4 byte\n");
 	    }
-	    if (debugout) TRACE_(olerelay)("%x",*arg);
+	    if (debugout) TRACE_(olerelay)("%x", *(DWORD *)arg);
 	    return hres;
         case VT_I2:
         case VT_UI2:
@@ -1079,7 +1088,7 @@ deserialize_param(
 		if (hres) ERR("Failed to read integer 4 byte\n");
 		memcpy(arg,&x,2);
 	    }
-	    if (debugout) TRACE_(olerelay)("%04x",*arg & 0xffff);
+	    if (debugout) TRACE_(olerelay)("%04x", *(WORD *)arg);
 	    return hres;
         case VT_I1:
 	case VT_UI1:
@@ -1089,7 +1098,7 @@ deserialize_param(
 		if (hres) ERR("Failed to read integer 4 byte\n");
 		memcpy(arg,&x,1);
 	    }
-	    if (debugout) TRACE_(olerelay)("%02x",*arg & 0xff);
+	    if (debugout) TRACE_(olerelay)("%02x", *(BYTE *)arg);
 	    return hres;
 	case VT_BSTR: {
 	    if (readit)
@@ -1165,17 +1174,17 @@ deserialize_param(
 	    if (alloc) {
 		/* Allocate space for the referenced struct */
 		if (derefhere)
-		    *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo));
+		    *arg=(DWORD_PTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo));
 	    }
 	    if (derefhere)
-		return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (LPDWORD)*arg, buf);
+		return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, (DWORD_PTR *)*arg, buf);
 	    else
 		return deserialize_param(tinfo, readit, debugout, alloc, tdesc->u.lptdesc, arg, buf);
         }
 	case VT_UNKNOWN:
 	    /* FIXME: UNKNOWN is unknown ..., but allocate 4 byte for it */
 	    if (alloc)
-	        *arg=(DWORD)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD));
+	        *arg=(DWORD_PTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DWORD_PTR));
 	    hres = S_OK;
 	    if (readit)
 		hres = _unmarshal_interface(buf,&IID_IUnknown,(LPUNKNOWN*)arg);
@@ -1231,7 +1240,7 @@ deserialize_param(
 			    debugout,
 			    alloc,
 			    &vdesc->elemdescVar.tdesc,
-			    (DWORD*)(((LPBYTE)arg)+vdesc->u.oInst),
+			    (DWORD_PTR *)(((LPBYTE)arg)+vdesc->u.oInst),
 			    buf
 			);
                         ITypeInfo_ReleaseVarDesc(tinfo2, vdesc);
@@ -1248,7 +1257,7 @@ deserialize_param(
 		        hres = xbuf_get(buf,(LPBYTE)arg,sizeof(DWORD));
 		        if (hres) ERR("Failed to read enum (4 byte)\n");
 		    }
-		    if (debugout) TRACE_(olerelay)("%x",*arg);
+		    if (debugout) TRACE_(olerelay)("%x", *(DWORD *)arg);
 		    break;
 		default:
 		    ERR("Unhandled typekind %d\n",tattr->typekind);
@@ -1274,7 +1283,7 @@ deserialize_param(
             if (_passbyref(&adesc->tdescElem, tinfo))
             {
 	        base = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,_xsize(tdesc->u.lptdesc, tinfo) * arrsize);
-                *arg = (DWORD) base;
+                *arg = (DWORD_PTR)base;
             }
 	    for (i=0;i<arrsize;i++)
 		deserialize_param(
@@ -1283,7 +1292,7 @@ deserialize_param(
 		    debugout,
 		    alloc,
 		    &adesc->tdescElem,
-		    (DWORD*)(base + i*_xsize(&adesc->tdescElem, tinfo)),
+		    (DWORD_PTR *)(base + i*_xsize(&adesc->tdescElem, tinfo)),
 		    buf
 		);
 	    return S_OK;
@@ -1413,7 +1422,7 @@ static inline BOOL is_out_elem(const ELEMDESC *elem)
 static DWORD WINAPI xCall(int method, void **args)
 {
     TMProxyImpl *tpinfo = args[0];
-    DWORD *xargs;
+    DWORD_PTR *xargs;
     const FUNCDESC	*fdesc;
     HRESULT		hres;
     int			i;
@@ -1473,7 +1482,7 @@ static DWORD WINAPI xCall(int method, void **args)
     if (nrofnames > sizeof(names)/sizeof(names[0]))
 	ERR("Need more names!\n");
 
-    xargs = (DWORD *)(args + 1);
+    xargs = (DWORD_PTR *)(args + 1);
     for (i=0;i<fdesc->cParams;i++) {
 	ELEMDESC	*elem = fdesc->lprgelemdescParam+i;
 	if (TRACE_ON(olerelay)) {
@@ -1540,7 +1549,7 @@ static DWORD WINAPI xCall(int method, void **args)
     buf.curoff = 0;
 
     /* generic deserializer using typelib description */
-    xargs = (DWORD *)(args + 1);
+    xargs = (DWORD_PTR *)(args + 1);
     status = S_OK;
     for (i=0;i<fdesc->cParams;i++) {
 	ELEMDESC	*elem = fdesc->lprgelemdescParam+i;
@@ -2067,7 +2076,8 @@ TMStubImpl_Invoke(
     const FUNCDESC *fdesc;
     TMStubImpl *This = impl_from_IRpcStubBuffer(iface);
     HRESULT	hres;
-    DWORD	*args = NULL, res, *xargs, nrofargs;
+    DWORD_PTR   *args = NULL, *xargs;
+    DWORD	res, nrofargs;
     marshal_state	buf;
     UINT	nrofnames = 0;
     BSTR	names[10];
@@ -2132,7 +2142,7 @@ TMStubImpl_Invoke(
     nrofargs = 0;
     for (i=0;i<fdesc->cParams;i++)
 	nrofargs += _argsize(&fdesc->lprgelemdescParam[i].tdesc, tinfo);
-    args = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(nrofargs+1)*sizeof(DWORD));
+    args = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nrofargs+1)*sizeof(DWORD_PTR));
     if (!args)
     {
         hres = E_OUTOFMEMORY;
@@ -2160,12 +2170,12 @@ TMStubImpl_Invoke(
 	}
     }
 
-    args[0] = (DWORD)This->pUnk;
+    args[0] = (DWORD_PTR)This->pUnk;
 
     __TRY
     {
         res = _invoke(
-            (*((FARPROC**)args[0]))[fdesc->oVft/4],
+            (*((FARPROC**)args[0]))[fdesc->oVft / sizeof(DWORD_PTR)],
             fdesc->callconv,
             (xargs-args),
             args
diff --git a/dlls/oleaut32/typelib.c b/dlls/oleaut32/typelib.c
index 0891098..97f40bc 100644
--- a/dlls/oleaut32/typelib.c
+++ b/dlls/oleaut32/typelib.c
@@ -6402,15 +6402,15 @@ static double (* const call_double_method)(void*,int,const DWORD*,int*) = (void
  * Invokes a method, or accesses a property of an object, that implements the
  * interface described by the type description.
  */
-DWORD
-_invoke(FARPROC func,CALLCONV callconv, int nrargs, DWORD *args) {
+DWORD _invoke(FARPROC func, CALLCONV callconv, int nrargs, DWORD_PTR *args)
+{
     DWORD res;
     int stack_offset;
 
     if (TRACE_ON(ole)) {
 	int i;
 	TRACE("Calling %p(",func);
-	for (i=0;i<min(nrargs,30);i++) TRACE("%08x,",args[i]);
+	for (i=0;i<min(nrargs,30);i++) TRACE("%08lx,",args[i]);
 	if (nrargs > 30) TRACE("...");
 	TRACE(")\n");
     }
@@ -6418,7 +6418,7 @@ _invoke(FARPROC func,CALLCONV callconv, int nrargs, DWORD *args) {
     switch (callconv) {
     case CC_STDCALL:
     case CC_CDECL:
-        res = call_method( func, nrargs, args, &stack_offset );
+        res = call_method(func, nrargs, (DWORD *)args, &stack_offset);
 	break;
     default:
 	FIXME("unsupported calling convention %d\n",callconv);
diff --git a/dlls/oleaut32/typelib.h b/dlls/oleaut32/typelib.h
index 8f274ba..5ecb75a 100644
--- a/dlls/oleaut32/typelib.h
+++ b/dlls/oleaut32/typelib.h
@@ -604,7 +604,7 @@ extern void  heap_free(void *ptr) DECLSPEC_HIDDEN;
 
 HRESULT ITypeInfoImpl_GetInternalFuncDesc( ITypeInfo *iface, UINT index, const FUNCDESC **ppFuncDesc ) DECLSPEC_HIDDEN;
 
-extern DWORD _invoke(FARPROC func,CALLCONV callconv, int nrargs, DWORD *args) DECLSPEC_HIDDEN;
+extern DWORD _invoke(FARPROC func, CALLCONV callconv, int nrargs, DWORD_PTR *args) DECLSPEC_HIDDEN;
 
 HRESULT TMARSHAL_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv) DECLSPEC_HIDDEN;
 
-- 
2.6.2

