LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Error Code 1097 Coming in DLL Calling

Solved!
Go to solution

Hi FraggerFox,

 

     See the attached VI to hopefully duplicate the LabVIEW crash.  Again, I am using LabVIEW 2010.

 

1) Open any windows app (I used Calculator)

2) Open the VI and enter the Window Name of the opened windows app (I used "Calculator")

3) Run the VI.

 

When I run the VI with calling convention "WINAPI" it completes normally and without error.  However, if I change to "C" calling convention, the VI fails to complete and LabVIEW crashes.

 

Thanks,

BB

0 Kudos
Message 11 of 25
(6,858 Views)

I'm really sorry if you felt I was being harsh to you. Your post did not indicate that you just meant it to be a FYI post and you were not seeking for any advice. But you have also to realize that there are many people who post about 1097 errors, assuming that since LabVIEW is reporting that error it has to be responsible for the error too. A lot like the messenger has to be the cause of the problem! However a 1097 error simply means that the user has not configured the Call Library Node correctly, or the DLL is doing something it is not supposed to do. For Windows DLLs you can usually assume that the latter is not true. So this leaves in most cases only the option about a misconfigured Call Library Node.

 

Now you will probably say: But it has worked before! Yes it SEEMS to have worked before, but only because of bad luck. The error that is now causing a 1097 error has existed before, it only didn't get caught as diligently by LabVIEW as it does now. This is because the exception handling around a Call Library Node has been significantly improved in newer LabVIEW versions, at the cost of some performance. And a LabVIEW user is free to set the debugging level in the Call Library Node to a lower level and not see that 1097 error anymore, but that does not mean that the error does not exist, it just is less likely to be noticed, because now the only way to notice it is if it happens to destroy vital memory locations that will cause a crash, but if it caused something that can get caugth by the exception handling, it has destroyed some memory that it should not have done, even if that memory happens to be not vital for the normal operation. It could be for instance a data structure in a VI that gets saved at the next saving to disk, and consequently corrupt something in that VI, that may prevent loading the VI later on.

 

In 95% to 99% of the cases the 1097 error is caused by to small buffers passed to the Call Library Node for output strings and arrays. If the function wants to return a string with 20 characters in such a parameter the LabVIEW VI better allocates a string buffer with at least 21 bytes before it passes that to the function. LabVIEW can't know itself how big such an array or string buffer needs to be so the programmer has to explicitedly allocate it or in newer LabVIEW versions, at least set the minimum size for that parameter in the configuration for that CLN parameter.

 

And the original post from Frager Fox indicated that if you upgrade to LabVIEW 2009 and see a crash or error it could be that the Call Library Node is configured as C calling convention but really needs to be configured as Winapi calling convention, because before LabVIEW 2009, it contained some heuristic that silently corrected a C calling convention to Winapi, if the function matched a certain pattern. But the opposite is never true. There is no way short of disassembling the function code, to determine if a function requires C calling convention and consequently LabVIEW never has tried that.

 

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 12 of 25
(6,856 Views)

I don't believe FraggerFox's intent was to suggest that the 1097 error can always be fixed by changing the calling convention, just that it may sometimes solve the problem - especially in cases such as Rolf mentioned where older versions LabVIEW would attempt (not always successfully) to "fix" it for you automatically, and newer versions no longer do so.

 

It's not really worth running your VI.  If it works with the WINAPI calling convention then it's likely to crash with the C calling convention.  If the two were compatible, you wouldn't have to make the choice.  A quick internet search will turn up an explanation of the differences, if you want to get into the details.

 

For whatever it's worth, I think Rolf's comments were appropriate.  It's always difficult to know how the tone of a message will be interpreted in forums such as this, and probably best to give the benefit of the doubt to the author rather than assuming they meant to offend.

0 Kudos
Message 13 of 25
(6,851 Views)

Are you happening to run this VI in a 64 Bit version of LabVIEW? Because this VI was explicitedly written LONG ago (I would guess maybe 15 years ago) when 32 Bit was all that LabVIEW ever was running on (and Windows 3.1 which was itself 16 Bit was still very dominant).

 

For this VI to work under 64 Bit LabVIEW, you need to configure the first paramter as pointer sized Integer, and also configure the return value as such. And now you run into a BIG problem. The dummy refnum used to mimic a Windows Handle is in all versions of LabVIEW only a 32 bit value, so typecasting your pointer sized value to this refnum will truncate the upper 32 bit of the window handle when run under 64 Bit LabVIEW. Basically the entire Window API library has not only to be reviewed to use proper pointer sized parameter wherevever necessary but it needs also a replacement of the windows refnum by a 64 bit integer value.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 14 of 25
(6,849 Views)

@rolfk wrote:

And the original post from Frager Fox indicated that if you upgrade to LabVIEW 2009 and see a crash or error it could be that the Call Library Node is configured as C calling convention but really needs to be configured as Winapi calling convention, because before LabVIEW 2009, it contained some heuristic that silently corrected a C calling convention to Winapi, if the function matched a certain pattern. But the opposite is never true. There is no way short of disassembling the function code, to determine if a function requires C calling convention and consequently LabVIEW never has tried that.

 


Rereading FraggerFoxs original post carefully, I see that he has indeed suggested the opposite, to change WinAPI calling convention to C calling convention, when seeing errors in the Call Library Node that have not appeared before 2009. This is of course not correct. LabVIEW NEVER has silently corrected the Windows API calling convention back to C calling convention, it only did the opposite before LabVIEW 2009 and only in some very specific cases.

 

And when calling Windows API DLLs, you can assume in 99% of the cases that they use the Windows API calling convention. There are some exceptions but they are rare and far in between.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 15 of 25
(6,848 Views)

