I am about to transfer a project I have written in Applescript and Objective C to Excel/VBA/dll. I have started with the Objective C functions that I want to place in a dll and call via VBA.
First of all, I am a statistician who managed to build the original Mac program (a statistical tool that have been very handy to me) through equal amounts of luck, trial and error and perseverance. Please take that into account (also if the answer is embarrassingly obvious). I have used this (minus the 64 bit part) as starting point for the dll: http://ift.tt/1lMUwqf
The Objective C is C with a thin dusting of special Obejctive C code to have it talk with Applescript and the rest of the project so in theory it should be easy to make dlls written in C from it.
But I have already problems with the tiniest of all functions. I am sure it can be done more effectively but right now I need to know WHY it doesn´t work if I am ever going to be able to transfer the much larger functions from Objective C to C.
Here is the C code I am trying:
int _stdcall game(int *games, int *gameskifte)
{
if (*games == 0){
if (*gameskifte == 1) return *games + 1;
else return *games + 2;}
else{
if (*games < 3){
if (*gameskifte == 2) return *games + 2;
else return *games + 4;}
else{
if (*gameskifte == 2) return *games + 3;
else return games + 5;
}
}
return 0;
}
Here is what the code is supposed to do:
- If games = 0 and gameskifte = 1, return 1
- if games = 0 (and by default gameskifte is not 1), return 2
- If games < 3 and gameskifte = 1, return games + 2
- If games < 3 (and by default gameshift is not 1), return games + 4
- If games > 2 and gameskifte = 1, return games + 3
- If games > 2 (and by default gameshift is not 1), return games + 5
What it does is ignore the value of gameshift and ALWAYS returns games + 4
I tried to change a few things...
This code (where all compares are "<" but the result should be the same)
int _stdcall game(int *games, int *gameskifte)
{
if (*games < 1){
if (*gameskifte < 2) return *games + 1;
else return *games + 2;}
else{
if (*games < 3){
if (*gameskifte < 2) return *games + 2;
else return *games + 4;}
else{
if (*gameskifte < 2) return *games + 3;
else return games + 5;
}
}
return 0;
}
Always gives me games + 1
And this (where all compares are ">" and where the result should NOT nessesary be the same as above):
int _stdcall game(int *games, int *gameskifte)
{
if (*games > 1){
if (*gameskifte > 2) return *games + 1;
else return *games + 2;}
else{
if (*games > 3){
if (*gameskifte > 2) return *games + 2;
else return *games + 4;}
else{
if (*gameskifte > 2) return *games + 3;
else return games + 5;
}
}
return 0;
}
always gives me games + 5
So when doing the return calculation it knows the value of *games but when comparing *games to another hardcoded value it throws a false when doing a direct or ">" compare and a positive when doing any "<" compare no matter if the value is lower or higher.
The rest of the project is
A def file: defFile.def:
LIBRARY ”minfil”
EXPORTS
game=game
The VBA:
Private Declare Function game Lib "c:\users\musholm\documents\visual studio 2013\Projects\mitprojekt\Debug\mitprojekt.dll" (ByRef games As Integer, ByRef gameskifte As Integer) As Integer
Function game1(games As Integer, gamesskifte As Integer) As Integer
game1 = game(games, gamesskifte)
End Function
The Excel function is =game1(x,y)
And finally the original objective c I am trying to recreate in C:
- (NSNumber *)game:(NSNumber *)games gamechange:(NSNumber *)gameskifte
{
int gamesab = [games intValue];
int gameskifteab = [gameskifte intValue];
int gamesud = 0;
if (gamesab == 0){
if (gameskifteab == 1) gamesud=1;
else gamesud=2;}
else{
if (gamesab<3){
if (gameskifteab==1)gamesud=gamesab+2;
else gamesud=gamesab+4;}
else{
if (gameskifteab==1)gamesud=gamesab+3;
else gamesud=gamesab+5;
}
}
return [NSNumber numberWithInt:gamesud];
}
Aucun commentaire:
Enregistrer un commentaire