C dll compare integers not behaving as expected

bligstar

New Member
Joined
Mar 27, 2015
Messages
6
I am about to transfer a project 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. 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.

Here is my original Objective C code:

Code:
- (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];
}

and here is what I am trying to use in Visual Studio (minfil.c):

Code:
int _stdcall spil(int *games, int *gameskifte)

{

	if (*games < 1){
		if (*gameskifte < 1) return *games + 1;
		else return *games + 2;}
	else{
		if (*games < 3){
			if (*gameskifte==1) return *games + 2;
[B]			else return *games + 4;}[/B]
		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 defalut gameshift is not 1), return games + 4
If games > 2 and gameskifte =1, return games + 3
If games > 2 and gameskifte =2, return games + 5

What it does is ignore the value of gameshift and ALWAYS returns games + 4 as highligted.

I have tried to change the code ever so slightly and it seems like every time the code test if the variables games or gameshift are equal to some number it throws a false and every time it asks if they are smaller than some numer it throws a true


I use a .def file (defFile.def):

Code:
LIBRARY "minfil"
EXPORTS
game = spil

In VBA I have placed the following:

Code:
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

Any idea whats going wrong here?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
That's a peculiar set of tests. Why not just

Code:
Function game1(games As Integer, gamesskifte As Integer) As Integer
  Select Case games
    Case 0
      game1 = IIf(gamesskifte = 1, 1, 2)
    Case 1, 2
      game1 = IIf(gamesskifte = 1, 2, 4) + games
    Case 3
      game1 = IIf(gamesskifte = 1, 3, 5) + games
    End Select
End Function

Row\Col
A​
B​
C​
D​
1​
games
gameskifte
game
2​
0​
1​
1​
C2: =game1(A2,B2)
3​
0​
2​
2​
4​
1​
1​
3​
5​
1​
2​
5​
6​
2​
1​
4​
7​
2​
2​
6​
8​
3​
1​
6​
9​
3​
2​
8​
 
Last edited:
Upvote 0
That's a peculiar set of tests. Why not just

Code:
Function game1(games As Integer, gamesskifte As Integer) As Integer
  Select Case games
    Case 0
      game1 = IIf(gamesskifte = 1, 1, 2)
    Case 1, 2
      game1 = IIf(gamesskifte = 1, 2, 4) + games
    Case 3
      game1 = IIf(gamesskifte = 1, 3, 5) + games
    End Select
End Function

Row\Col
A​
B​
C​
D​
1​
games
gameskifte
game
2​
0​
1​
1​
C2: =game1(A2,B2)
3​
0​
2​
2​
4​
1​
1​
3​
5​
1​
2​
5​
6​
2​
1​
4​
7​
2​
2​
6​
8​
3​
1​
6​
9​
3​
2​
8​

Thank you. In this articular case I can use your VBA version.

But finding a solution is not a goal in itseld. My project is much much larger with among other things several large monte carlso simulations. VBA is not an option in those cases and I need to understand why it doesn´t perform as expected since the code is littered with variables I need to compare.
 
Upvote 0
You're welcome.

If VBA is not your platform, you'd be better off in a C forum.
 
Upvote 0
I have done so now, thank you.

But the problem might be with the vba I have also posted. I would be grateful if someone would check if I am calling the dll correctly from the vba. Especially if it is the correct way of passing two variables to a dll.
 
Last edited:
Upvote 0
VBA is happier with Longs than Integers anyway.
 
Upvote 0

Forum statistics

Threads
1,215,206
Messages
6,123,638
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top