Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Document Type: Tutorial
NI Supported: Yes
Publish Date: Oct 18, 2006


Feedback


Yes No

Related Categories

Related Links - Developer Zone

Related Links - Products and Services

Setting Occurrences from C in Windows

3 ratings | 1.33 out of 5
Print

If you call a Code Interface Node (CIN) or shared library (DLL) from LabVIEW, you might want to set a LabVIEW occurrence from inside the CIN or DLL. You also might want to set an occurrence from a completely independent program. Setting occurrences from an external routine requires that you be familiar with calling code from shared libraries. In LabVIEW 7.1 or earlier, refer to the Using External Code in LabVIEW manual (linked below) for more information about using CINs and external subroutines to import code written in text-based programming languages. In LabVIEW 8.0 or later, refer to the Fundamentals»Calling Code Written in Text-Based Programming Languages book in the LabVIEW Help (linked below) for more information. The following two sections describe how to set the occurrence from external code.

Method 1: Post a Windows Message
Use the Generate Occurrence function to generate an occurrence refnum in LabVIEW. You must pass this occurrence refnum to the function that will set the occurrence. If this function is inside a DLL or a CIN, you can pass the reference using an argument. If another application requires the occurrence refnum, you must use another method, such as creating an exported function in the other executable that LabVIEW can call from a Call Library Function Node.

To set the occurrence, post a message to the LabVIEW window telling it to set the occurrence. To find which message to pass to LabVIEW, call the function GetLVAppWindHandle, which returns a message identifier that you can use to set occurrences. The function also returns the handle of the LabVIEW window:

DWORD __cdecl GetLVAppWindHandle( HWND* handle);
For example:

HWND LVhWnd;
DWORD WM_SET_OCCURRENCE;

WM_SET_OCCURRENCE = GetLVAppWindHandle( &LVhWnd );
If you are calling this function from a CIN, you must add the function prototype to the header for the CIN and call the function. labview.lib, which you must include with your CIN, handles the dynamic linking to labview.exe. If you are compiling a shared library, you can link versus labview.lib and include extcode.h. In this case, you also can call the function directly.

However, if you want to avoid linking to labview.lib, you can load the library dynamically and obtain a pointer to the Occur function, which differs depending on operating system. On Windows, use the GetModuleAddress and GetProcAddress functions to obtain the address of a exported function (GetLVAppWindHandle). Refer to Win32 SDK (linked below) for more information. Refer to KnowledgeBase 1Q3FAFNV (linked below) for more information about calling LabVIEW functions from external code. You have the occurrence refnum from another part of the code. Using this, you can post the WM_SET_OCCURRENCE message to LabVIEW setting the lParam value of the message to the occurrence you want to set:
LVRefNum occurrence;

PostMessage(LVhWnd, WM_SET_OCCURRENCE, NULL, (LONG)occurrence);

MgErr and LVRefNum are defined in extcode.h in labview\cintools.

Method 2: Call the Occur Function
In addition to posting a message, you can set a more immediate occurrence by calling the Occur function in labview.exe. This eliminates latencies involved with message processing. However, you should call the occur function only from threads created by LabVIEW. If another thread needs to set an occurrence, use the first method.

With the occurrence refnum received earlier, call the Occur function exported by LabVIEW. It has the following prototype:
MgErr __cdecl Occur( LVRefNum occurrence_to_set );

Call it when the occurrence should be set, passing to it the occurrence refnum.

Related Links
Win32 SDK
Product Manuals: Using External Code in LabVIEW
KnowledgeBase 1Q3FAFNV: How Do I Create a DLL That Can Call LabVIEW Manager Functions?

Calling Code Written in Text-Based Programming Languages



 

3 ratings | 1.33 out of 5
Print

Reader Comments | Submit a comment »

 

Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).