Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

pointer targets in passing argument 1 of 'strlen' differ in signedness

length= strlen(aProcessName);
str=(char*)malloc(length);
str=aProcessName;
str[strlen(aProcessName)]='\0';
str[strlen(aProcessName)-1]=' ';
buff=(char*)malloc(length+1);
tempbuff=(char*)malloc(length);

The above c code throws the fallowing warnings
would any one helpme to over come this problem i am new user of c and mac



main.c: In function 'VersionAppRunning':
main.c:210: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness
main.c:212: warning: pointer targets in assignment differ in signedness
main.c:213: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness
main.c:214: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness
main.c: In function 'Java JNIWrappergetWord':
main.c:513: warning: return makes pointer from integer without a cast
administrators-computer:~/Desktop/Wconnect administrator$


Thankyou in advance
Anbu



I Mac Mac OS X (10.4.8) Intel based Mac

I Mac Mac OS X (10.4.8) Intel based Mac

I Mac Mac OS X (10.4.8) Intel based Mac

Posted on Apr 6, 2007 4:12 AM

Reply
Question marked as Best reply

Posted on Apr 6, 2007 7:16 AM

<pre>length= strlen(aProcessName);
str=(char*)malloc(length);
str=aProcessName;
str[strlen(aProcessName)]='\0';
str[strlen(aProcessName)-1]=' ';
buff=(char*)malloc(length+1);
tempbuff=(char*)malloc(length);</pre>
main.c: In function 'VersionAppRunning':
main.c:210: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness
main.c:212: warning: pointer targets in assignment differ in signedness
main.c:213: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness
main.c:214: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness
main.c: In function 'JavaJNIWrappergetWord':
main.c:513: warning: return makes pointer from integer without a cast


You didn't post the entire function, what is the type of aProcessName? The compiler is implying that the type is unsigned char *. Therefore, it may not be a real, null-terminated C string. That first call to strlen() could very well crash your program as it is going to search for a null value that may not be there. Your subsequent efforts at creating a null-terminated string will not help you. Your code assumes aProcessName is already a null terminated string.
13 replies
Question marked as Best reply

Apr 6, 2007 7:16 AM in response to Anbu

<pre>length= strlen(aProcessName);
str=(char*)malloc(length);
str=aProcessName;
str[strlen(aProcessName)]='\0';
str[strlen(aProcessName)-1]=' ';
buff=(char*)malloc(length+1);
tempbuff=(char*)malloc(length);</pre>
main.c: In function 'VersionAppRunning':
main.c:210: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness
main.c:212: warning: pointer targets in assignment differ in signedness
main.c:213: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness
main.c:214: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness
main.c: In function 'JavaJNIWrappergetWord':
main.c:513: warning: return makes pointer from integer without a cast


You didn't post the entire function, what is the type of aProcessName? The compiler is implying that the type is unsigned char *. Therefore, it may not be a real, null-terminated C string. That first call to strlen() could very well crash your program as it is going to search for a null value that may not be there. Your subsequent efforts at creating a null-terminated string will not help you. Your code assumes aProcessName is already a null terminated string.

Apr 6, 2007 9:51 PM in response to etresoft

Hi etresoft


I change the code as


Str31 aProcessName;

......
......


208 length=strlen((char*) aProcessName);
str=(char*)malloc(length);
210 str=(char*)aProcessName;
211 str[strlen(char*)aProcessName)]='\0';
212 str[strlen((char*)aProcessName)-1]=' ';
buff=(char*)malloc(length+1);
tempbuff=(char*)malloc(length);


The warnings go off but still I have one more warning
main.c:512: warning: return makes pointer from integer without a cast

the code that throws the warning is

bail:
AEDisposeDesc(&scriptTextDesc);
if (scriptID != kOSANullScript) OSADispose(theComponent, scriptID);
if (resultID != kOSANullScript) OSADispose(theComponent, resultID);
if (theComponent != NULL) CloseComponent(theComponent);
return err;


i declare err as OSStatus err;

would you suggest me , how to over come this warning


thank you
Anbu


I Mac Mac OS X (10.4.8) Intel based Mac

I Mac Mac OS X (10.4.8) Intel based Mac

Apr 6, 2007 11:18 PM in response to Anbu

Anbu,

