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$
<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.
<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.
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.
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);
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.
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 .
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;
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;
}
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
#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;
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";*/
/*
"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"
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
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.
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
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.
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???