Hi,
I'm having troubles in getting Windows API Call WNetAddConnection2 to Mpr.dll to work. Hopefully somebody might show me what I'm doing wrong or I'm missing ...
On Windows 7, 64-Bit, behaviour of MprTest in a 32-Bit JVM (1.7.0_45, 1.6.0_41) is differing from execution in a 64-Bit JVM (1.7.0_25, 1.6.0_41):
On a 32-Bit JVM the return value will always be 67 (ERROR_BAD_NET_NAME), even when I'm completely sure, that lpRemoteName is a correct UNC path to an existing and reachable share.
On a 64-Bit JVM the return value will always be 0 (ERROR_SUCCESS). Great! But unfortunately no connection is established. It seems like the value of lpRemoteName - is ignored: Even when providing a nonsense value for lpRemoteName the return value will always be 0.
Unfortunately I can not see the mistake in my implemenation.
32-Bit JVM: java version "1.7.0_45", Java HotSpot(TM) Client VM (build 24.45-b08, mixed mode, sharing)
(Platform.RESOURCE_PREFIX=win32-x86, Kernel32.GetNativeSystemInfo(lpSystemInfo): lpSystemInfo.processorArchitecture.pi.wProcessorArchitecture=0 (x86))
Mounting Windows Share [\\hostname\share] [userName] ...
Mounting Windows Share: 67 (ERROR_BAD_NET_NAME)
64-Bit JVM: java version "1.7.0_25", Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
(Platform.RESOURCE_PREFIX=win32-x86-64, Kernel32.GetNativeSystemInfo(lpSystemInfo): lpSystemInfo.processorArchitecture.pi.wProcessorArchitecture=0 (x86))
Mounting Windows Share [\\hostname\share] [userName] ...
Mounting Windows Share: 0 (ERROR_SUCCESS)
Besides I'm wondering why Kernel32.GetNativeSystemInfo will always return a ProcessorArchitecture of 0 (x86) on 32-Bit and on a 64-Bit JVM. I would expect 9 (x64 (AMD or Intel)) on a 64-Bit JVM. Maybe somebody may explain it to me...
Mpr.java
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.win32.W32APIOptions;
public interface Mpr extends WinNT
Mpr INSTANCE = (Mpr)Native.loadLibrary("Mpr", Mpr.class, W32APIOptions.UNICODE_OPTIONS);
public int WNetAddConnection2(NETRESOURCE lpNetResource, String lpPassword, String lpUserName, int dwFlags);
}
NETRESOURCE.java
import com.sun.jna.Structure;
public class NETRESOURCE extends Structure
public static final java.util.List<String> fieldOrder = java.util.Arrays.asList(new String[]
public int dwDisplayType;
public String lpLocalName;
public String lpRemoteName;
public String lpProvider;
protected java.util.List getFieldOrder()
MprTest.java
import org.junit.Test;
public class MprTest
{
public static final int RESOURCETYPE_DISK = 0x00000001;
public static final int RESOURCEDISPLAYTYPE_SHARE = 0x00000003;
public static final int RESOURCEUSAGE_CONNECTABLE = 0x00000001;
@Test
public void testMpr() throws Exception
{
NETRESOURCE lpNetResource;
String lpPassword, lpUserName;
int dwFlags;
int errorCode;
lpNetResource = new NETRESOURCE();
lpNetResource.dwScope = 0;
lpNetResource.dwType = RESOURCETYPE_DISK;
lpNetResource.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
lpNetResource.dwUsage = RESOURCEUSAGE_CONNECTABLE;
lpNetResource.lpLocalName = null;
lpNetResource.lpRemoteName = "\\\\hostname\\share";
lpNetResource.lpComment = null;
lpNetResource.lpProvider = null;
lpPassword = "password";
lpUserName = "userName";
dwFlags = 0;
System.out.println("Mounting Windows Share [" + lpNetResource.lpRemoteName + "] [" + lpUserName + "] ...");
errorCode = Mpr.INSTANCE.WNetAddConnection2(lpNetResource, lpPassword, lpUserName, dwFlags);
System.out.println("Mounting Windows Share: " + errorCode);
}
}
Thanks in advance!