12-08-2010 05:07 PM - edited 12-08-2010 05:11 PM
Another very common error when calling DLLs is to not pass large enough output buffers to DLL function parameters. With that I mean array or string parameters where the function writes information into.
While in LabVIEW you don't have to worry about allocating buffers as LabVIEW will simply create them whenever needed, this is not true when calling DLL functions. When that function wants to write 1000 characters in a string buffer, you better make sure to pass in a buffer with at least 1001 bytes allocated. LabVIEW can not automagically know what buffer the function requires and the function can not allocate it when needed since it can not pass a LabVIEW memory manager compatible buffer back to LabVIEW, unless the DLL function was specifically written to use LabVIEW memory manager functions for this, but this makes the DLL useless for non LabVIEW applications.
What happens if you don't pass a large enough output buffer to the DLL function is that the function writes it's information into the supposedly valid buffer anyhow. This can either lead to a direct access violation if the passed pointer is not valid at all, or it can silently destroy information in memory if the pointer is pointing to valid memory but not being large enough. This will sooner or later lead to crashes but in some cases I have seen such crashes only at the time when trying to close LabVIEW since it started to clean up various memory areas on exit which had been trashed by earlier DLL calls with invalid parameter output buffers.
The easiest way to allocate output buffers to pass to DLL functions is using Initialize Array, possibly followed with a Byte Array to String conversion for string parameters. This will work in all LabVIEW versions. In recent LabVIEW versions since 8.5 I believe you can also specify in the Call Library Node configuration a minimum size for array and string parameters. LabVIEW will then make sure that the parameter is always resized to this size if it is smaller, before calling the function and passing this parameter to it.
12-09-2010 01:20 AM
Hello Rolf,
you post is very true. I expirienced that problem also a few times.