Friday, May 14, 2010

Pointers in JNI / C++

When writing native code for Java (JNI), It's a common behaviour to store the pointer as long in a field in Java, and do a type-cast conversion on the native code back to the original pointer.


I did that on the native module on HornetQ, however this seems to eventually fail.

when using different bit sets as the target compilation (32 bits for instance) this seems to fail when you move back and forth.


The best way for that would be to use a Native Byte Buffer. This way you don't need to worry about conversions. You can just use a pointer back and forth:


Example:


In your Java class, you create this signature:

public class SomeClass
{
public native ByteBuffer initPointer();
}


And in your C++ code:


JNIEXPORT jobject JNICALL Java_SomeClass_initPointer
(JNIEnv * env, jobject obj)
{
return env->NewDirectByteBuffer(myPointer, 0); // size = 0, you don't want anyone to change it
}




Later, any C++ code can get back the pointer by doing:


return (MyClass *) env->GetDirectBufferAddress(myPointer);




I know the word pointer makes this a bit sour, but maybe it will be helpful to someone some day :-)

22 comments:

mohan said...

Hi,

I am struggling to see very clear articulation of "large message" handling using HornetQ. There are sample codes and configuration details but I am looking for more pictorial representation of "large message" pattern.

Can you or tim put together a powerpoint to explain clearly design of large message handling using HornetQ??

Thanks

貴寶 said...

人有兩眼一舌,是為了觀察倍於說話的緣故。............................................................

吳婷婷 said...

人有兩眼一舌,是為了觀察倍於說話的緣故。............................................................

胤綸胤綸 said...

人要學習健忘,把所有不如意忘掉,才會快樂。..................................................

偉軍 said...

[做人難,人難做,難做人] 人.事的艱困與磨難,是一種考驗!要以樂觀歡喜之心,很珍惜地過每一天!^^..................................................

吳婷婷 said...

Quality is better than quantity.............................................................

王美妹 said...

被你的人氣吸引過來~~.................................[/url]...............

水慧 said...

精彩的部落格 值得一推再推 支持你............................................................

士凱士凱 said...

一定要保持最佳狀況呦,祝您開心............................................................

bur蔡ge佳ssal郁berto said...

堅持是為著某種目的或目標,而持續不斷朝向既定方向努力的一種意念。..................................................

童祖如童祖如 said...

絕不要羞於承認自己不知道的事。..................................................

ToryO_Vis建銘 said...

堅持是為著某種目的或目標,而持續不斷朝向既定方向努力的一種意念。..................................................

蕭劉明倫松恬 said...

第一忠誠,第二勤奮,第三專心工作。..................................................

周志v豪 said...

愛,拆開來是心和受兩個字。用心去接受對方的一切,用心去愛對方的所有。......................................................................

文王廷 said...

人不能像動物一樣活著,而應該追求知識和美德............................................................

偉曹琬 said...

你的部落格很棒,我期待更新喔............................................................

張王雅竹欣虹 said...

一個人的際遇在第一次總是最深刻的,有時候甚至會讓人的心變成永遠的絕緣。......................................................................

1615 said...

當我微笑時,世界和我一起微笑;當我快樂時,世界和我一起活躍。..................................................

孟謝謝謝凱謝謝謝謝 said...

不要去想沒拿到的東西,多想想自己手裡所擁有的..................................................

翊翊翊翊張瑜翊翊翊 said...

愛,拆開來是心和受兩個字。用心去接受對方的一切,用心去愛對方的所有。......................................................................

怡靜怡靜怡靜怡雯 said...

你怎麼能經過一片海,而忘記它的藍?......................................................................

姚义丰 said...

要保持更新呦,加油!!!期待你的新文章!!!............................................................