Results 1 to 4 of 4

Thread: Casting data type...please help

  1. #1
    Dimension
    Guest

    Casting data type...please help

    #define UNICODE
    #include <jni.h>
    #include "AcrobatFile.h"
    #include <windows.h>

    void launchAcrobat(char fileName[256]);

    JNIEXPORT void JNICALL Java_AcrobatFile_PrintAcrobatFile
    (JNIEnv *env, jobject obj, jstring fileName)
    {
    char acrobatFileName[256];

    acrobatFileName = env->GetStringChars(fileName,0);

    launchAcrobat(acrobatFileName);

    acrobatFileName = env->ReleaseStringChars(fileName,acrobatFileName);
    }

    I'm trying to make the following code work but i keep getting the following errors

    [ error C2440: '=' : cannot convert from 'const unsigned short *' to 'char [256]'
    [error C2664: 'ReleaseStringChars' : cannot convert parameter 2 from 'char [256]' to 'const unsigned short *'

    My question is. How do i convert acrobatFileName[256] to a const unsigned short *? Really appreciate the help. Been stuck on this for a while now. Thanks!

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    If you declare a string as an array of characters on the stack, like this:
    Code:
    char acrobatFileName[256];
    then what you have actually done is reserved 256 bytes on the stack for your app, and got yourself a constant pointer to the first byte.

    You can't just reassign arrays using the = operator. You have to assign each element individual, or copy a whole block of memory over, or use a utility function (like strcpy() for instance) to do it for you.

    Anyway, it seems you are also having some type problems.

    char[] is in this case actually exactly the same as const unsigned short *, on 32 bit Windows at least. You should be able to do a simple cast in this case to let the compiler know that you know what you're doing:

    Code:
    strcpy(acrobatFileName, (char *)(env->GetStringChars(fileName,0)));
    You may need to include string.h to get the strcpy() function.
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Don't you have to use Unicode with Java? That would explain the "short" part of it...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    parksie hit it straight:
    Code:
    void launchAcrobat(wchar_t *fileName); 
    
    JNIEXPORT void JNICALL Java_AcrobatFile_PrintAcrobatFile 
    (JNIEnv *env, jobject obj, jstring fileName) 
    { 
    wchar_t acrobatFileName[256]; 
    
    acrobatFileName = env->GetStringChars(fileName,0); 
    
    launchAcrobat(acrobatFileName); 
    
    acrobatFileName = env->ReleaseStringChars(fileName,acrobatFileName); 
    }
    You must use UNICODE characters.

    Harry: those are NOT the same, never have been and never will be. Don't make that mistake, it can cause strange and hard-to-find errors.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width