[英]JNA Pass char* By Reference
DllExport void LoadString(char *myStr)
{
cout << "Before: " << myStr << endl;
LoadStringData(&myStr);
cout << "After:" << myStr << endl;
}
and this in Java: 在Java中:
Pointer myStr = new Memory(Pointer.SIZE);
System.out.println(String.format("myStr Value: %s", myStr.getPointer(0).getString(0)));
this.Lib.LoadString(myStr);
System.out.println(String.format("myStr Value: %s", myStr.getPointer(0).getString(0)));
And this is the output: 这是输出:
myStr Value: ¸ï1
Before: Øî1
After:test
myStr Value: ¸ï1
So I can clearly see a garbage pointer being passed in, reallocated on C++ (After:test), but for some reason JNA isn't aware of the change. 因此,我可以清楚地看到传入的垃圾指针已在C ++(After:test)上重新分配,但是由于某些原因,JNA并不知道该更改。
These threads suggest what I'm doing is correct: 这些线程表明我在做什么是正确的:
How to obtain a new Pointer in Java? 如何在Java中获取新的Pointer?
JNA Struct and Pointer mapping JNA结构和指针映射
And I've also tried PointerByReference (though to be honest, that acts like void**), but I'm throwing every idea I can get at it. 而且我还尝试了PointerByReference(虽然,老实说,这就像void **一样),但是我想尽我所能。 However the threads above are about passing structs, not char*, but I can't see why JNA would care about the difference here. 但是上面的线程是关于传递结构而不是char *的,但是我看不出JNA为什么会在这里关心差异。
Any ideas? 有任何想法吗?
DllExport void LoadString(char **myStr)
{
cout << "Before: " << *myStr << endl;
LoadStringData(myStr);
cout << "After:" << *myStr << endl;
}
Should work better to pass a reference to the memory (char* ) rather than the memory it self(char ), that way your changes make it back to Java. 将引用传递给内存(char * )而不是传递内存自身(char )的效果更好,这样您所做的更改会将其返回给Java。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.