Natahnd, thanks for the reply.  Yes, I understand that FraggerFox was not guaranteeing changing to "C" from "WINAPI" would solve the 1097 problem.  My point was not only (and not even primarily, however poorly I phrased it) that it did not fix the 1097 problem but that it introduced a LabVIEW crash wherever I tried it.  FraggerFox's post stated that the calling convention must be defined as"C" and not as "WINAPI" for LabVIEW > 9.0.  My experience is that one should do just the opposite, and I thought others might benefit if we could somehow reconcile our contradictory experiences.

 

Thanks again,

BB

0 Kudos
Message 16 of 25
(6,839 Views)

From my experiences, whenever I build a dll out of a C code, i MUST keep the calling convention to C rather than WinAPI. The original post was about a dll made out of C code. I have faced several situations where migrating to LV2009 and above from old versions have made LV to crash while using wrong calling conventions for my DLLs made out of C code. For me, a dll made out of C must be set to C calling convention. Please correct me if I am wrong.

 

@Billy

From the VI you have posted, it is getting the window handle using WINDOWS based dll, hence we must keep it to WINAPI calling convention. Changing it to C convention does not make any sense in this case. It should only work with WINAPI convention. It is not needed to open your VI to check this, because, it is wrong to call windows based api with c calling convention.

 


@rolfk wrote:

Rereading FraggerFoxs original post carefully, I see that he has indeed suggested the opposite, to change WinAPI calling convention to C calling convention, when seeing errors in the Call Library Node that have not appeared before 2009. This is of course not correct.

 


 

rolfk,

The original post was made about C code, and what Billy is doing is calling a windows based api with wrong calling convention. Both are different cases. For the original post which was talking about C structure, my suggestion does not seem to be incorrect, though it does not relate directly with 1094 error, which, as mentioned in the error description, is something related more outside of LabVIEW.

-FraggerFox!
Certified LabVIEW Architect, Certified TestStand Developer
"What you think today is what you live tomorrow"
0 Kudos
Message 17 of 25
(6,831 Views)

@Fragger Fox wrote:

From my experiences, whenever I build a dll out of a C code, i MUST keep the calling convention to C rather than WinAPI. The original post was about a dll made out of C code. I have faced several situations where migrating to LV2009 and above from old versions have made LV to crash while using wrong calling conventions for my DLLs made out of C code. For me, a dll made out of C must be set to C calling convention. Please correct me if I am wrong.

 

 

rolfk,

The original post was made about C code, and what Billy is doing is calling a windows based api with wrong calling convention. Both are different cases. For the original post which was talking about C structure, my suggestion does not seem to be incorrect, though it does not relate directly with 1094 error, which, as mentioned in the error description, is something related more outside of LabVIEW.


Fragger Fox

 

There are actually two issues here:

 

What calling convention to use for a function depends on how the code was compiled. You can specify specific compiler statements in the source code that tell the compiler what calling convention the function should have. If that statement is missing, it will use the calling convention set in the command line parameters to the compiler, and if that command line parameter is missing it will use the compiler default, which for Visual C is actually cdecl. Other compilers likely use the same default too, unless they are embedded compilers, which may use much more exotic calling conventions for performance reasons.

 

The other issue is that LabVIEW has had some heuristics before 2009 that were analyzing DLL function names and silently correcting a cdecl calling convention to stdcall calling convention if it found a certain pattern in the function name. This silent correction was removed, without adding some according mutation code when loading old VIs into LabVIEW 2009 or later. LabVIEW NEVER corrected a stdcall into cdecl convention since there is no way to detect if a function needs cdecl, other than disassembling the function. Also the detection for stdcall was based on a naming convention for stdcall functions, that is simply a convention introduced by Microsoft. There is no rule that forbids a programmer to name his cdecl compiled function in a similar way, so they removed that silent correction in LabVIEW 2009, as LabVIEW would be otherwise unable to call a cdecl function that happens to match this naming convention.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 18 of 25
(6,825 Views)

rolfk,

Developing mostly in LabVIEW, I just followed a plain compiling process in C (I am not that well versed with C), without looking much into what could be changed by using different compiler statements. Thanks for the info, I would need to do more study on this part. 🙂

 

 

-FraggerFox!
Certified LabVIEW Architect, Certified TestStand Developer
"What you think today is what you live tomorrow"
0 Kudos
Message 19 of 25
(6,820 Views)

Hi rolfk,

 

     Indeed you are correct about my VI previously (in LabVIEW 8.6) NOT catching the 1097 error.  In LabVIEW 8.6, I bumped the error handling up from default to maximum for the suspect CLF node, and sure enough the 1097 error showed itself.  The problem turned out to be that the offending function requires a pointer to a callback function as one of its parameters.  Previously (via the clf node), I had been passing in a NULL for this parameter.  This "worked" in LabVIEW 8.6 with the error handling set to default but would not similarly "work" in LabVIEW 2010.  So, to resolve the issue for LabVIEW 2010 (since the DLL I had been calling in the suspect clf node is third party freeware and I do not have the source for it), I ended up creating a wrapper DLL in Visual C++ containing a single wrapper function and a simple callback function.  I let the wrapper function accept from LabVIEW (via the clf node) all parameters passed down to the suspect function in the third party DLL EXCEPT for the callback function pointer.  For that parameter, I let the wrapper function pass that in independent of LabVIEW.  Works great now.  I have contacted the author of the 3rd party DLL to see if they can update their DLL to accept and properly handle a NULL for the callback function pointer.  If they could do that and since I don't really care about having a callback function, I would then be able to go back to calling their DLL directly from the clf node and could eliminate my wrapper DLL.  I have not heard back from them yet, however, and don't expect to get anything any time soon if at all.

 

Thanks,

BB

0 Kudos
Message 20 of 25
(6,788 Views)