KNI学习资料在网上找了半天的KNI编程资料,说的都太浅。正好我在这方面有些研究,慢慢整理出一些资料供大家使用吧。 KNIEXPORT KNI_RETURNTYPE_INT Java_com_sun_midp_io_j2me_sms_Protocol_send0(void) {
//定义4个句柄 KNI_StartHandles(4);
//一个句柄取名为this,注意KNI_DeclareHandle()是一个宏,而不是一个函数 KNI_DeclareHandle(this);
//一个句柄取名为thisClass KNI_DeclareHandle(thisClass);
//取当前引用对象的this指针,注意当前对象对这个函数的引用一定不是static的,否则没有this指针 KNI_GetThisPointer(this);
//取当前类对象的指针 KNI_GetObjectClass(this, thisClass);
//取当前类对象中的一个属性 isOpen = KNI_GetBooleanField(this, KNI_GetFieldID(thisClass, "open", "Z"));
//取几个参数 jint messageType; jint sourcePort; jint destPort; jint handle; KNI_DeclareHandle(messageBuffer); KNI_DeclareHandle(address);
handle = KNI_GetParameterAsInt(1); messageType = KNI_GetParameterAsInt(2); KNI_GetParameterAsObject(3, address); destPort = KNI_GetParameterAsInt(4); sourcePort = KNI_GetParameterAsInt(5); KNI_GetParameterAsObject(6, messageBuffer);
//取一个字符串 len = KNI_GetStringLength(address); data = (jchar *)malloc(len * sizeof (jchar)); if( data == NULL ) KNI_ThrowNew(midpOutOfMemoryError, NULL); KNI_GetStringRegion(address, 0, len, data);
//取一个对象,把它拷贝到自己定义的缓存区里面 if (!KNI_IsNullHandle(messageBuffer)) { messageLength = KNI_GetArrayLength(messageBuffer); } if (messageLength > 0) { pMessageBuffer = (unsigned char *)malloc( messageLength ); memset(pMessageBuffer, 0, messageLength); KNI_GetRawArrayRegion(messageBuffer, 0, messageLength, (jbyte *)pMessageBuffer); }
//交回句柄缓存区,记住KNI_EndHandles()是一个宏,而不是一个函数,让句柄所对应的对象的存储空间交回虚拟机管理 KNI_EndHandles();
KNI_ReturnInt(0); /* currently ignored. */ }//end of Java_com_sun_midp_io_j2me_sms_Protocol_send0() |
|