Source code © 1994-2009 Guillaume Dargaud.
Free use, distribution and modification.
Can be integrated in commercial programs, but the source/fp/lib/dll provided here must not be sold independently.
Last updated on 2009/03/04
Q: How many software engineers does it take to change a lightbulb ?
A: It can't be done; it's a hardware problem.
LabWindows/CVI is a great package from National Instruments. Its main purpose is to be a powerful tool for writing data acquisition programs, but it's really a multipurpose 32 bits C compiler, doubled by a code generator and some of the most powerful C libraries ever seen on a PC (easy to use and very complete User Interface, signal/maths and more with Advanced Analysis, Data Acquisition, VXI, GPIB 488, VISA, TCP, DDE, formatting and I/O, Utilities and finally ANSI C).
If you have specific requirements, I do freelance consultant and development work on LabWindows/CVI with my own company, Briansoft.
Note: I have recompiled all the .lib and .fp with CVI version 5.5 and the .fp are not backward compatible with previous versions of CVI. Sorry. You can still try to use them by linking directly to the .lib.
First things first, 2 headers that everybody should have, the iso646.h for meaningful connectors and some version of my def.h useful definitions file.
There are a few things missing from early (or even current) versions of LabWindows. Here is some code I wrote or adapted for getting the available disk size, doing on the fly statistics, doing color scaling, getting compiled Html Help files (*.chm) to display contextual help and doing extended time formatting. You can right click on a frame and select [Open frame in new window] to get a full size listing; or copy/paste the frame into your favorite editor...
This is the source code for a small 16 bits DLL for compilation with Microsoft C v1.52. I don't know if this will compile for Win95.
3 source files: DiskFree.c, DiskFree.h and DiskFree.def in DiskFree.zip.
Things are different here, playing around with interrupts is no longer a Good Thing, and it is better to use a call to a Windows SDK function (source sent by National Instrument technical support). Note the #include to windows.h:
One source file: AvailableDiskSize.c in DiskFree.zip.
Damn, this is turning into a real tutorial. If your HD is bigger than 2Gb, GetDiskFreeSpace will return only 2Gb. You have to use GetDiskFreeSpaceEx, but there's a catch... it uses 64 bits integer arithmetics, and CVI won't support them. The hack is to use a struct with 2 long and some conversions to double. Note that this function returns a volume free size in Mb, not bytes.
Starting with version 5.5, there is a GetDiskSpace function in CVI. But it still requires 64 bits integers which CVI does not support. Bouhhh...
There's now full support of 64 bits integer with __int64. It works directly for instance with the fsetpos and fgetpos file functions for files above 2Gb. Example: __int64 Position; [...] fsetpos(File, (fpos_t*)&Position));
The new HtmlHelp system is a great way to write you online documentation if you already know HTML. But then, how do you call it from your application ? I hacked this short module for LabWindows in order to display contextual help, the ChmHelp() function is similar to the SystemHelp() function and if defined in HtmlHelp.h. Note, you need to include HtmlHelp.lib and Advapi32.lib to you project. You need the Microsoft HtmlHelp workshop in order to get those libraries (and to compile chm files).
Two source files: ChmHelp.h and ChmHelp.c.
Starting with version 5.5, there is a ShowHtmlHelp function in CVI. You don't need to link anymore with HtmlHelp.h and HtmlHelp.lib. But you still need to prepend the mk:@MSITStore... stuff.
Have you ever noticed that in the user interface editor you can move between panels using Ctrl-shift and the left or right arrow ? Well, here's a little piece of code that allows you to do the same thing in your C programs. In addition it will print or center your panels on request.
OK, so LabWindows/CVI can convert a panel or control to a bitmap and then save it as as .bmp file. Hmmm, not very useful: you can't put it on the Web and it takes a huge amount of space on the disk. The JPG format is not very adapted to saving a typical panel (the text gets hard to read because of the lossy compression), and the GIF format is limited to 256 colors and is a proprietary format. So what gives ? The only other compressed format that is common on the Web is the Portable Network Graphics (PNG) format that can be viewed in IE4 or NS4 or above.
So I got the LIBPNG library and the ZLIB library from their respective web sites and managed to compile them for LabWindows/CVI. I wrote this Function Panel to save a bitmap, a panel or a control to a PNG file, and inversely that can read a PNG file into a bitmap or a picture control. LIBPNG and ZLIB are public domain, so this FP file is too.
PNG files have a lot of characteristics we don't need here: they can have RGB 3*8 or 3*16 bits (possibly with 8 or 16 bits alpha channel), greyscale 1, 2, 4, 8 or 16 bits (possibly with Alpha channel). Palettes of 1, 2, 4, 8 or 16 colors with multiple transparencies, comment text... Basically here we save in RGB 3*8, although I have put options for Alpha channel, greyscale conversion and black&white conversion.
Saving: If your graphic card is in 16/24-bit mode, it saves 24-bits RGB colors PNG files without alpha layer; if your card is in 32-bit mode, it saves a 24-bits RGB image with the option of having the current selection in the alpha layer. It won't work with monitors in paletted mode, I didn't want to bother with palettes, although if you pay me I could implement it in less time than it takes for a French trucker to go on strike. This 3*8 RGB should be OK for most applications, but you can save room by saving as greyscale (3 times smaller uncompressed) or black&white (24 times smaller uncompressed). PNG control options such as interlacing, gamma (default is set to 1, but you might get more accurate colors with gamma set to 0.5), resolution, compression level and comment texts are implemented.
Reading: It can read any PNG file I've cared to test: paletted 1/2/4/8/16 bits, 3*8/3*16 RGB, 8/16 greyscale, alpha layers or no, but it converts everything to 3*8 bits RGB. Image comment texts are read. No gamma conversion.
I don't give the source code here because it relies on LIBPNG and ZLIB and needs a lot of source files. The FP file contains extended information on use.
Note: Do you want to generate web pages to present those nice PNG files ? Create a cgi-script exe file with LabWindows ? Then you can easily compile and use the public domain cgic106 library in version 5.5 of LabWindows/CVI (you need the possibility to do console applications). And use my PanelToCgi code generator to convert one of your existing CVI application to the web !
Some sample files: TestReadSavePng.c and a function panel (ReadSavePng.fp/lib) in ReadSavePng.zip (111Kb). Example of use:
// Saving
Png_SetParameters (1, 1, 72, Z_BEST_SPEED, FALSE);
Png_SetTextAuthor("Guillaume Dargaud");
Png_SavePanelControlToFile (PanelHandle, 0, "PanelSpeedInterlaced.png");
// Reading
Png_ReadControlFromFile(PanelHandle, PictureControl, 0, "PanelSpeedInterlaced.png");
printf(Png_GetTextAuthor());
OK, so just above here you have downloaded my fp so that you can read and save images of panels/controls/bitmaps in LabWindows/CVI. PNG files are great but for pictures you might want to use the superior compression of the JPEG format. You have to be aware of the limitations: JPEG works well with RGB or greyscale pictures but not well at all with the typical panel containing text and graphics. JPEG is also the standard image file on the Web.
So I got the JPEG-6B library from the Independent JPEG Group and managed to compile it for LabWindows/CVI. I wrote this Function Panel to save a bitmap, a panel or a control to a JPEG file, and inversely that can read a JPEG file into a bitmap or a picture control. IJG's JPEG-6b is public domain, so this FP file is too.
JPEG files are either 24 bit RGB or 8 bit greyscale. This FP accepts 32 bit arrays but will convert them. On reading, greyscales are converted to RGB. JPG does not support palettes, alpha channels or transparencies.
Limitations: It won't work with monitors in paletted mode, I didn't want to bother with palettes, although if you pay me I could implement it in less time than it takes for a French trucker to go on strike. Also I haven't implemented interlaced on saving (it will read them, though).
I don't give the source code here because it relies on JPEG-6b and needs a lot of source files. The FP file contains extended information on use.
Note: Do you want to generate web pages to present those nice JPEG files ? Create a cgi-script exe file with LabWindows ? Then you can easily compile and use the public domain cgic106 library in version 5.5 of LabWindows/CVI (you need the possibility to do console applications, so it won't work with earlier versions). And use my PanelToCgi code generator to convert one of your existing CVI application to the web !
A function panel (ReadSaveJpeg.fp/lib) in ReadSaveJpeg.zip (95Kb). Example of use:
// Saving Png_SavePanelControlToFile (PanelHandle, 0, "PanelSpeed.jpg", 75); // Reading Png_ReadControlFromFile(PanelHandle, PictureControl, 0, "PanelSpeed .jpg");
OK, so there are several predefined popups, like ConfirmPopup or GeneralPopup that you can call, and they will return the button clicked by the user. But how do I make a custom popup, for instance the classic [Yes|No|YesToAll|NoToAll|Cancel] popup ?
I mulled that one over for a while, until a suggestion from Martin J. Saxon: the trick is to call RunUserInterface again... Here's the sourcecode for the [Yes|No|YesToAll|NoToAll|Cancel] popup.
Here's another example which allows you to change the format, precision and padding of a numeric control:
There are many default events in LabWindows, but some are missing. For instance you can drag and drop files from explorer onto a panel only if you call the EnableDragAndDrop function. Something else that is sometimes necessary is shift-click, ctrl-click and alt-click. The code is below. The thing I haven't been able to figure out yet is how to separate and get the coordinates of mouse-down and mouse-up on a canvas, which is necessary for drawing a box for instance.
#include <windows.h>
...
int CVICALLBACK cb_View (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) {
BOOL KeyShift, KeyCtrl, KeyAlt;
// can be combined
KeyShift=GetAsyncKeyState(VK_SHIFT)>>8; // >>8 if the key is pressed right now,
KeyCtrl =GetAsyncKeyState(VK_CONTROL)>>8; // &0xFF if it's been pressed since the last call
KeyAlt =GetAsyncKeyState(VK_MENU)>>8;
switch (event) {
case EVENT_LEFT_CLICK:
if (KeyShift) ...;
if (KeyCtrl) ...;
if (KeyAlt) ...;
break;
...
Another trick that has to do with user input is to regroup the keyboard events to the top panel. For instance if you have a child or separate panel that catches a few keyboard event but you want all other keyboard events to go to the main one (useful for instance if the menu bar is only on the main panel), do the following in the child panel callback function:
switch (event) {
case EVENT_KEYPRESS:
switch (eventData1) { // Keyboard events recognized by child panel
case 'm': DoSomething(); return 1; // Act and swallow the event
...
}
// Key not recognized, passed to Main window
SetActivePanel(MainPanel);
FakeKeystroke (eventData1);
return 1; // We swallow the event but fake it again
}
As of 2007, LabWindows can run as real-time. The setup is fairly complex: you develop your code on a Windows machine, in the usual CVI IDE, and then you push it onto a machine running a real-time minimalist OS called PharLap ETS. Now I've had some trouble getting that thing running. The remote machine (also called real-time target) can be a PC, a PXI box or some other compbination, but for PCs the requirements are actually pretty tight:
Now NI gives you several ways to configure the target. I'll speak first about the OS installation, then about the network config:
Anyway, to make a long story short, I wasn't able to properly run the RTOS on my test laptop: incompatible network card. It's the proper chipset but it needs to be PCI and not MiniPCI. End of the story for now.
As of version 8.0, National Instruments provides a version of LabWindows/CVI for Linux. Here are some notes about using it.
Some tricks:
Other resources about LabWindows/CVI include:
LabWindows/CVI Programming for Beginners;Counters: Page:103257, Section:2619930, Site:20810908.
Forward to the next Hack page. Back to my contact page, my computer page or my home page.