LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to fix VBA DLL calling problem with SetCtrlVal() UI function ?

I am using Excel VBA declaration of external function in cvirte.dll library. I have done declaration and calling successfully of these functions(LoadPanel,DisplayPanel,DiscardPanel,GetUserEvent,PlotStripChart,PlotStripChartPoint,ClearStripChart) and they works OK. But with SetCtrlVal() and all functions which have some argument of any data type I have trouble.
To function SetCtrlVal() I have done following steps, as shown bellow.

in C language the definition of function I need to call is as follows:

int SetCtrlVal (int Panel_Handle, int Control_ID, ...);
last argument can by of any data type.

in Excel VBA module my code is as follows:

Public Declare Function SetValue Lib "cvirte.dll" (ByV
al Panel_Handle As Integer, ByVal Control_ID As Integer, Value As Any) As Integer

How you can see, argument "value" I have declared as Any data type.

Compiling of the VBA code is OK, but when I call function in this code:
------------------------------------------------------
Public Declare Function SetValue Lib "cvirte.dll" (ByVal Panel_Handle As Integer, ByVal Control_ID As Integer, Value As Any) As Integer

Dim speed as Double
Dim ph as Integer

ph=SetCtrlVal(1, 8, speed)
'panel has handle=1
'control has id =8

following error occurs:
Run-time error :'49' Bad DLL calling convention

I think my function declaration is OK and I dont know where is a problem now, what is wrong. I think that problem is at 3-rd argument,but not sure. Do you now how to fix it ? I am using LabWindows/CVI 4.0.1.

Any help or suggestions would be greatly appreciated.
0 Kudos
Message 1 of 3
(3,616 Views)
That function is declared with a CDECL calling convention which is NOT compatible with Visual Basic which uses a STDCALL calling convention. I don't know anyway to call CDECL function from VB or VBA. You can do it from VB.NET. There is a book on Advanced Visual Basic Programming that talks about this and may have some hints you can find here. To my knowledge though, you will not be able to call this function from VBA since it is declared with a CDECL calling convention.

Best Regards,

Chris Matthews
National Instruments
0 Kudos
Message 2 of 3
(3,616 Views)
After some experimentation under LabWindows/CVI I could say that elegant solution for this problem is as follows:
Write dll under LabWindows/CVI with redefinition of desired functions, but with __stdcall calling convention. After that step you should build DLL library. Calling redefined function under MS Excel in VBA environment works great !!!
0 Kudos
Message 3 of 3
(3,616 Views)