etresoft brings up a good point, which was also mentioned by Dale Ranta in your other post here.

Str31 is a Pascal style string - a length byte followed by the actual characters. It's not a C style string - characters terminated by a null byte. I do not believe a Str31 is guaranteed to have a null byte on the end. So if you attempt to find the length using strlen(), which assumes you've given it a null-terminated string, you will potentially get garbage for the result. Type-casting aProcessName to a (char *) may get rid of the warning but it doesn't fix the underlying problem of trying to use strlen() on something that may not be a valid C string.

Re-read Dale's post in your other thread. He gives you the technique for finding the length of a Pascal string.

Steve

Apr 7, 2007 3:02 PM in response to Anbu

The warnings go off but still I have one more warning


That is why people say the C-style cast is a bad idea. It just makes warnings go away when the real problem is still there.

main.c:512: warning: return makes pointer from
integer without a cast

the code that throws the warning is

bail:
AEDisposeDesc(&scriptTextDesc);
if (scriptID != kOSANullScript)
) OSADispose(theComponent, scriptID);
if (resultID != kOSANullScript)
) OSADispose(theComponent, resultID);
if (theComponent != NULL)
) CloseComponent(theComponent);
return err;


i declare err as OSStatus err;

would you suggest me , how to over come this warning


How is your function declared? Clearly it doesn't look like:<pre>OSStatus foo(Str31 aProcessName)</pre>It returns some kind of pointer and you aren't giving it a pointer - that is bad, bad, bad.

I am particularly intrigued by the line:<pre>bail:</pre>I've never used that construct, but I know what it is. It is a label. You are using goto aren't you? That is another bad idea.

