Another portion of HeapReAlloc fixes.

diff --git a/dlls/dsound/buffer.c b/dlls/dsound/buffer.c
index f248228..b68054b 100644
--- a/dlls/dsound/buffer.c
+++ b/dlls/dsound/buffer.c
@@ -121,8 +121,13 @@
 	} else {
 	    /* Make an internal copy of the caller-supplied array.
 	     * Replace the existing copy if one is already present. */
-	    This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
-	        This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
+	    if (This->dsb->notifies) 
+		    This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
+			This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
+	    else
+		    This->dsb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
+			howmuch * sizeof(DSBPOSITIONNOTIFY));
+
 	    if (This->dsb->notifies == NULL) {
 		    WARN("out of memory\n");
 		    return DSERR_OUTOFMEMORY;
@@ -1163,7 +1168,12 @@
 	/* register buffer */
 	RtlAcquireResourceExclusive(&(ds->lock), TRUE);
 	if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
-		IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,ds->buffers,sizeof(IDirectSoundBufferImpl*)*(ds->nrofbuffers+1));
+		IDirectSoundBufferImpl **newbuffers;
+		if (ds->buffers)
+			newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,ds->buffers,sizeof(IDirectSoundBufferImpl*)*(ds->nrofbuffers+1));
+		else
+			newbuffers = (IDirectSoundBufferImpl**)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSoundBufferImpl*)*(ds->nrofbuffers+1));
+
 		if (newbuffers) {
 			ds->buffers = newbuffers;
 			ds->buffers[ds->nrofbuffers] = dsb;
diff --git a/dlls/dsound/capture.c b/dlls/dsound/capture.c
index 3948882..a7a46da 100644
--- a/dlls/dsound/capture.c
+++ b/dlls/dsound/capture.c
@@ -757,8 +757,10 @@
 
 	    buflen = lpcDSCBufferDesc->dwBufferBytes;
             TRACE("desired buflen=%ld, old buffer=%p\n", buflen, ipDSC->buffer);
-            newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,ipDSC->buffer,buflen);
-
+	    if (ipDSC->buffer)
+                newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,ipDSC->buffer,buflen);
+	    else
+		newbuf = (LPBYTE)HeapAlloc(GetProcessHeap(),0,buflen);	    
             if (newbuf == NULL) {
                 WARN("failed to allocate capture buffer\n");
                 err = DSERR_OUTOFMEMORY;
@@ -850,8 +852,13 @@
     } else {
 	/* Make an internal copy of the caller-supplied array.
 	 * Replace the existing copy if one is already present. */
-	This->dscb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
-	    This->dscb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
+	if (This->dscb->notifies)
+	    This->dscb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
+		This->dscb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
+	else
+	    This->dscb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
+		howmuch * sizeof(DSBPOSITIONNOTIFY));
+
 	if (This->dscb->notifies == NULL) {
 	    WARN("out of memory\n");
 	    return DSERR_OUTOFMEMORY;
@@ -1336,8 +1343,12 @@
 		TRACE("nrofnotifies=%d\n", This->nrofnotifies);
 
                 /* prepare headers */
-                ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,
-                    ipDSC->nrofpwaves*sizeof(WAVEHDR));
+		if (ipDSC->pwave)
+                    ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,
+	                ipDSC->nrofpwaves*sizeof(WAVEHDR));
+		else 
+                    ipDSC->pwave = HeapAlloc(GetProcessHeap(),0,
+	                ipDSC->nrofpwaves*sizeof(WAVEHDR));
 
                 for (c = 0; c < ipDSC->nrofpwaves; c++) {
                     if (c == 0) {
@@ -1379,7 +1390,11 @@
 		TRACE("no notifiers specified\n");
 		/* no notifiers specified so just create a single default header */
 		ipDSC->nrofpwaves = 1;
-                ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,sizeof(WAVEHDR));
+		if (ipDSC->pwave)
+                    ipDSC->pwave = HeapReAlloc(GetProcessHeap(),0,ipDSC->pwave,sizeof(WAVEHDR));
+		else
+                    ipDSC->pwave = HeapAlloc(GetProcessHeap(),0,sizeof(WAVEHDR));
+
                 ipDSC->pwave[0].lpData = ipDSC->buffer;
                 ipDSC->pwave[0].dwBufferLength = ipDSC->buflen; 
                 ipDSC->pwave[0].dwUser = (DWORD)ipDSC;
diff --git a/dlls/dsound/dsound_main.c b/dlls/dsound/dsound_main.c
index 1470a73..67caa89 100644
--- a/dlls/dsound/dsound_main.c
+++ b/dlls/dsound/dsound_main.c
@@ -623,7 +623,12 @@
 	/* register buffer */
 	RtlAcquireResourceExclusive(&(This->lock), TRUE);
 	{
-		IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1));
+		IDirectSoundBufferImpl **newbuffers;
+		if (This->buffers)
+    			newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1));
+		else
+    			newbuffers = (IDirectSoundBufferImpl**)HeapAlloc(GetProcessHeap(),0,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1));
+
 		if (newbuffers) {
 			This->buffers = newbuffers;
 			This->buffers[This->nrofbuffers] = dsb;
diff --git a/dlls/dsound/primary.c b/dlls/dsound/primary.c
index 7589f8c..f6a3368 100644
--- a/dlls/dsound/primary.c
+++ b/dlls/dsound/primary.c
@@ -90,7 +90,12 @@
 		buflen = ((This->wfx.nAvgBytesPerSec / 100) & ~3) * DS_HEL_FRAGS;
 		TRACE("desired buflen=%ld, old buffer=%p\n", buflen, This->buffer);
 		/* reallocate emulated primary buffer */
-		newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,This->buffer,buflen);
+
+		if (This->buffer)
+			newbuf = (LPBYTE)HeapReAlloc(GetProcessHeap(),0,This->buffer,buflen);
+		else
+			newbuf = (LPBYTE)HeapAlloc(GetProcessHeap(),0,buflen);
+
 		if (newbuf == NULL) {
 			ERR("failed to allocate primary buffer\n");
 			merr = DSERR_OUTOFMEMORY;
diff --git a/dlls/kernel/editline.c b/dlls/kernel/editline.c
index 0dada92..88ee70b 100644
--- a/dlls/kernel/editline.c
+++ b/dlls/kernel/editline.c
@@ -150,7 +150,12 @@
 
         /* round up size to 32 byte-WCHAR boundary */
         newsize = (ctx->len + len + 1 + 31) & ~31;
-	newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
+
+	if (ctx->line)
+	    newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
+	else
+	    newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize);
+
 	if (!newline) return FALSE;
 	ctx->line = newline;
 	ctx->alloc = newsize;
@@ -237,7 +242,8 @@
     if (len <= 0) return;
 
     WCEL_FreeYank(ctx);
-    ctx->yanked = HeapReAlloc(GetProcessHeap(), 0, ctx->yanked, (len + 1) * sizeof(WCHAR));
+    /* After WCEL_FreeYank ctx->yanked is empty */
+    ctx->yanked = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
     if (!ctx->yanked) return;
     memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
     ctx->yanked[len] = 0;
diff --git a/dlls/kernel/global16.c b/dlls/kernel/global16.c
index a21c7c2..76641e5 100644
--- a/dlls/kernel/global16.c
+++ b/dlls/kernel/global16.c
@@ -352,10 +352,16 @@
          * given out by GetVDMPointer32W16),
          * only try to realloc in place
          */
-        newptr = HeapReAlloc( GetProcessHeap(),
-                              (pArena->pageLockCount > 0) ? 
-                              HEAP_REALLOC_IN_PLACE_ONLY : 0, 
+
+	if (ptr)
+            newptr = HeapReAlloc( GetProcessHeap(),
+		(pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0, 
                               ptr, size );
+	else
+            newptr = HeapAlloc( GetProcessHeap(),
+		(pArena->pageLockCount > 0) ? HEAP_REALLOC_IN_PLACE_ONLY : 0, 
+                              size );
+
     }
 
     if (!newptr)
diff --git a/dlls/kernel/resource16.c b/dlls/kernel/resource16.c
index a00a89a..8545588 100644
--- a/dlls/kernel/resource16.c
+++ b/dlls/kernel/resource16.c
@@ -95,10 +95,15 @@
     /* If no space left, grow table */
     if ( map->nUsed == map->nAlloc )
     {
-        if ( !(newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
-                                                    map->elem,
-                                                    (map->nAlloc + HRSRC_MAP_BLOCKSIZE)
-                                                    * sizeof(HRSRC_ELEM) ) ))
+
+	if (map->elem)
+    	    newElem = (HRSRC_ELEM *)HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
+                    map->elem, (map->nAlloc + HRSRC_MAP_BLOCKSIZE) * sizeof(HRSRC_ELEM) );
+	else
+    	    newElem = (HRSRC_ELEM *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
+                    (map->nAlloc + HRSRC_MAP_BLOCKSIZE) * sizeof(HRSRC_ELEM) );
+
+        if ( !newElem )
         {
             ERR("Cannot grow HRSRC map\n" );
             return 0;
diff --git a/dlls/kernel/snoop16.c b/dlls/kernel/snoop16.c
index ab27640..a155703 100644
--- a/dlls/kernel/snoop16.c
+++ b/dlls/kernel/snoop16.c
@@ -145,7 +145,12 @@
                 }
 		dll = &((*dll)->next);
 	}
-	*dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP16_DLL)+strlen(name));
+
+	if (*dll)
+		*dll = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *dll, sizeof(SNOOP16_DLL)+strlen(name));
+	else
+		*dll = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(SNOOP16_DLL)+strlen(name));	
+
 	(*dll)->next	= NULL;
 	(*dll)->hmod	= pModule->self;
 	if ((s=strrchr(name,'\\')))
diff --git a/dlls/ole32/oleproxy.c b/dlls/ole32/oleproxy.c
index bcce77c..0739592 100644
--- a/dlls/ole32/oleproxy.c
+++ b/dlls/ole32/oleproxy.c
@@ -185,7 +185,12 @@
 	}
 
 	msg->cbBuffer = ststg.cbSize.s.LowPart;
-	msg->Buffer = HeapReAlloc(GetProcessHeap(),0,msg->Buffer,ststg.cbSize.s.LowPart);
+
+	if (msg->Buffer)
+	    msg->Buffer = HeapReAlloc(GetProcessHeap(),0,msg->Buffer,ststg.cbSize.s.LowPart);
+	else
+	    msg->Buffer = HeapAlloc(GetProcessHeap(),0,ststg.cbSize.s.LowPart);
+
 	seekto.s.LowPart = 0;seekto.s.HighPart = 0;
 	hres = IStream_Seek(pStm,seekto,SEEK_SET,&newpos);
 	if (hres) {
diff --git a/dlls/ole32/rpc.c b/dlls/ole32/rpc.c
index fed58aa..43e823a 100644
--- a/dlls/ole32/rpc.c
+++ b/dlls/ole32/rpc.c
@@ -606,7 +606,12 @@
 		continue;
 	    if (xreq->reqh.reqid == resph.reqid) {
 		memcpy(&(xreq->resph),&resph,sizeof(resph));
-		xreq->Buffer = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,xreq->Buffer,xreq->resph.cbBuffer);
+
+		if (xreq->Buffer)
+		    xreq->Buffer = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,xreq->Buffer,xreq->resph.cbBuffer);
+		else
+		    xreq->Buffer = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,xreq->resph.cbBuffer);
+
 		hres = _xread(xhPipe,xreq->Buffer,xreq->resph.cbBuffer);
 		if (hres) goto end;
 		xreq->state = REQSTATE_RESP_GOT;
diff --git a/dlls/rpcrt4/rpc_server.c b/dlls/rpcrt4/rpc_server.c
index e9ed593..e9fe902 100644
--- a/dlls/rpcrt4/rpc_server.c
+++ b/dlls/rpcrt4/rpc_server.c
@@ -390,7 +390,11 @@
       cps = cps->Next;
     }
     /* make array of connections */
-    objs = HeapReAlloc(GetProcessHeap(), 0, objs, count*sizeof(HANDLE));
+    if (objs)
+	objs = HeapReAlloc(GetProcessHeap(), 0, objs, count*sizeof(HANDLE));
+    else
+	objs = HeapAlloc(GetProcessHeap(), 0, count*sizeof(HANDLE));
+
     objs[0] = m_event;
     count = 1;
     cps = protseqs;
diff --git a/dlls/setupapi/dirid.c b/dlls/setupapi/dirid.c
index 7728f89..63f2e86 100644
--- a/dlls/setupapi/dirid.c
+++ b/dlls/setupapi/dirid.c
@@ -176,8 +176,16 @@
         if (nb_user_dirids >= alloc_user_dirids)
         {
             int new_size = max( 32, alloc_user_dirids * 2 );
-            struct user_dirid *new = HeapReAlloc( GetProcessHeap(), 0, user_dirids,
+
+	    struct user_dirid *new;
+
+	    if (user_dirids)
+                new = HeapReAlloc( GetProcessHeap(), 0, user_dirids,
                                                   new_size * sizeof(*new) );
+	    else
+                new = HeapAlloc( GetProcessHeap(), 0, 
+                                                  new_size * sizeof(*new) );
+
             if (!new) return FALSE;
             user_dirids = new;
             alloc_user_dirids = new_size;
diff --git a/dlls/setupapi/setupx_main.c b/dlls/setupapi/setupx_main.c
index fdbf666..4f5b952 100644
--- a/dlls/setupapi/setupx_main.c
+++ b/dlls/setupapi/setupx_main.c
@@ -131,7 +131,10 @@
 	/* alloc entry for new substring in steps of 32 units and copy over */
 	if (count % 32 == 0)
 	{ /* 1 for count field + current count + 32 */
-	    res = HeapReAlloc(GetProcessHeap(), 0, res, (1+count+32)*sizeof(LPSTR));
+	    if (res)
+    		res = HeapReAlloc(GetProcessHeap(), 0, res, (1+count+32)*sizeof(LPSTR));
+	    else
+		res = HeapAlloc(GetProcessHeap(), 0, (1+count+32)*sizeof(LPSTR));	    
 	}
 	*(res+1+count) = HeapAlloc(GetProcessHeap(), 0, len+1);
 	strncpy(*(res+1+count), p, len);
diff --git a/dlls/user/message.c b/dlls/user/message.c
index 01abe4e..31ee8b9 100644
--- a/dlls/user/message.c
+++ b/dlls/user/message.c
@@ -1160,8 +1160,14 @@
     /* now remember the pair of hMem on both sides */
     if (dde_num_used == dde_num_alloc)
     {
-        struct DDE_pair* tmp = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
+        struct DDE_pair* tmp;
+	if (dde_pairs)
+	    tmp  = HeapReAlloc( GetProcessHeap(), 0, dde_pairs,
                                             (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
+	else
+	    tmp  = HeapAlloc( GetProcessHeap(), 0, 
+                                            (dde_num_alloc + GROWBY) * sizeof(struct DDE_pair));
+
         if (!tmp)
         {
             LeaveCriticalSection(&dde_crst);