Skip to content

Commit 21d459b

Browse files
msteigerdblock
authored andcommitted
Added CoTaskMemAlloc, CoTaskMemRealloc and CoTaskMemFree to com.sun.jna.platform.win32.Ole32.
1 parent 05210b2 commit 21d459b

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Features
1111
* [#290](https://github.com/twall/jna/pull/290): Improved the stacktrace for the exceptions thrown by `com.sun.jna.Structure` - [@ebourg](https://github.com/ebourg).
1212
* Added Winspool monitor sample and updated Kernel32, WinBase, Winspool - [@wolftobias](https://github.com/wolftobias).
1313
* Added Some minor changes to MS Office samples Test and small changes to the MS Office samples Bug Fixes - [@wolftobias](https://github.com/wolftobias).
14+
* [#333](https://github.com/twall/jna/pull/333): Added `CoTaskMemAlloc`, `CoTaskMemRealloc` and `CoTaskMemFree` to `com.sun.jna.platform.win32.Ole32` - [@msteiger](https://github.com/msteiger).
1415

1516
Bug Fixes
1617
---------

contrib/platform/src/com/sun/jna/platform/win32/Ole32.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.sun.jna.Native;
1616
import com.sun.jna.Pointer;
1717
import com.sun.jna.WString;
18+
import com.sun.jna.platform.win32.BaseTSD.SIZE_T;
1819
import com.sun.jna.platform.win32.Guid.CLSID;
1920
import com.sun.jna.platform.win32.Guid.GUID;
2021
import com.sun.jna.platform.win32.WinDef.LPVOID;
@@ -214,4 +215,56 @@ HRESULT CoCreateInstance(GUID rclsid, Pointer pUnkOuter, int dwClsContext,
214215
*/
215216
HRESULT CLSIDFromString(WString lpsz, CLSID.ByReference pclsid);
216217

218+
/**
219+
* Allocates a block of task memory in the same way that IMalloc::Alloc does. CoTaskMemAlloc uses the default
220+
* allocator to allocate a memory block in the same way that IMalloc::Alloc does. It is not necessary to call the
221+
* CoGetMalloc function before calling CoTaskMemAlloc.
222+
* <br/><br/>The initial contents of the returned memory block are
223+
* undefined - there is no guarantee that the block has been initialized. The allocated block may be larger than cb
224+
* bytes because of the space required for alignment and for maintenance information.
225+
* <br/><br/>
226+
* If cb is 0, CoTaskMemAlloc
227+
* allocates a zero-length item and returns a valid pointer to that item. If there is insufficient memory available,
228+
* CoTaskMemAlloc returns NULL. Applications should always check the return value from this function, even when
229+
* requesting small amounts of memory, because there is no guarantee that the memory will be allocated.
230+
* @param cb The size of the memory block to be allocated, in bytes.
231+
* @return If the function succeeds, it returns the allocated memory block. Otherwise, it returns NULL.
232+
*/
233+
LPVOID CoTaskMemAlloc(SIZE_T cb);
234+
235+
/**
236+
* Changes the size of a previously allocated block of task memory. This function changes the size of a previously
237+
* allocated memory block in the same way that IMalloc::Realloc does. It is not necessary to call the CoGetMalloc
238+
* function to get a pointer to the OLE allocator before calling CoTaskMemRealloc.
239+
* <br/><br/>
240+
* The pv parameter points to the
241+
* beginning of the memory block. If pv is NULL, CoTaskMemRealloc allocates a new memory block in the same way as
242+
* the CoTaskMemAlloc function. If pv is not NULL, it should be a pointer returned by a prior call to
243+
* CoTaskMemAlloc.
244+
* <br/><br>
245+
* The cb parameter specifies the size of the new block. The contents of the block are unchanged up
246+
* to the shorter of the new and old sizes, although the new block can be in a different location. Because the new
247+
* block can be in a different memory location, the pointer returned by CoTaskMemRealloc is not guaranteed to be the
248+
* pointer passed through the pv argument. If pv is not NULL and cb is 0, then the memory pointed to by pv is freed.
249+
* <br/><br/>
250+
* CoTaskMemRealloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is
251+
* NULL if the size is 0 and the buffer argument is not NULL, or if there is not enough memory available to expand
252+
* the block to the specified size. In the first case, the original block is freed; in the second case, the original
253+
* block is unchanged. The storage space pointed to by the return value is guaranteed to be suitably aligned for
254+
* storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.
255+
* @param pv A pointer to the memory block to be reallocated. This parameter can be NULL.
256+
* @param cb The size of the memory block to be reallocated, in bytes. This parameter can be 0.
257+
* @return If the function succeeds, it returns the reallocated memory block. Otherwise, it returns NULL.
258+
*/
259+
LPVOID CoTaskMemRealloc(LPVOID pv, SIZE_T cb);
260+
261+
/**
262+
* Frees a block of task memory previously allocated through a call to the {@link #CoTaskMemAlloc} or
263+
* {@link #CoTaskMemRealloc} function. The function uses the default OLE allocator. The number of bytes
264+
* freed equals the number of bytes that were originally allocated or reallocated. After the call, the memory block
265+
* pointed to by pv is invalid and can no longer be used.
266+
* @param pv A pointer to the memory block to be freed. If this parameter is NULL, the function has no effect.
267+
*/
268+
void CoTaskMemFree(LPVOID pv);
269+
217270
}

contrib/platform/test/com/sun/jna/platform/win32/Ole32Test.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
import com.sun.jna.Native;
1818
import com.sun.jna.Pointer;
19+
import com.sun.jna.platform.win32.BaseTSD.SIZE_T;
1920
import com.sun.jna.platform.win32.Guid.GUID;
21+
import com.sun.jna.platform.win32.WinDef.LPVOID;
2022
import com.sun.jna.platform.win32.WinNT.HRESULT;
2123
import com.sun.jna.ptr.PointerByReference;
2224

@@ -106,4 +108,24 @@ public final void testCLSIDFromProgID() {
106108
assertEquals(WinError.S_OK, Ole32.INSTANCE.CLSIDFromProgID("jpegfile", clsid));
107109
assertEquals("{25336920-03F9-11CF-8FD0-00AA00686F13}", clsid.toGuidString());
108110
}
111+
112+
public void testCoTaskMemAlloc() {
113+
LPVOID ptr = Ole32.INSTANCE.CoTaskMemAlloc(new SIZE_T(256));
114+
115+
assertTrue(ptr.longValue() != 0);
116+
117+
Ole32.INSTANCE.CoTaskMemFree(ptr);
118+
}
119+
120+
public void testCoTaskMemRealloc() {
121+
LPVOID ptr = Ole32.INSTANCE.CoTaskMemAlloc(new SIZE_T(256));
122+
123+
assertTrue(ptr.longValue() != 0);
124+
125+
ptr = Ole32.INSTANCE.CoTaskMemRealloc(ptr, new SIZE_T(128));
126+
127+
assertTrue(ptr.longValue() != 0);
128+
129+
Ole32.INSTANCE.CoTaskMemFree(ptr);
130+
}
109131
}

0 commit comments

Comments
 (0)