08-06-2014 04:37 PM
Hello,
I am using Labwindows CVI 10.0.1. I am including a header which has several function declared like this;
__declspec(dllexport) int __stdcall func1(int x);
__declspec(dllexport) int __stdcall func2(int x);
__declspec(dllexport) int __stdcall func3(int x);
I want to store an array of structs which contain a pointer to those functions. I declare the struct and array like this;
typedef struct {
int __stdcall (* apiFunc)(int);
int setpoint;
} storedFuncs_t;
storedFuncs_t funcs[] = { {func1, 1}, {func2, 2}, {func3, 3} };
Then I execute the functions like this;
for (int i=0; i <3; ++i)
{
funcs[i].apiFunc(funcs[i].setpoint);
}
I get the following runtime error when executing debug build;
NON-FATAL RUN-TIME ERROR: "tests.c", line 362, col 18, thread id 0x000022AC: CVI has detected an inconsistency of the run-time stack. This might have been caused by a calling convention mismatch in the last function call.
I've tried chaning the placement of the calling convention as suggested in other posts, like this
typedef struct {
int (*__stdcall apiFunc)(int);
int setpoint;
} storedFuncs_t;
Also, I look at the array in the variable window and see it displayed like this;
int (__stdcall *)(int)
Also, default calling convention in build options is set to __stdcall.
Any advice would be really helpful.
Thanks,
Morgan
08-07-2014 12:04 PM
Hello Morgan,
Were the declared functions created in C++? And if so, are the functions marked with extern "C" to specify they will be used with a C compiler? Sec 9 of this article discusses using DLLs created in C++ in CVI further
08-07-2014 02:00 PM
Haley,
Thanks for the response. The library was definately written in c, all functions declared in c.
Morgan
08-08-2014 04:12 PM
Did you include the import library (file extension .lib)?
http://digital.ni.com/public.nsf/allkb/EC2ECA974F8763D586256929006D17D7
08-11-2014 09:30 AM
I did, I think the application would fail to link if I didn't include it. The dll came with no import library, so I used Options->Create DLL Import Library.
Thanks,
Morgan
08-12-2014 03:07 PM
Might it be a problem with the calling convention in the build options? This help document details a little about using stdcall over cdecl.
http://zone.ni.com/reference/en-XX/help/370051T-01/cvi/usermanual/prjbuildopt/
08-13-2014 03:33 AM
Hello Morgan,
I was able to reproduce the run-time error with your code and a dll using __cdecl calling convention.
Did the header you are including come with the dll or is it created by yourself?
If you replace __stdcall with __cdecl and re-create the import library does it work?
Constantin
08-14-2014 03:39 PM
Thanks, I have the default calling convention set to __stdcall.
-Morgan
08-14-2014 03:58 PM
Constanin,
The header came with the DLL. I tried changing the calling convention to __cdecl in the header and re-creating the import library. It fails to create the import lib, stating that the calling convention is different than previously declared.
Thanks,
Morgan
08-18-2014 07:42 AM - edited 08-18-2014 07:44 AM
Hello,
Does the message states where the previous declaration is? You should modify in the other declarations,too?
How did you get the dll? Do you have the source code?
If you could send the dll and related files would be usefull to investigate.
Constantin.