简体   繁体   English

VC ++ 6.0:实际参数太多了

[英]VC++ 6.0: too many actual parameters

I am working with a USB security key that uses a simple API. 我正在使用一个使用简单API的USB安全密钥。 All I should have to do is include their header file and make my calls. 我所要做的就是包括他们的头文件并拨打我的电话。 I have a sample C program that works fine, and pretty much does this: 我有一个样本C程序,工作正常,几乎这样做:

HINSTANCE hDll;
FARPROC dongle;
WORD retcode, handle[16], SD_p1, SD_p2, SD_p3, SD_p4;
DWORD lp1, lp2;
BYTE buffer [1024];
SD_p1 = 0x1C76; // example password 1
SD_p2 = 0x8078; // example password 2
SD_p3 = 0;
SD_p4 = 0;

hDll = GetModuleHandle("dongle.dll");

if (hDll == NULL)
{
    hDll = LoadLibrary("dongle.dll");
    if (hDll == NULL)
    {
        printf("Can't find dongle.dll\n");
        return;
    }
}

dongle = GetProcAddress(hDll, "dongle");

retcode = dongle(SD_FIND, &handle[0], &lp1, &lp2, &SD_p1, &SD_p2, &SD_p3, &SD_p4, buffer);

So everything works fine with this. 所以一切都运行正常。 The dongle is found, and later calls to different functions on the dongle work as well. 找到加密狗,后来调用加密狗的不同功能。 However, when I plug this exact same code into the C++ application that I want to protect, I get the following error: 但是,当我将这个完全相同的代码插入我想要保护的C ++应用程序时,我收到以下错误:

error C2197: 'int (__stdcall *)(void)' : too many actual parameters

This is happening on the retcode = dongle() call. 这发生在retcode = dongle()调用上。 I don't understand why the compiler would believe there are too many parameters in my application but not in the sample application. 我不明白为什么编译器会认为我的应用程序中有太多参数但在示例应用程序中没有。 I did find an article pertaining to the difference between using GetProcAddress() this way in C vs. C++, but I'm not sure if this is the problem I'm seeing here, or how I would apply that solution in this particular scenario. 我找到了一篇关于在C与C ++中以这种方式使用GetProcAddress()的区别的文章 ,但我不确定这是否是我在这里看到的问题,或者我将如何在这个特定场景中应用该解决方案。

What I need to know is how I can get this C code to compile in C++. 我需要知道的是如何让这个C代码在C ++中编译。

You need to use a better type definition for dongle : 您需要为dongle使用更好的类型定义:

/* I didn't know what type SD_FIND was, assumed DWORD */
typedef WORD (CALLBACK *DONGLEPTR)(DWORD,WORD*,DWORD*,DWORD*
                                  ,WORD*,WORD*,WORD*,WORD*,BYTE*);

DONGLEPTR dongle; /* <-- need to change this type as well */

/* ... */

dongle = (DONGLEPTR)GetProcAddress(hDll, "dongle");

/* ... */
retcode = dongle(SD_FIND, ...);

That article is exactly the problem that you are having. 那篇文章正是你所遇到的问题。 In C a function definition with no parameters in it's list is a function that can take any number of parameters. 在C中,列表中没有参数的函数定义是一个可以接受任意数量参数的函数。 That effectively means the compiler does not check the number and type of parameters that you pass to the function. 这实际上意味着编译器不会检查传递给函数的参数的数量和类型。 In C++ the compiler always checks the number and type of parameters passed to the function. 在C ++中,编译器始终检查传递给函数的参数的数量和类型。 So you need to use a typedef with the correct number and type of parameters for the function call. 因此,您需要为函数调用使用具有正确数量和类型参数的typedef。

So something like: 所以类似于:

typedef WORD (CALLBACK* DONGLEPROC)(DWORD, LPWORD, LPDWORD , LPDWORD , LPWORD, LPWORD, LPWORD, LPWORD, LPBYTE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM