JNA for Windows API function GetVolumePathNamesForVolumeName

后端 未结 3 1649
庸人自扰
庸人自扰 2021-01-19 13:54

I\'ve successfully used JNA to call a couple of Windows API functions but I get stuck at this one

GetVolumePathNamesForVolumeName

The full C declaration is:<

3条回答
  •  佛祖请我去吃肉
    2021-01-19 14:33

    Thanks eee for your answer. It led me to the following. You answer was almost complete. Just needed a last bit to split the resultant char[] into the path components which is separated by null characters.

    // Decleration...
    
    public interface Kernel32 extends StdCallLibrary   {
    
       public boolean GetVolumePathNamesForVolumeName(
            WString lpszVolumeName,
            char[] lpszVolumePathNames,
            int cchBufferLength,
            IntByReference lpcchReturnLength
       );
    
       // Other methods....
    }
    

    ...

    // Instantiation
    Native.loadLibrary('kernel32', Kernel32.class, W32APIOptions.UNICODE_OPTIONS)
    

    ...

    // Usage
    
    public List getMountPoints() {
        char[] pathNames = new char[100];
        IntByReference len = new IntByReference();
        if (!kernel32.GetVolumePathNamesForVolumeName(new WString(this.getGuidPath()), pathNames, 100, len)) {
            if (kernel32.GetLastError() == WindowsConstants.ERROR_MORE_DATA) {
                pathNames = new char[len.getValue()];
                if (!kernel32.GetVolumePathNamesForVolumeName(new WString(this.getGuidPath()), pathNames, len.getValue(), len)) {
                    throw new WinApiException(kernel32.GetLastError());
                }
            } 
            else
                throw new WinApiException(kernel32.GetLastError());
        }
        List list = new LinkedList();
        int offset = 0;
        for (int i = 0; i < pathNames.length; i++) {
            if (pathNames[i] == '\u0000') {
                list.add(String.valueOf(pathNames, offset, i-offset));
                offset = i+1;
                if (pathNames[i+1] == '\u0000')
                    break;
            }
        }
        return list;
    }
    

提交回复
热议问题