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:Timothy Wall (twal@dev.java.net)
Date:Jan 31, 2008 1:42:22 pm
List:net.java.dev.jna.users

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.