Reputation: 1335
I am working with a USB security key that uses a simple API. 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:
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:
error C2197: 'int (__stdcall *)(void)' : too many actual parameters
This is happening on the retcode = dongle() call. 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.
What I need to know is how I can get this C code to compile in C++.
Upvotes: 1
Views: 1112
Reputation: 64078
You need to use a better type definition for 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, ...);
Upvotes: 2
Reputation: 31394
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. 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. So you need to use a typedef with the correct number and type of parameters for the function call.
So something like:
typedef WORD (CALLBACK* DONGLEPROC)(DWORD, LPWORD, LPDWORD , LPDWORD , LPWORD, LPWORD, LPWORD, LPWORD, LPBYTE);
Upvotes: 1