As Steve pointed out, you aren't dealing with C-strings. That is a Pascal string. One way (out of many) to convert a C-string into a Pascal string is:<pre>
char * getCProcessName(Str31 aProcessName)
{
if(aProcessName[0])
{
char * result = (char *)malloc(aProcessName[0] + 1);

strncpy(result, (char *)aProcessName + 1, aProcessName[0]);
result[aProcessName[0] = 0;

return result;
}

return 0;
}</pre>
Don't forget to check the result for null. If the result isn't null, don't forget to delete it. Hopefully my code doesn't have errors, but I haven't check it and I haven't written any C for about 6 years.

Feel free to post more of your code. The more you post, the more responses will be.

Apr 8, 2007 10:29 PM in response to etresoft

This is the function that throws the warnings,
This code is not written by me, and i am not a c or carbon programer i know only java, by using this file i am making a jnilib file that will be loaded by using System.loadLibarey("libWconnecjnilib"); as class .

i use the following commands in the terminal

Shell>cc -arch ppc -arch i386 -c -I/System/Library/Frameworks/JavaVM.framework/Headers -o main.o main.c

it throws following warnings

main.c: In function 'VersionAppRunning':
main.c:209: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness
main.c:211: warning: pointer targets in assignment differ in signedness
main.c:212: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness
main.c:213: warning: pointer targets in passing argument 1 of 'strlen' differ in signedness

main.c: In function 'Java JNIWrappergetWord':
main.c:449: warning: return makes pointer from integer without a cast

void VersionAppRunning()
{

char buff,tempbuff;
char *str;
int length;
int i=0;
int j=0;
int flag;
int k=0;
int count=0;

ProcessSerialNumber PSN;
ProcessInfoRec info;
Str31 aProcessName;
OSErr err;
FSSpec aProcessAppSpec;
PSN.highLongOfPSN=0;

PSN.lowLongOfPSN=kNoProcess;
info.processInfoLength=sizeof(ProcessInfoRec);
info.processName=aProcessName;
info.processAppSpec=&aProcessAppSpec;


while(GetNextProcess(&PSN)==noErr)
{
if(GetProcessInformation(&PSN,&info)==noErr)
{
length= strlen(aProcessName);
str=(char*)malloc(length);
str= aProcessName;
str[strlen(aProcessName)]='\0';
str[strlen(aProcessName)-1]=' ';
buff=(char*)malloc(length+1);
tempbuff=(char*)malloc(length);
while(str !='\0')
{
if(i==0 )
{
i++;
flag=1;
}
buff[j]=str
;
tempbuff[k]=buff[j];
if(buff[j]==32)
{
tempbuff[k]='\0';
if(tempbuff[3]=='d')
{
tempbuff[4]='\0';
}
if(tempbuff[7]=='r')
{
tempbuff[8]='\0';
}

if(strcmp(tempbuff,"Internet")==0)
{
count=count+1;
tempbuff=NULL;
k=0;
k=k-1;
tempbuff=(char*)malloc(strlen(str));
}
if(count>=1 && strcmp(tempbuff,"Explorer")==0)

{

IE=1;
tempbuff=NULL;
k=0;
k=k-1;
tempbuff=(char*)malloc(strlen(str));
count=0;

}

if(strcmp(tempbuff,"Microsoft")==0)
{
count=count+1;
tempbuff=NULL;
k=0;
k=k-1;
tempbuff=(char*)malloc(strlen(str));
}
if(count>=1 && strcmp(tempbuff,"Word")==0)
{
MW=1;
tempbuff=NULL;
k=0;
k=k-1;
tempbuff=(char*)malloc(strlen(str));
count=0;

}


}
j++;
i++;
k++;

}
if(flag==1)
{
buff[i-1]='\0';
tempbuff[k]='\0';
}
else
{
buff ='\0';
tempbuff[k]='\0';
}
free(tempbuff);
free(buff);
tempbuff=NULL;
buff=NULL;
k=0;
i=0;
j=0;
}
}
}
JNIEXPORT jstring JNICALL JavaJNIWrappergetWord(JNIEnv *env , jobject obj,jstring j_script)
{
ComponentInstance theComponent;
AEDesc scriptTextDesc,resultData1;
char *returnChar;
const char *text;
jstring jstr;
Size returnSize;
OSStatus err;
OSAID scriptID, resultID;
theComponent = NULL;
AECreateDesc(typeNull, NULL, 0, &scriptTextDesc);
scriptID = kOSANullScript;
resultID = kOSANullScript;
VersionAppRunning();
if(IE==1 && MW==0)
{
text="tell application \"Internet Explorer\" \n"
"set theString to get selected text as string \n"
"if length of theString is not equal to 0 then \n"
"return theString \n"
"else \n"
"return \"NOWORD\" \n"
"end if \n"
"end tell \n";
}
if(MW==1 && IE==0)
{
text="try \n"
"tell application \"Microsoft Word\" \n"
"set str to get selection as string \n"
"if length of str is not equal to 0 then \n"
"return str \n"
"end if \n"
"end tell \n"
"on error \n"
"return \"NOWORD\" \n"
"end try \n";
}
if(MW==1 && IE==1)
{
text= "try \n"
"tell application \"Internet Explorer\" \n"
"set stre to get selected text as string \n"
"if length of stre is not equal to 0 then \n"
"return stre \n"
"end if \n"
"tell application \"Microsoft Word\" \n"
"set str to get selection as string \n"
"if length of str is not equal to 0 then \n"
"return str \n"
"end if \n"
"end tell \n"
"on error \n"
"return \"NOWORD\" \n"
"end try \n"
"return \"NOWORD\" \n";
}
if(MW==0 && IE==0)
{
text="return \"NOWORD\" \n";
}
MW=0;
IE=0;
theComponent = OpenDefaultComponent(kOSAComponentType,
typeAppleScript);
if (theComponent == NULL) { err = paramErr; goto bail; }
/* put the script text into an aedesc */
err = AECreateDesc(typeChar, text,strlen(text), &scriptTextDesc);
if (err != noErr) goto bail;
/* compile the script */
err = OSACompile(theComponent, &scriptTextDesc,
kOSAModeNull, &scriptID);
if (err != noErr) goto bail;
/* run the script/get the result */
err = OSAExecute(theComponent, scriptID, kOSANullScript,
kOSAModeNull, &resultID);
if( err ==noErr)
{
err=AECreateDesc(typeNull, NULL, 0, &resultData1);
if( err ==noErr)
{
err=OSADisplay(theComponent,resultID,typeChar,kOSAModeNull,&resultData1);
returnSize=AEGetDescDataSize(&resultData1);
returnChar=malloc(returnSize);
AEGetDescData(&resultData1, returnChar,returnSize);
returnChar=Convert(returnChar);
//printf(returnChar);
// return((*env)->NewStringUTF(returnChar));
jstr=(*env)->NewStringUTF(env,returnChar);
return jstr;
}
}
bail:
AEDisposeDesc(&scriptTextDesc);
if (scriptID != kOSANullScript) OSADispose(theComponent, scriptID);
if (resultID != kOSANullScript) OSADispose(theComponent, resultID);
if (theComponent != NULL) CloseComponent(theComponent);
return err;
}

Apr 9, 2007 7:38 AM in response to Anbu

This code is not written by me,


That's a relief!

It appears that this code checks to see if either Internet Explorer or Microsoft Word are running and, if they are, extracts any selected text.

What are you actually trying to do here? First of all, nobody runs Internet Explorer anymore so that is a waste of your time. I'm sure you can find better code that will determine what applications are running. Then, if Word is running, you can execute that 3 line Apple Script and get the result in a much better way too.

The code barely works, if at all. It leaks memory. It stands a good chance of crashing. I have no doubt that you could write better code yourself, even if you only know Java.

Unfortunately, this isn't code that can be fixed. If it is critical to what you are doing, you'll have to figure out how to do it correctly or find someone who can.

Otherwise, I suggest you replace the code with the following:<pre>
void VersionAppRunning()
{
IE = 0;
MW = 0;
}

JNIEXPORT jstring JNICALL Java JNIWrappergetWord(JNIEnv *env , jobject obj,jstring j_script)
{
return((*env)->NewStringUTF("NOWORD"));
}</pre>

Apr 9, 2007 9:15 AM in response to etresoft

Hi

The aim of the application I am developing is to fetch word from MS Word or MS IE application, and display the meaning for that word in window (ie a dictionary), for this we use java swings, JNI, carbon, and some apple script, this application was completed before 2003, the problem now is it is not working properly in Intel based Mac, my aim is to make it work on Intel based Mac.

In this application we are having jnilib file which is generated from the c file, I think it is not loaded properly when load using the java function System.loadLibarey(), I think there might bean version conflict of this jnilib file, so I am recompiling this file to generate the new jnilib file.

The code in my last post is to fetch the word from IE & MS Word, if there is any thing wrong in our code kindly mention it

Thank you
Anbu


I Mac Mac OS X (10.4.8) Intel based Mac

Apr 9, 2007 9:25 AM in response to etresoft

This is the full code of my .c file

/* main.c */

#include <Carbon/Carbon.h>
#include <jni.h>
#include "main.h"
#include <JavaVm/jni.h>
#include <Strings.h>
int IE=0;
int MW=0;
char *Convert(char *pt)
{

int len;
int i;
int j;
int k;
int ii;
int h;
char ch[25];
wchar_t ptr=(wchar_t*)malloc(strlen(pt)4);
char pts=(char*)malloc(strlen(pt)6+1);
mbstowcs(ptr,pt,strlen(pt));
len =strlen(pt);
i=0;
while(len!=0)
{
//printf("\n %d \n",ptr );
i++;
len--;
}
ptr
='\0';

j=0;
k=0;
h=0;


while(*ptr!='\0')
{
pts[k]='&';
pts[++k]='#';
ii=ptr[j];
if(ii==131)
{
ptr++;
ii=*ptr;
switch (ii)
{
case 159:ii=913;sprintf(ch,"%d",ii);break;
case 160:ii=914;sprintf(ch,"%d",ii);break;
case 161:ii=914;sprintf(ch,"%d",ii);break;
case 162:ii=916;sprintf(ch,"%d",ii);break;
case 163:ii=917;sprintf(ch,"%d",ii);break;
case 164:ii=918;sprintf(ch,"%d",ii);break;
case 165:ii=919;sprintf(ch,"%d",ii);break;
case 166:ii=920;sprintf(ch,"%d",ii);break;
case 167:ii=921;sprintf(ch,"%d",ii);break;
case 168:ii=922;sprintf(ch,"%d",ii);break;
case 169:ii=923;sprintf(ch,"%d",ii);break;
case 170:ii=924;sprintf(ch,"%d",ii);break;
case 171:ii=925;sprintf(ch,"%d",ii);break;
case 172:ii=926;sprintf(ch,"%d",ii);break;
case 173:ii=927;sprintf(ch,"%d",ii);break;
case 174:ii=928;sprintf(ch,"%d",ii);break;
case 175:ii=929;sprintf(ch,"%d",ii);break;
case 176:ii=931;sprintf(ch,"%d",ii);break;
case 177:ii=932;sprintf(ch,"%d",ii);break;
case 178:ii=933;sprintf(ch,"%d",ii);break;
case 179:ii=934;sprintf(ch,"%d",ii);break;
case 180:ii=935;sprintf(ch,"%d",ii);break;
case 181:ii=936;sprintf(ch,"%d",ii);break;


case 191:ii=945;sprintf(ch,"%d",ii);break;
case 192:ii=946; sprintf(ch,"%d",ii);break;
case 193:ii=947;sprintf(ch,"%d",ii);break;
case 194:ii=948;sprintf(ch,"%d",ii);break;
case 195:ii=949;sprintf(ch,"%d",ii);break;
case 196:ii=950;sprintf(ch,"%d",ii);break;
case 197:ii=951;sprintf(ch,"%d",ii);break;
case 198:ii=952;sprintf(ch,"%d",ii);break;
case 199:ii=953;sprintf(ch,"%d",ii);break;
case 200:ii=954;sprintf(ch,"%d",ii);break;
case 201:ii=955;sprintf(ch,"%d",ii);break;
case 202:ii=956;sprintf(ch,"%d",ii);break;
case 203:ii=957;sprintf(ch,"%d",ii);break;
case 204:ii=958;sprintf(ch,"%d",ii);break;
case 205:ii=959;sprintf(ch,"%d",ii);break;

case 207:ii=961;sprintf(ch,"%d",ii);break;

case 208:ii=963;sprintf(ch,"%d",ii);break;
case 209:ii=964;sprintf(ch,"%d",ii);break;
case 210:ii=965;sprintf(ch,"%d",ii);break;
case 211:ii=966;sprintf(ch,"%d",ii);break;
case 212:ii=967;sprintf(ch,"%d",ii);break;
case 213:ii=968;sprintf(ch,"%d",ii);break;
case 214:ii=969;sprintf(ch,"%d",ii);break;
}

}
else if(ii==63||ii==205||ii==206||ii==216||ii==217||ii==189||ii==192||ii==219||ii==2 20||ii==221||ii==185||ii==86||ii==222||ii==224||ii==241||ii==46||ii==47||ii==63| |ii==171||ii==142||ii==241||ii==225||ii==150||ii==136||ii==137||ii==141||ii==143 ||ii==158)
{
switch(ii)
{
case 46:ii=183;sprintf(ch,"%d",ii);break;
case 47:ii=824;sprintf(ch,"%d",ii);break;
case 63:ii=821;sprintf(ch,"%d",ii);break;
case 171:ii=180;sprintf(ch,"%d",ii);break;
case 185:ii=960;sprintf(ch,"%d",ii);break;
case 86:ii=962;sprintf(ch,"%d",ii);break;
case 205:ii=902;sprintf(ch,"%d",ii);break;
case 206:ii=904;sprintf(ch,"%d",ii);break;
case 216:ii=906;sprintf(ch,"%d",ii);break;
case 217:ii=908;sprintf(ch,"%d",ii);break;
case 142:ii=233;sprintf(ch,"%d",ii);break;
case 150:ii=241;sprintf(ch,"%d",ii);break;
case 225:ii=183;sprintf(ch,"%d",ii);break;
case 136:ii=224;sprintf(ch,"%d",ii);break;
case 137:ii=226;sprintf(ch,"%d",ii);break;
case 141:ii=231;sprintf(ch,"%d",ii);break;
case 143:ii=232;sprintf(ch,"%d",ii);break;
case 158:ii=251;sprintf(ch,"%d",ii);break;


case 189:ii=937;sprintf(ch,"%d",ii);break;
case 192:ii=940;sprintf(ch,"%d",ii);break;
case 219:ii=941;sprintf(ch,"%d",ii);break;
case 220:ii=942;sprintf(ch,"%d",ii);break;
case 221:ii=943;sprintf(ch,"%d",ii);break;
case 222:ii=972;sprintf(ch,"%d",ii);break;
case 224:ii=973;sprintf(ch,"%d",ii);break;
case 241:ii=974;sprintf(ch,"%d",ii);break;


}

}
else
{
k=k-1;
pts[k]=(char)ptr[j];
pts[k+1]='\0';
k++;
ptr++;
continue;

}



while(ch[h]!='\0')
{
pts[++k]=ch[h];
h++;

}
h=0;
pts[++k]=';';

ptr++;
k++;
}
pts[k]='\0';
return pts;

/*
printf("\n The actual value is %s",pts);
pts='\0';
ptr='\0';
free(pts);
free(ptr);*/


}

void VersionAppRunning()
{

char buff,tempbuff;
char *str;
int length;
int i=0;
int j=0;
int flag;
int k=0;
int count=0;

ProcessSerialNumber PSN;
ProcessInfoRec info;
Str31 aProcessName;
OSErr err;
FSSpec aProcessAppSpec;
PSN.highLongOfPSN=0;

PSN.lowLongOfPSN=kNoProcess;
info.processInfoLength=sizeof(ProcessInfoRec);
info.processName=aProcessName;
info.processAppSpec=&aProcessAppSpec;



while(GetNextProcess(&PSN)==noErr)
{
if(GetProcessInformation(&PSN,&info)==noErr)
{
length=strlen( (char*) aProcessName);
str=(char*)malloc(length);
str=(char*)aProcessName;
str[strlen((char*)aProcessName)]='\0';
str[strlen((char*)aProcessName)-1]=' ';
buff=(char*)malloc(length+1);
tempbuff=(char*)malloc(length);
while(str !='\0')
{
if(i==0 /*&& str[str[0]==32*/)
{
i++;
flag=1;
}
buff[j]=str
;
tempbuff[k]=buff[j];
if(buff[j]==32)
{
tempbuff[k]='\0';
if(tempbuff[3]=='d')
{
tempbuff[4]='\0';
}
if(tempbuff[7]=='r')
{
tempbuff[8]='\0';
}

if(strcmp(tempbuff,"Internet")==0)
{
count=count+1;
tempbuff=NULL;
k=0;
k=k-1;
tempbuff=(char*)malloc(strlen(str));
}
if(count>=1 && strcmp(tempbuff,"Explorer")==0/* (strcmp(tempbuff,"Explorer")==0||strcmp(tempbuff,"Explorere")==0)*/)

{

IE=1;
tempbuff=NULL;
k=0;
k=k-1;
tempbuff=(char*)malloc(strlen(str));
count=0;

}

if(strcmp(tempbuff,"Microsoft")==0)
{
count=count+1;
tempbuff=NULL;
k=0;
k=k-1;
tempbuff=(char*)malloc(strlen(str));
}
if(count>=1 && strcmp(tempbuff,"Word")==0/ (strcmp(tempbuff,"Wordrll")==0||strcmp(tempbuff,"Wordrer")==0)/)
{
MW=1;
tempbuff=NULL;
k=0;
k=k-1;
tempbuff=(char*)malloc(strlen(str));
count=0;

}


}
j++;
i++;
k++;

}
if(flag==1)
{
buff[i-1]='\0';
tempbuff[k]='\0';
}
else
{
buff ='\0';
tempbuff[k]='\0';
}
//printf("%s\n",buff);
//printf("%d\n",IE);
//printf("%d\n",MW);
//printf("%s\n",tempbuff);
free(tempbuff);
free(buff);
tempbuff=NULL;
buff=NULL;
k=0;
i=0;
j=0;
/*
while(length!=0)
{
buff
=aProcessName ;
if(buff
=='\0')
{
tempBuff=buff;
printf("%s \n",tempBuff);

}

length=length-1;

}
*/


}


}
// printf("%d\n",IE);
// printf("%d\n",MW);
}


JNIEXPORT jstring JNICALL Java JNIWrappergetWord(JNIEnv *env , jobject obj,jstring j_script)
//(JNIEnv *en , jobject obj, jstring str,jstring j_target,jstring j_location)
{
ComponentInstance theComponent;
AEDesc scriptTextDesc,resultData1;
char *returnChar;
const char *text;
jstring jstr;
Size returnSize;
OSStatus err;
OSAID scriptID, resultID;
theComponent = NULL;
AECreateDesc(typeNull, NULL, 0, &scriptTextDesc);
scriptID = kOSANullScript;
resultID = kOSANullScript;

VersionAppRunning();
if(IE==1 && MW==0)
{
text="tell application \"Internet Explorer\" \n"
"set theString to get selected text as string \n"
"if length of theString is not equal to 0 then \n"
"return theString \n"
"else \n"
"return \"NOWORD\" \n"
"end if \n"
"end tell \n";
}
if(MW==1 && IE==0)
{
text="try \n"
"tell application \"Microsoft Word\" \n"
"set str to get selection as string \n"
"if length of str is not equal to 0 then \n"
"return str \n"
"end if \n"
"end tell \n"
"on error \n"
"return \"NOWORD\" \n"
"end try \n";
}


if(MW==1 && IE==1)
{


/* text= "tell application \"Microsoft Word\" \n"
"set str to get selection as string \n"
"if length of str is 0 then \n"
"tell application \"Internet Explorer\" \n"
"set stre to get selected text as string \n"
"if length of stre is 0 then \n"
"return \"Word from I.E or word document is not selected\" \n"
"else \n"
"return stre \n"
"end if \n"
"end tell \n"
"else \n"
"return str \n"
"end if \n"
"end tell \n";*/


/*text ="tell application \"Microsoft Word\" \n"
"activate \n"
"do Visual Basic \"\n"
"Dim objwrd As Object \n"
"Dim filenum As Interger \n"
"Set objwrd=GetObject(,\"Word.Application\") \n"
"Dim str as String \n"
"str=objwrd.Selection.Words(1).Text \n"
"filenum=FreeFile \n"
"Open \"sample.txt\" For Output As filenum \n"
"Print #filenum, str \n"
"Close #filenum \n"
" \" \n"
"return \"hai\" \n"
"end tell \n" ;*/
text=

/*
"set bMsWord to boolean \n"
"set bInternet to boolean \n"
"tell application \"Finder\" \n"
"set myList to every process as list \n"
"end tell \n"
"repeat with i from 1 to count of myList \n"
"set myItem to (item i of myList) \n"
"set VIS to visible of (item i of myList) \n"

"if VIS is true and (name of myItem is \"Microsoft Word\" or name of myItem is \"Internet Explorer\") then \n"
"if name of myItem is \"Microsoft Word\" then \n"
"set bMsWord to true \n"

"else \n"
"set bInternet to true \n"

"end if \n"

"end if \n"

"end repeat \n"*/

"try \n"
// "if bInternet is true then \n"
"tell application \"Internet Explorer\" \n"
"set stre to get selected text as string \n"
"if length of stre is not equal to 0 then \n"
"return stre \n"
"end if \n"
"end tell \n"
// "end if \n"

// "if bMsWord is true then \n"
"tell application \"Microsoft Word\" \n"
"set str to get selection as string \n"
"if length of str is not equal to 0 then \n"
"return str \n"
"end if \n"
"end tell \n"
// "end if \n"

"on error \n"
"return \"NOWORD\" \n"
"end try \n"
"return \"NOWORD\" \n";
}
if(MW==0 && IE==0)
{

text="return \"NOWORD\" \n";
}
MW=0;
IE=0;



theComponent = OpenDefaultComponent(kOSAComponentType,
typeAppleScript);
if (theComponent == NULL) { err = paramErr; goto bail; }
/* put the script text into an aedesc */
err = AECreateDesc(typeChar, text,strlen(text), &scriptTextDesc);
if (err != noErr) goto bail;
/* compile the script */
err = OSACompile(theComponent, &scriptTextDesc,
kOSAModeNull, &scriptID);
if (err != noErr) goto bail;
/* run the script/get the result */
err = OSAExecute(theComponent, scriptID, kOSANullScript,
kOSAModeNull, &resultID);
if( err ==noErr)
{
err=AECreateDesc(typeNull, NULL, 0, &resultData1);
if( err ==noErr)
{
err=OSADisplay(theComponent,resultID,typeChar,kOSAModeNull,&resultData1);
returnSize=AEGetDescDataSize(&resultData1);
returnChar=malloc(returnSize);
AEGetDescData(&resultData1, returnChar,returnSize);
returnChar=Convert(returnChar);
//printf(returnChar);
// return((*env)->NewStringUTF(returnChar));
jstr=(*env)->NewStringUTF(env,returnChar);
return jstr;

}

}
bail:
AEDisposeDesc(&scriptTextDesc);
if (scriptID != kOSANullScript) OSADispose(theComponent, scriptID);
if (resultID != kOSANullScript) OSADispose(theComponent, resultID);
if (theComponent != NULL) CloseComponent(theComponent);
return (jstring)err;


}


I Mac Mac OS X (10.4.8) Intel based Mac

Apr 9, 2007 2:51 PM in response to Anbu

The aim of the application I am developing is to
o fetch word from MS Word or MS IE application, and
display the meaning for that word in window (ie a
dictionary), for this we use java swings, JNI,
carbon, and some apple script, this application was
completed before 2003, the problem now is it is not
working properly in Intel based Mac, my aim is to
make it work on Intel based Mac.


You might try changing your build script. You were using:
cc -arch ppc -arch i386 -c -I/System/Library/Frameworks/JavaVM.framework/Headers -o main.o main.c

instead try:
gcc -isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch ppc -arch i386 -c -I/System/Library/Frameworks/JavaVM.framework/Headers -o main.o main.c

It that doesn't work, you might want to further specify just what isn't working on the Intel Mac. Take the old version, add some debugging code, and try to find out just where it is failing. I think there are two possiblities:

1) The error could be in the Intel version of the Java-C bridge. There could be a problem running PPC code on an Intel machine trying to get information about and data from old PPC code running on that machine.

2) The Intel chip is being less forgiving of poor pointer arithmetic.

In this application we are having jnilib file which
h is generated from the c file, I think it is not
loaded properly when load using the java function
System.loadLibarey(), I think there might bean
version conflict of this jnilib file, so I am
recompiling this file to generate the new jnilib
file.

The code in my last post is to fetch the word from
m IE & MS Word, if there is any thing wrong in our
code kindly mention it


The code casts Pascal strings as C strings and then performs C functions on them. This is a showstopper. It must be fixed before you can proceed. It even looks like there were extra string comparisons added because the process name contained garbage("Explorere", "Wordii", etc.). I wonder why?

The code is poorly documented. It is difficult for me to find out what is going on and why. That new code you just posted is just incomprehensible.

There are better ways of accomplishing what you are trying than through this C code. Try some pure Applescript perhaps. It seems as if this code is taking the most difficult path to the solution.

Apr 11, 2007 4:34 AM in response to etresoft

Hi etresoft

The problem here is the apple script code

tell application "Microsoft Word"
set str to get selection as string
if length of str is not equal to {} then
return str
else
return "NOWORD"
end if
end tell

Is not working properly, this apple script is suppose to return the selected word from Microsoft word, but its only returning noword

Thank you


I Mac Mac OS X (10.4.8) Intel based Mac

