Thread: jbyteArray to LPBYTE conversion

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    jbyteArray to LPBYTE conversion

    I'm a beginner in Windows Programming or I'll say I don't know much at all.
    In my program, I'm using JNI to import a DLL file written in Windows API into my Java program.

    In the C program that direct the JNI variables into DLL Functions, I'm required to convert jbyte* which is similar to byte * to LPBYTE as the function parameters are of LPBYTE type.

    I've tried the code below as a separate running program and it works.
    Code:
    	byte t2 ='4';
    	printf( "number value :  %c\n", t2 );
    	printf( "number address : %p\n", &t2 );
    	
    	LPBYTE test = new byte[256];
    	test = &t2;
    	printf("test address : %p\n",test);
    	printf("test value : %c\n\n",*test);
    However, when I port it into my real program using this code:

    Code:
    	jbyte *lpbMRZ_local = (*env)->GetByteArrayElements(env, lpbMRZ, 0);
    	jsize len = (*env)->GetArrayLength(env, lpbMRZ);
            int i=0, j = 0;
    	DWORD slen=len;
    	printf("Length = %d %lu\n",len, slen);
    
    	for( i=0; i<len; i++)
    		printf("%lu ",lpbMRZ_local[i]);
    	printf("\n");
    
    	LPBYTE lpbMRZ_lp = &lpbMRZ_local;
    I get this error, referring to

    Code:
    C:\eclipse\workspace\ePPTestApp\src>cl -IC:\j2sdk1.4.2_12\include -IC:\j2sdk1.4.2_12\include\win32      -LD JavaBACInterface.c -Febacinterface.dll
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
    Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.
    
    JavaBACInterface.c
    JavaBACInterface.c(46) : error C2275: 'LPBYTE' : illegal use of this type as an expression
    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\WinDef.h(150) : see declaration of 'LPBYTE'
    JavaBACInterface.c(46) : error C2146: syntax error : missing ';' before identifier 'lpbMRZ_lp'
    JavaBACInterface.c(46) : error C2144: syntax error : '<Unknown>' should be preceded by '<Unknown>'
    JavaBACInterface.c(46) : error C2144: syntax error : '<Unknown>' should be preceded by '<Unknown>'
    JavaBACInterface.c(46) : error C2143: syntax error : missing ';' before 'identifier'
    JavaBACInterface.c(46) : error C2065: 'lpbMRZ_lp' : undeclared identifier
    JavaBACInterface.c(46) : warning C4047: '=' : 'int' differs in levels of indirection from 'jbyte ** '
    I've been trying of all sort of ways to perform the conversion from byte* to LPBYTE for the week. And this is my last resort. I don't even know what's wrong.

    So, may I know how to convert common byte* into LPBYTE?

    P/S: I post the question here as it involves LPBYTE, windows api data type. Please inform me if I should post elsewhere.

  2. #2
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Code:
    jbyte* lpbMRZ_local;
    .....
    LPBYTE lpbMRZ_lp = &lpbMRZ_local;
    Your lpbMRZ_local is already a pointer, which means the value it contains is an address. LPBYTE is the same as saying BYTE*, which means it should be assigned the address of a BYTE type, or similar to it. By using the & operator, you're assigning lpbMRZ_lp the address of an LPBYTE, a double pointer (BYTE**), rather than a single (BYTE*). That's probably your problem with converting types. However, I use Dev-C++, and things may be different with MSV-C++, but if I got those errors in Dev, I'd say there's a few syntax errors, somewhere else in your code. But, that small bit, without the environment variables you use, compiles fine with that change.
    Code:
    void function(void)
     {
      function();
     }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    I've actually tried that. I thought my pointer referencing was invalid so I tried the code below earlier. I wonder if anybody has done JNI to import a ready-built DLL library.

    Code:
    LPBYTE lpbMRZ_lp = lpbMRZ_local;

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    I solved it by copying the content one by one. I should have thought about this earlier.

    Code:
    LPBYTE convert2LPBYTE(unsigned char * input,int input_size){
    	DWORD size = (DWORD)input_size;
    	printf("size input: %lu\n",size);
    	LPBYTE output = (LPBYTE)malloc(size*sizeof(input[0]));
    	for(DWORD i = 0 ; i <size; i++){
    		output[i] = input[i];
    		printf("%c",output[i]);
    	}
    	printf("\n");
    	return output;
    }
    Anyway, I failed to retrieve the real sizeof(input) as it's always only fixed at 4. So, I have another parameter to fix the size of each array to be converted.

    Any better idea?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, that's exactly the correct way. sizeof is not the right tool for getting the size of a runtime array, despite its name.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    The array in the parameter has fixed size instead of being dynamically allocated. So, its size should be retrievable by using sizeof ?
    What else can i do?

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, only the array you pass to the parameter is fixed size. The parameter is just a pointer, not an array.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. byte array to lpbyte conversion
    By navygreen in forum C Programming
    Replies: 2
    Last Post: 09-26-2006, 08:12 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM