将整数与从 VBA 调用的 dll 中的硬编码值进行比较时出现问题

Problems comparing integers to hardcoded values in a dll called from VBA

我要将我用 Applescript 和 Objective C 编写的项目转移到 Excel/VBA/dll。我已经开始使用 Objective C 函数,我想将它们放在 dll 中并通过 VBA.

调用

首先,我是一名统计学家,通过同等的运气、反复试验和毅力,我设法构建了最初的 Mac 程序(一种对我来说非常方便的统计工具)。请考虑到这一点(如果答案非常明显的话)。我使用这个(减去 64 位部分)作为 dll 的起点:https://sites.google.com/site/jrlhost/links/excelcdll

Objective C 是 C 语言,带有一层薄薄的特殊 Obejctive C 代码,可以让它与 Applescript 和项目的其余部分进行对话,所以理论上应该很容易从它制作用 C 语言编写的 dll。

但我已经遇到了所有功能中最微小的问题。我确信它可以更有效地完成,但现在我需要知道如果我能够将更大的功能从 Objective C 转移到 C.

为什么它不起作用

这是我正在尝试的 C 代码:

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;
}

这是代码应该做的事情:

它所做的是忽略 gameshift 的值和总是 returns games + 4

我试着改变了一些东西...

此代码(所有比较都是“<”但结果应该相同)

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;
}

总是给我游戏+1

还有这个(所有比较都是“>”并且结果不应该与上面相同):

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;
}

总是给我游戏+5

因此,在进行 return 计算时,它知道 *games 的值,但是当将 *games 与另一个硬编码值进行比较时,它会在进行直接或“>”比较时抛出错误,而在进行任何操作时抛出正数"<"无论值是高是低都进行比较。

项目的其余部分是

一个 def 文件:defFile.def:

LIBRARY ”minfil”
EXPORTS
game=game

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

Excel函数为=game1(x,y)

最后是原始 objective c 我正在尝试在 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];
 }

VBA的Integer对应C的short.
您需要 VBA.

中的函数参数和 return 值 As Long

此外,您在 else return games + 5; 中还遗漏了一个 *。尽管您首先不需要 *,但您应该将它们全部删除并在 VBA 中用 ByVal.

重新声明函数参数