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 =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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

[Table="width:, class:grid"][tr][td]Row\Col[/td][td]
A​
[/td][td]
B​
[/td][td]
C​
[/td][td]
D​
[/td][/tr][tr][td]
1​
[/td][td]
games
[/td][td]
gameskifte
[/td][td]
game
[/td][td]
[/td][/tr]
[tr][td]
2​
[/td][td]
0​
[/td][td]
1​
[/td][td]
1​
[/td][td]C2: =game1(A2,B2)[/td][/tr]
[tr][td]
3​
[/td][td]
0​
[/td][td]
2​
[/td][td]
2​
[/td][td][/td][/tr]
[tr][td]
4​
[/td][td]
1​
[/td][td]
1​
[/td][td]
3​
[/td][td][/td][/tr]
[tr][td]
5​
[/td][td]
1​
[/td][td]
2​
[/td][td]
5​
[/td][td][/td][/tr]
[tr][td]
6​
[/td][td]
2​
[/td][td]
1​
[/td][td]
4​
[/td][td][/td][/tr]
[tr][td]
7​
[/td][td]
2​
[/td][td]
2​
[/td][td]
6​
[/td][td][/td][/tr]
[tr][td]
8​
[/td][td]
3​
[/td][td]
1​
[/td][td]
6​
[/td][td][/td][/tr]
[tr][td]
9​
[/td][td]
3​
[/td][td]
2​
[/td][td]
8​
[/td][td][/td][/tr]
[/table]
 
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

[Table="width:, class:grid"][tr][td]Row\Col[/td][td]
A​
[/td][td]
B​
[/td][td]
C​
[/td][td]
D​
[/td][/tr][tr][td]
1​
[/td][td]
games
[/td][td]
gameskifte
[/td][td]
game
[/td][td]
[/td][/tr]
[tr][td]
2​
[/td][td]
0​
[/td][td]
1​
[/td][td]
1​
[/td][td]C2: =game1(A2,B2)[/td][/tr]
[tr][td]
3​
[/td][td]
0​
[/td][td]
2​
[/td][td]
2​
[/td][td][/td][/tr]
[tr][td]
4​
[/td][td]
1​
[/td][td]
1​
[/td][td]
3​
[/td][td][/td][/tr]
[tr][td]
5​
[/td][td]
1​
[/td][td]
2​
[/td][td]
5​
[/td][td][/td][/tr]
[tr][td]
6​
[/td][td]
2​
[/td][td]
1​
[/td][td]
4​
[/td][td][/td][/tr]
[tr][td]
7​
[/td][td]
2​
[/td][td]
2​
[/td][td]
6​
[/td][td][/td][/tr]
[tr][td]
8​
[/td][td]
3​
[/td][td]
1​
[/td][td]
6​
[/td][td][/td][/tr]
[tr][td]
9​
[/td][td]
3​
[/td][td]
2​
[/td][td]
8​
[/td][td][/td][/tr]
[/table]

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,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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