Apr 11, 2007 10:29 AM in response to Anbu

Steve,
Anbu did say that the code had been working since 2003 and only started failing on the Intel machines.

Anbu,
Are you positively sure that it is your AppleScript code that is no longer working? It is possible that the getRunningProcess() function is failing and not setting MW to 1. If that is happening, you just get "NOWORD". You would have to either step through the code in the debugger or add debug AppleScript that gets executed. Since this is all part of some C code being executed by Java, I think you should probably try adding some AppleScript alerts so you know for sure which script is being run.

There could also be a problem in the C code that executes the AppleScript. That particular section of the code, even with the "goto" looks OK. Still, I'm no AppleScript expert and it could be different or less forgiving on Intel.

Apr 11, 2007 11:17 AM in response to etresoft

Steve,
Anbu did say that the code had been working since
2003 and only started failing on the Intel machines.


Oops, I didn't catch that part... but I did try running the AppleScript snippet in Script Editor and it doesn't work for me - and this was on a PPC Mac.

The "get selection as string" line errors indicating it "can't make the selection into type string". This was with a MS Word document open that contained only a couple of sentences and a single word highlighted. Removing the "as string" allows the script to run but it still would not get the selected text.

I only fiddled with it for about 10 minutes but I wasn't ever able to get Word to return anything that was selected back to AppleScript. So, I figured it was something kind of goofy with Word's AppleScript implementation but really didn't dig any deeper.

Maybe an Office update has broken something that used to be working???

Steve

pointer targets in passing argument 1 of 'strlen' differ in signedness

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.