Skip to content

Commit

Permalink
Merge pull request #365 from lgoldstein/get-computer-name-ex
Browse files Browse the repository at this point in the history
Added GetComputerNameEx support
  • Loading branch information
dblock committed Sep 4, 2014
2 parents 6dc14f8 + 29b4912 commit ee677e3
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Features
* Improved `contrib/msoffice` sample - [@wolftobias](https://github.com/wolftobias).
* [#352](https://github.com/twall/jna/pull/352): Performance improvements due to reduced locking in `com.sun.jna.Library$Handler` and fewer vararg checks in `com.sun.jna.Function` - [@Boereck](https://github.com/Boereck).
* [#357](https://github.com/twall/jna/pull/357): Added `com.sun.jna.platform.win32.Kernel32.SetSystemTime` - [@lgoldstein](https://github.com/lgoldstein), [@thomasjoulin](https://github.com/thomasjoulin).
* [#365](https://github.com/twall/jna/pull/365): Added `com.sun.jna.platform.win32.Kernel32.GetComputerNameEx` support - [@lgoldstein](https://github.com/lgoldstein).

Bug Fixes
---------
Expand Down
29 changes: 29 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,35 @@ Pointer MapViewOfFile(HANDLE hFileMappingObject, int dwDesiredAccess,
*/
public boolean GetComputerName(char[] buffer, IntByReference lpnSize);

/**
* Retrieves a NetBIOS or DNS name associated with the local computer,
* according to the <code>nameType</code> enumeration value
*
* @param nameType
* An enumeration value specifying the type of name to be
* retrieved - this parameter is a value from the
* COMPUTER_NAME_FORMAT in the WinBase definitions
* @param buffer
* A pointer to a buffer that receives the computer name or the
* cluster virtual server name. The length of the name may
* be greater than MAX_COMPUTERNAME_LENGTH characters because DNS
* allows longer names. To ensure that this buffer is large enough,
* set this parameter to NULL and use the required buffer size
* returned in the lpnSize parameter.
* @param lpnSize
* On input, specifies the size of the buffer, in TCHARs. On
* output, the number of TCHARs copied to the destination buffer,
* not including the terminating null character. If the buffer is
* too small, the function fails and GetLastError returns
* ERROR_BUFFER_OVERFLOW. The lpnSize parameter specifies the
* size of the buffer required, including the terminating null
* character.
* @return If the function succeeds, the return value is a nonzero value. If
* the function fails, the return value is zero. To get extended
* error information, call GetLastError.
*/
boolean GetComputerNameEx(int nameType, char[] buffer, IntByReference lpnSize);

/**
* The OpenThread function opens an existing thread object.
*
Expand Down
63 changes: 63 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/WinBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -923,4 +923,67 @@ protected List getFieldOrder() {
return Arrays.asList(new String[] { "foreignLocation" });
}
}

/**
* Specifies a type of computer name to be retrieved by the GetComputerNameEx function
*/
public static interface COMPUTER_NAME_FORMAT {
/**
* The NetBIOS name of the local computer or the cluster associated with the local
* computer. This name is limited to MAX_COMPUTERNAME_LENGTH + 1 characters and may
* be a truncated version of the DNS host name. For example, if the DNS host name is
* &quot;corporate-mail-server&quot;, the NetBIOS name would be &quot;corporate-mail-"&quot;.
*/
int ComputerNameNetBIOS = 0;

/**
* The DNS name of the local computer or the cluster associated with the local computer.
*/
int ComputerNameDnsHostname = 1;

/**
* The name of the DNS domain assigned to the local computer or the cluster associated
* with the local computer.
*/
int ComputerNameDnsDomain = 2;

/**
* The fully qualified DNS name that uniquely identifies the local computer or the cluster
* associated with the local computer. This name is a combination of the DNS host name and
* the DNS domain name, using the form HostName.DomainName. For example, if the DNS host
* name is &quot;corporate-mail-server&quot; and the DNS domain name is &quot;microsoft.com&quot;,
* the fully qualified DNS name is &quot;corporate-mail-server.microsoft.com&quot;.
*/
int ComputerNameDnsFullyQualified = 3;

/**
* The NetBIOS name of the local computer. On a cluster, this is the NetBIOS name of the
* local node on the cluster.
*/
int ComputerNamePhysicalNetBIOS = 4;

/**
* The DNS host name of the local computer. On a cluster, this is the DNS host name of the
* local node on the cluster.
*/
int ComputerNamePhysicalDnsHostname = 5;

/**
* The name of the DNS domain assigned to the local computer. On a cluster, this is the DNS
* domain of the local node on the cluster.
*/
int ComputerNamePhysicalDnsDomain = 6;

/**
* The fully qualified DNS name that uniquely identifies the computer. On a cluster, this is
* the fully qualified DNS name of the local node on the cluster. The fully qualified DNS name
* is a combination of the DNS host name and the DNS domain name, using the form HostName.DomainName.
*/
int ComputerNamePhysicalDnsFullyQualified = 7;

/**
* Note used - serves as an upper limit in case one wants to go through all the values
*/
int ComputerNameMax = 8;
}
}
17 changes: 17 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -144,6 +145,22 @@ public void testGetComputerName() {
assertTrue(Kernel32.INSTANCE.GetComputerName(buffer, lpnSize));
}

public void testGetComputerNameExSameAsGetComputerName() {
IntByReference lpnSize = new IntByReference(0);
char buffer[] = new char[WinBase.MAX_COMPUTERNAME_LENGTH + 1];
lpnSize.setValue(buffer.length);
assertTrue("Failed to retrieve expected computer name", Kernel32.INSTANCE.GetComputerName(buffer, lpnSize));
String expected = Native.toString(buffer);

// reset
lpnSize.setValue(buffer.length);
Arrays.fill(buffer, '\0');
assertTrue("Failed to retrieve extended computer name", Kernel32.INSTANCE.GetComputerNameEx(WinBase.COMPUTER_NAME_FORMAT.ComputerNameNetBIOS, buffer, lpnSize));
String actual = Native.toString(buffer);

assertEquals("Mismatched names", expected, actual);
}

public void testWaitForSingleObject() {
HANDLE handle = Kernel32.INSTANCE.CreateEvent(null, false, false, null);

Expand Down

0 comments on commit ee677e3

Please sign in to comment.