LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

calling convention mismatch

 

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

0 Kudos
Message 1 of 10
(6,158 Views)

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

0 Kudos
Message 2 of 10
(6,125 Views)

Haley, 

 

Thanks for the response. The library was definately written in c, all functions declared in c. 

 

Morgan

0 Kudos
Message 3 of 10
(6,120 Views)

Did you include the import library (file extension .lib)? 

 

http://digital.ni.com/public.nsf/allkb/EC2ECA974F8763D586256929006D17D7

0 Kudos
Message 4 of 10
(6,097 Views)

 

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

 

 

0 Kudos
Message 5 of 10
(6,059 Views)

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/

Rob S
Applications Engineer
National Instruments
0 Kudos
Message 6 of 10
(6,040 Views)

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

0 Kudos
Message 7 of 10
(6,027 Views)

 

Thanks, I have the default calling convention set to __stdcall.

 

-Morgan

0 Kudos
Message 8 of 10
(6,000 Views)

 

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

0 Kudos
Message 9 of 10
(5,999 Views)

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.

0 Kudos
Message 10 of 10
(5,944 Views)