Skip to content

Commit

Permalink
Fixed inheritance of LPVOID and simplified access to CoTaskMem*
Browse files Browse the repository at this point in the history
  • Loading branch information
msteiger committed May 25, 2014
1 parent a0eaa3f commit e52c425
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
6 changes: 3 additions & 3 deletions contrib/platform/src/com/sun/jna/platform/win32/Ole32.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ HRESULT CoCreateInstance(GUID rclsid, Pointer pUnkOuter, int dwClsContext,
* @param cb The size of the memory block to be allocated, in bytes.
* @return If the function succeeds, it returns the allocated memory block. Otherwise, it returns NULL.
*/
LPVOID CoTaskMemAlloc(SIZE_T cb);
Pointer CoTaskMemAlloc(long cb);

/**
* Changes the size of a previously allocated block of task memory. This function changes the size of a previously
Expand All @@ -256,7 +256,7 @@ HRESULT CoCreateInstance(GUID rclsid, Pointer pUnkOuter, int dwClsContext,
* @param cb The size of the memory block to be reallocated, in bytes. This parameter can be 0.
* @return If the function succeeds, it returns the reallocated memory block. Otherwise, it returns NULL.
*/
LPVOID CoTaskMemRealloc(LPVOID pv, SIZE_T cb);
Pointer CoTaskMemRealloc(Pointer pv, long cb);

/**
* Frees a block of task memory previously allocated through a call to the {@link #CoTaskMemAlloc} or
Expand All @@ -265,6 +265,6 @@ HRESULT CoCreateInstance(GUID rclsid, Pointer pUnkOuter, int dwClsContext,
* pointed to by pv is invalid and can no longer be used.
* @param pv A pointer to the memory block to be freed. If this parameter is NULL, the function has no effect.
*/
void CoTaskMemFree(LPVOID pv);
void CoTaskMemFree(Pointer pv);

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static String getKnownFolderPath(GUID guid) throws Win32Exception
}

String result = outPath.getValue().getWideString(0);
Ole32.INSTANCE.CoTaskMemFree(new LPVOID(outPath.getPointer().getLong(0)));
Ole32.INSTANCE.CoTaskMemFree(outPath.getValue());

return result;
}
Expand Down
20 changes: 9 additions & 11 deletions contrib/platform/src/com/sun/jna/platform/win32/WinDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ public ATOM(long value) {
public static class PVOID extends PointerType {

public PVOID() {
// TODO Auto-generated constructor stub
super();
}

/**
Expand All @@ -992,25 +992,23 @@ public PVOID(Pointer pointer) {
}

/**
* Message parameter.
* LPVOID is simply a Windows API typedef for void* - to pointer to any type so to speak.
*/
public static class LPVOID extends LONG_PTR {
public static class LPVOID extends PointerType {

/**
* Instantiates a new lpvoid.
* Instantiates a new instance to NULL.
*/
public LPVOID() {
this(0);
super();
}

/**
* Instantiates a new lpvoid.
*
* @param value
* the value
* Instantiates a new instance using a given pointer.
* @param p the pointer
*/
public LPVOID(long value) {
super(value);
public LPVOID(Pointer p) {
super(p);
}
}

Expand Down
12 changes: 6 additions & 6 deletions contrib/platform/test/com/sun/jna/platform/win32/Ole32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,21 @@ public final void testCLSIDFromProgID() {
}

public void testCoTaskMemAlloc() {
LPVOID ptr = Ole32.INSTANCE.CoTaskMemAlloc(new SIZE_T(256));
Pointer ptr = Ole32.INSTANCE.CoTaskMemAlloc(256);

assertTrue(ptr.longValue() != 0);
assertTrue(!ptr.equals(Pointer.NULL));

This comment has been minimized.

Copy link
@twall

twall Jun 1, 2014

Contributor

Wouldn't assertNotNull() be more concise?


Ole32.INSTANCE.CoTaskMemFree(ptr);
}

public void testCoTaskMemRealloc() {
LPVOID ptr = Ole32.INSTANCE.CoTaskMemAlloc(new SIZE_T(256));
Pointer ptr = Ole32.INSTANCE.CoTaskMemAlloc(256);

assertTrue(ptr.longValue() != 0);
assertTrue(!ptr.equals(Pointer.NULL));

ptr = Ole32.INSTANCE.CoTaskMemRealloc(ptr, new SIZE_T(128));
ptr = Ole32.INSTANCE.CoTaskMemRealloc(ptr, 128);

assertTrue(ptr.longValue() != 0);
assertTrue(!ptr.equals(Pointer.NULL));

Ole32.INSTANCE.CoTaskMemFree(ptr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void testSHGetKnownFolderPath()
GUID guid = KnownFolders.FOLDERID_Fonts;
HRESULT hr = Shell32.INSTANCE.SHGetKnownFolderPath(guid, flags, token, outPath);

Ole32.INSTANCE.CoTaskMemFree(new LPVOID(outPath.getPointer().getLong(0)));
Ole32.INSTANCE.CoTaskMemFree(outPath.getValue());

assertTrue(W32Errors.SUCCEEDED(hr.intValue()));
}
Expand Down

3 comments on commit e52c425

@lwahonen
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms692727(v=vs.85).aspx

SIZE_T == ULONG_PTR == unsigned long == 32 bits. Java long is 64 bits. Did you mean NativeLong?

@msteiger
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the "Community Additions" it should work for long also. Do you think it would be better to use SIZE_T or int ?

@twall
Copy link
Contributor

@twall twall commented on e52c425 May 31, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC SIZE_T is 32-bits or 64-bits depending on the architecture, so NativeLong would not be appropriate. According to @lwahonen 's link, only 32 bits is used, but you still need to start with a 64-bit quantity on win64.

Please sign in to comment.