Reputation: 51
I'm currently coding a Java application which loads a native library coded in C. A C function which gets called from Java is passed a jchar. In that C function, another library gets loaded with a new function which needs the jchar converted to a char* pointer. All I know about that new function is that it takes a pointer to a char value as parameter (char* parity).
This is my current code:
JNIEXPORT jint JNICALL Java_analyzer_DeclareNative_init (JNIEnv *env, jobject obj, ... , jchar parity, ...) {
typedef int (__stdcall *Init) (..., char* parity, ...);
HINSTANCE hinstLib = LoadLibrary("rs232");
Init initcon = (Init) GetProcAdress(hinstLib, "functionname");
char p = (char) parity; //(Typecasting jchar to char)
char *pchar = &p; //(Creating a pointer to p)
initcon(..., *pchar, ...);
}
When I try to compile this code, it throws following error: "Invalid conversion from char to char*"
Upvotes: 0
Views: 201
Reputation: 100123
jchar
is a single 16-bit, UTF-16, Unicode character. '&' of it is not char *
, and will not work passed to a char *
. You need to convert it to the ACP and null-terminate it. WideCharToMultiByte
might prove helpful.
Upvotes: 0
Reputation: 3501
As @bmargulies already pointed out, the problem here is that you are not passing pchar
(which is of type (char *)
, but *pchar
. *
is a dereferencing operator and it means "give me whatever is at the address pointed to by pchar
. In other words - a single char
.
Upvotes: 2
Reputation: 11
If you look at your function parameters you'll see that parity
is coming in as a char *
, yet further down you have this line:
char p = (char) parity;
You're casting a char pointer to a char. You don't need to do this based upon what you appear to need on the next line:
char *pchar = &p;
You already have parity as a char *
as a result of the parameter being passed in.
Upvotes: 0