4.2.2 - OpenLabJack() [U3 Datasheet] | LabJack
« Close

Datasheets and User Guides

App Notes

Software & Driver


4.2.2 - OpenLabJack() [U3 Datasheet]

Call OpenLabJack() before communicating with a device. This function can be called multiple times, however, once a LabJack is open, it remains open until your application ends (or the DLL is unloaded). If OpenLabJack is called repeatedly with the same parameters, thus requesting the same type of connection to the same LabJack, the driver will simply return the same LJ_HANDLE every time. Internally, nothing else happens. This includes when the device is reset, or disconnected. Once the device is reconnected, the driver will maintain the same handle. If an open call is made for USB, and then Ethernet, a different handle will be returned for each connection type and both connections will be open.

OpenLabJackS() is a special version of open where DeviceType and ConnectionType are strings rather than longs. This is useful for passing string constants in languages that cannot include the header file. The strings should contain the constant name as indicated in the header file (such as “LJ_dtUE9” and ”LJ_ctUSB”). The declaration for the S version of open is the same as below except for (const char *pDeviceType, const char *pConnectionType, …).


LJ_ERROR _stdcall OpenLabJack (  long DeviceType,
                                 long ConnectionType,
                                 const char *pAddress,
                                 long FirstFound,
                                 LJ_HANDLE *pHandle)

Parameter Description:
Returns: LabJack errorcodes or 0 for no error.

  • DeviceType – The type of LabJack to open. LJ_dtUE9 (9), LJ_dtU3 (3), or LJ_dtU6 (6). For other LabJack devices, see What driver/library should I use with my LabJack?
  • ConnectionType – Enter the constant for the type of connection. LJ_ctUSB (1) or LJ_ctETHERNET (2) (UE9 only).
  • pAddress – For USB, pass the local ID or serial number of the desired LabJack. For Ethernet pass the IP address of the desired LabJack. If FirstFound is true, Address is ignored.
  • FirstFound – If true, then the Address and ConnectionType parameters are ignored and the driver opens the first LabJack found with the specified DeviceType. Generally only recommended when a single LabJack is connected. Currently only supported with USB. If a USB device is not found, it will try Ethernet but with the given Address.


  • pHandle – A pointer to a handle for a LabJack.


If you are adding more than 1 Labjack, can you do that. If you can how would the function call for OpenLabJack would look.

You make 1 call for each device to get a handle for each device.  See Section 4.3.1.

I believe that there is a typo in the documentation: "pHandle – A pointer to a handle for a LabJack" should be listed in the Outputs section instead of the Inputs section because this handle is the result of the OpenLabJack call. I guess pHandle got listed in the Inputs section because memory should be pre-allocated for this pointer before the OpenLabJack call?

It was actually in the textile source, but was not rendering properly on the web page.  Fixed now.

Is there a CloseLabJack() method?  I can't see it in the list.  I have accidentally called OpenLabJack in a loop and nothing bad happened.

There is a function that is defined as:


Which will close all open devices/handles and reset the driver to a fresh state.  It's undocumented because it shouldn't be needed.  As you've shown, the open call can be called repeatedly and will return the LJHandle to the already open device.  Once a device is opened that handle should always be valid (as long as the device is connected).


We find that we can't use the labjack software without closing all of LabView. Will the use of the close() function allow this?
Also as a point of clarification, what variables does close() use?

Because of how Windows controls handles for USB devices using the WinUSB driver, only one program can have one of those USB devices open for communicating at a time.  That is why LabView must be closed before another application can speak to the device.  The same would be true for any two programs (or even two copies of the same program).

The close function should allow you to get around that, but it shouldn't be necessary.

Generally, if you need more than one application to communicate with the LabJack, we recommend that you create a 3rd program that handles all the communication (and is always open) and then have the other two programs communicate with that one to pass data back and forth.

You don't need to pass anything to the close() function.


I saw a close() function in the library but I don't now the function inputs or how to make it work, can you help me?

The close() function is called without any inputs or return values, simply:


It will destroy/release all the handles to all devices when called.  Basically, it will put the driver back into the state of when the program was first loaded.  Thus, you will have to reopen a device before further communicating with it.

I have a 32 bit program which streams data from a LabJack U6. It works fine under 32 bit Windows XP.

When I run it under 64 bit Windows 7 it gives an access violation on the call to close() after I have stopped streaming. The call to close() at the beginning of the program works perfectly.

It also gives access violations under Windows 8. The violations are not always the same.

The call to Close() shouldn't be necessary in normal operation, it is more something for specific cases and debugging.  Either way though it shouldn't be giving an access violation.  Would it be possible to send us a copy of the executable is causing the issue?  It can be sent to [email protected]

I have sent you the exe file.

When I run it in Windows XP SP3 compatibility mode it works OK.


I am not seeing an email with an exe (it's not in the spam box either).  Sometimes ISPs will strip out emails with attachments that are .exes.  You might try renaming the extension of it to something else (i.e. .zzz) and trying again.