Thread: Compare integers with hard coded values goes wrong in dll

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    4

    Compare integers with hard coded values goes wrong in dll

    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.

    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 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;
                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 defalut gameshift is not 1), return games + 4
    If games > 2 and gameskifte =1, return games + 3
    If games > 2 (and by defalut gameshift is not 1), 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?

  2. #2
    Registered User
    Join Date
    Mar 2015
    Posts
    4
    Woops. Forgot the C code in the first post. It was from one of the experiments. Here is the code as described:

    Code:
    int _stdcall spil(int *games, int *gameskifte)
    {
                if (*games == 0){
                             if (*gameskifte == 1) return *games + 1;
                             else return *games + 2;}
                else{
                             if (*games < 3){
                                         if (*gameskifte == 1) return *games + 2;
                                         else return *games + 4;}
                             else{
                                         if (*gameskifte == 1) return *games + 3;
                                         else return *games + 5;
                             }
                }
     
                                         return 0;
    }
    
    

    That gives me games + 4 as a result

    This code (where all compares are "<")

    Code:
    int _stdcall spil(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 code (where all compares are ">")

    Code:
    int _stdcall spil(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.

    Last edited by bligstar; 03-29-2015 at 01:18 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First off, try a simple test like this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int spil(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;
    }
    
    
    int main()
    {
      int games,gameskifte;
      for ( games = 0 ; games < 5 ; games++ ) {
        for ( gameskifte = 0 ; gameskifte < 5 ; gameskifte++ ) {
          int result = spil(&games,&gameskifte);
          printf("games=%d, gameskifte=%d, result=%d\n", games, gameskifte, result);
        }
      }
      return 0;
    }
    
    
    
    $ gcc bar.c
    $ ./a.out 
    games=0, gameskifte=0, result=1
    games=0, gameskifte=1, result=1
    games=0, gameskifte=2, result=2
    games=0, gameskifte=3, result=2
    games=0, gameskifte=4, result=2
    games=1, gameskifte=0, result=3
    games=1, gameskifte=1, result=3
    games=1, gameskifte=2, result=5
    games=1, gameskifte=3, result=5
    games=1, gameskifte=4, result=5
    games=2, gameskifte=0, result=4
    games=2, gameskifte=1, result=4
    games=2, gameskifte=2, result=6
    games=2, gameskifte=3, result=6
    games=2, gameskifte=4, result=6
    games=3, gameskifte=0, result=6
    games=3, gameskifte=1, result=6
    games=3, gameskifte=2, result=8
    games=3, gameskifte=3, result=8
    games=3, gameskifte=4, result=8
    games=4, gameskifte=0, result=7
    games=4, gameskifte=1, result=7
    games=4, gameskifte=2, result=9
    games=4, gameskifte=3, result=9
    games=4, gameskifte=4, result=9
    Next, put some debug print statements inside your C function (maybe even log to a file if you don't have a handy console), to verify what you're being passed makes sense.

    > 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
    But your function is called spil ?
    How does that work?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    4
    Thank you. I had hoped it was something very fundamental to the grammar of C I had misunderstood. Unfortunetly it doesn´t seems like it.

    First of all, I should have made it clear 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. I do not have much knowledge on how to use programming tools so you have to be very specific in that area.

    How do I perform the test you suggest? I have Visual Studio Express at my disposal.

    Quote Originally Posted by Salem View Post
    > 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
    But your function is called spil ?
    How does that work?
    I followed the basic instructions on how to make a dll from here: https://sites.google.com/site/jrlhost/links/excelcdll (except I discovered my Excel is NOT 64 bit so I had to remove those parts) and it included a defFile.def file that I used to translate the name. The private function i Excel is game1, in VBA it calls game and in the def file game becomes spil. Since that only complicate things I have now changed that so it is game instead of spil.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Put this at the start of your C function.
    Code:
      FILE *fp = fopen("gamesLog.txt","a");
      if ( fp ) {
        fprintf(fp,"games=%d, gameskifte=%d\n",*games,*gameskifte);
        fclose(fp);
      }
    This will produce a text file (in the current directory of the executable) showing you what values you got.



    > How do I perform the test you suggest? I have Visual Studio Express at my disposal.
    You should be able to create a new project using a project wizard. If you select a "console" project, the rest should happen automatically.
    Then all you do is copy/paste the source code into it.

    You could make it link with your DLL for a proper test, but that's a little more involved than simple copy/paste.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    4
    Turned out it was a type mismatch. VBA and C integers are not the same. VBA integer corresponds with C short.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-31-2014, 09:27 PM
  2. write() and read() int values from socket returning wrong values.
    By Premjith P S in forum Linux Programming
    Replies: 8
    Last Post: 11-29-2012, 02:59 PM
  3. Replies: 5
    Last Post: 06-20-2012, 01:48 AM
  4. Replies: 9
    Last Post: 03-31-2012, 10:51 PM
  5. Two Hard-coded Arrays into One 2-Dimensional Vector
    By codechick in forum C++ Programming
    Replies: 11
    Last Post: 03-24-2012, 02:41 PM

Tags for this Thread