如何从C ++中的char指针获取Java字符串

时间:2016-08-30 13:01:01

标签: c++ arrays pointers char jna

在我们使用jna创建openvr sample之后,我将jogl移植到binding

几乎在最后(在渲染控制器和跟踪基站之前),我试图将C中的char指针转换为Java中的String。

C ++代码here

//-----------------------------------------------------------------------------
// Purpose: Helper to get a string from a tracked device property and turn it
//          into a std::string
//-----------------------------------------------------------------------------
std::string GetTrackedDeviceString( vr::IVRSystem *pHmd, vr::TrackedDeviceIndex_t unDevice, vr::TrackedDeviceProperty prop, vr::TrackedPropertyError *peError = NULL )
{
    uint32_t unRequiredBufferLen = pHmd->GetStringTrackedDeviceProperty( unDevice, prop, NULL, 0, peError );
    if( unRequiredBufferLen == 0 )
        return "";

    char *pchBuffer = new char[ unRequiredBufferLen ];
    unRequiredBufferLen = pHmd->GetStringTrackedDeviceProperty( unDevice, prop, pchBuffer, unRequiredBufferLen, peError );
    std::string sResult = pchBuffer;
    delete [] pchBuffer;
    return sResult;
}

GetStringTrackedDeviceProperty here

/** Returns a string property. If the device index is not valid or the property is not a string type this function will 
* return 0. Otherwise it returns the length of the number of bytes necessary to hold this string including the trailing
* null. Strings will generally fit in buffers of k_unTrackingStringSize characters. */
virtual uint32_t GetStringTrackedDeviceProperty( vr::TrackedDeviceIndex_t unDeviceIndex, ETrackedDeviceProperty prop, VR_OUT_STRING() char *pchValue, uint32_t unBufferSize, ETrackedPropertyError *pError = 0L ) = 0;

VR_OUT_STRING()定义为here的位置为:

# define VR_CLANG_ATTR(ATTR)

#define VR_OUT_STRING() VR_CLANG_ATTR( "out_string: ;" )

我已经做了类似的事情,我必须调用一个函数,期望指向TrackedDevicePose_t结构数组的指针:

private TrackedDevicePose_t.ByReference trackedDevicePosesReference = new TrackedDevicePose_t.ByReference();
public TrackedDevicePose_t[] trackedDevicePose
            = (TrackedDevicePose_t[]) trackedDevicePosesReference.toArray(VR.k_unMaxTrackedDeviceCount);

我首先创建了引用,然后从中创建了实际的数组。

here我无法扩展char数组..

private String getTrackedDeviceString(IVRSystem hmd, int device, int prop, IntBuffer propError) {

    int requiredBufferLen = hmd.GetStringTrackedDeviceProperty.apply(device, prop, Pointer.NULL, 0, propError);

    if(requiredBufferLen == 0) {
        return "";
    }


    CharArray.ByReference charArrayReference = new CharArray.ByReference();
    char[] cs = charArrayReference.toArray(requiredBufferLen);

    return null;
}

applyhere)的位置:

public interface GetStringTrackedDeviceProperty_callback extends Callback {

    int apply(int unDeviceIndex, int prop, Pointer pchValue, int unBufferSize, IntBuffer pError);
};

CharArray课程,废话尝试here

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我已经完成了将C和C ++代码移植到Java的过程,虽然它可能非常hacky,但我能提出最好的解决指向{{的指针的情况。 1}}原语或函数调用需要int / char*,是创建一个包含单个属性的小包装类,将该对象传递给函数,根据需要更改属性,以及在函数调用后检索新值。如下所示:

String

public class StringPointer { public String value = ""; } StringPointer pchBuffer = new StringPointer(); unRequiredBufferLen = pHmd.GetStringTrackedDeviceProperty( unDevice, prop, pchBuffer, unRequiredBufferLen, peError ); String sResult = pchBuffer.value;

GetStringTrackedDeviceProperty()

在这种情况下,您可以使用... pchValue.value = "some string"; ... ,因为您的代码在函数调用后使用String执行的操作,但是如果实际上确实需要char* {1}},您只需创建char[]并将其传递给函数即可。就像你在C ++中使用char[] pchBuffer = new char[unRequiredBufferLen];一样,你在数组中做出的任何更改都会在函数结束后显示,你甚至可以char*