4 messages in net.java.dev.jna.usersRe: [jna-users] Mapping for LPCTSTR a...
FromSent OnAttachments
tho...@atunes.orgJan 31, 2008 12:39 pm 
Timothy WallJan 31, 2008 1:42 pm 
Paul LoyFeb 1, 2008 5:24 am 
Timothy WallFeb 1, 2008 5:43 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Mapping for LPCTSTR and LPTSTRActions...
From:Paul Loy (kete@gmail.com)
Date:Feb 1, 2008 5:24:20 am
List:net.java.dev.jna.users

funnily enough I have just done this myself:

/** * Mapping of the win32 API Kernel32.dll * @author Paul Loy * */ public abstract class Kernel32 {

private static final int CHAR_BYTE_WIDTH = 2;

public interface Kernel32Library extends Library {

/** * Unicode (wchar_t*) version of GetShortPathName()<br /> * <br /> * DWORD WINAPI GetShortPathNameW( * __in LPCTSTR lpszLongPath, * __out LPTSTR lpdzShortPath, * __in DWORD cchBuffer * ); */ NativeLong GetShortPathNameW(WString inPath, Memory outPathBuffer, NativeLong outPathBufferSize); }

public static Kernel32Library NATIVE = (Kernel32Library) Native.loadLibrary("Kernel32", Kernel32Library.class);

//TODO: Move helpers out into a separate class to simplify this class

/** * Helper method for Unicode Kernel32.NATIVE.GetShortPathNameW(). It performs * all the JNA munging for you. * * @param longPathName the path to on existing file or directory * @return 8.3 short form of the longPathName or null if the path could not * be found. */ public static String getShortPathNameW(String longPathName) { WString pathname = new WString(longPathName); NativeLong bufferSize = new NativeLong( (pathname.length()*CHAR_BYTE_WIDTH)+CHAR_BYTE_WIDTH); Memory buffer = new Memory(bufferSize.longValue());

if (Kernel32.NATIVE.GetShortPathNameW(pathname, buffer, bufferSize).longValue() == 0) { return null; } return buffer.getString(0, true); } }

On Jan 31, 2008 9:42 PM, Timothy Wall <twal@dev.java.net> wrote:

On Jan 31, 2008, at 3:40 PM, tho@atunes.org wrote:

Hi,

I want to call GetShortPathNameW (http://msdn2.microsoft.com/en-us/ library/aa364989(VS.85).aspx). What is the right mapping for LPCTSTR and LPTSTR? Maybe this?:

public interface Kernel32 extends StdCallLibrary {

public int GetShortPathNameW(PointerByReference lpszLongPath, PointerByReference lpszShortPath, int cchBuffer);

}

Use char[] to represent an array of wchar_t (which is what LPCTSTR is for W-suffixed w32 API methods). If you use the w32 unicode api mappings (see W32API.DEFAULT_OPTIONS), you can pass String for read- only native strings. You can then use Native.toString to convert the populated buffer back to a String.

This is one of my attemps but it doesn't work (java.lang.IndexOutOfBoundsException: Bounds exceeds available space : size=4, offset=112)

public class NativeCallUtils {

private static Kernel32 INSTANCE; static { System.setProperty("jna.encoding", "UTF8"); INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class); }

Setting the encoding is only necessary for the A-suffixed w32 API methods.

public static String getEightPointThreePathName(String longPathName) { PointerByReference pbr = new PointerByReference(); pbr.getPointer().setString(0, "\\\\?\\" + longPathName); PointerByReference pbr2 = new PointerByReference(); INSTANCE.GetShortPathNameW(pbr, pbr2, longPathName.length()); String s = pbr2.getValue().getString(0); return s; } }

Sorry, if this is actually a dumb question but I haven't done much C/C++ programming so far.