Key Technology 4: Export DLL from CVI for external use

Source: Internet
Author: User

Ni's LabWindows/CVI development environment has been upgraded to version 2009, not only can it use Windows Borland C ++, Visual C ++ compiled DLL, in addition, these external compilers can be used directly to replace the CVI compiler to compile the original code resources! Compared with the original LabWindows/CVI 5.5, version 2009 significantly reduces the resistance of collaborative development in Multi-compiler environments and provides a lot of convenience, there are at least nine cooperative development methods that combine multiple compilation environments. Here, we consider converting the existing CVI project code to DLL and embedding it into the visual C ++ project. Clearly intended-used for the construction of a semi-physical simulation system. This technology can be implemented in two phases: 1. reconfigure the existing CVI project code in CVI to generate a dynamic link library available for Visual C ++; 2. Make necessary modifications in the new visual C ++ and call the dynamic link library of CVI. The detailed steps are as follows: 1. In the CV section, we mainly modify the code and configuration. In the code, find out the program entry file of the original CVI project, which is generally the c file where the main is located. Modify or create a new C file to include it, which is equivalent to creating a DLL shell. The shell must contain at least the following code:

# Include <cvirte. h>

Int _ stdcall dllmain (hinstance hinstdll, DWORD fdwreason, lpvoid lpvreserved)
{
Switch (fdwreason)
{
Case dll_process_attach:
If (initcvirte (hinstdll, 0, 0) = 0)
Return 0;/* out of memory */
Break;
Case dll_process_detach:
Closecvirte ();
Break;
}
 
Return 1;
}

Int _ stdcall dllentrypoint (hinstance hinstdll, DWORD fdwreason, lpvoid lpvreserved)
{
/* Supported ded for compatibility with Borland */

Return dllmain (hinstdll, fdwreason, lpvreserved );
}

This code is basically a requirement of the operating system and does not involve the CVI runtime system. To generate a DLL that is available for external use, there is also a slight modification to the Code: Modify the Loading Method of the Panel. The General Main Function loads the Panel as follows: If (panelhandle = loadpanel (0, "testcvi. UIR", panel)
) <0)
Return;
Displaypanel (panelhandle );
Runuserinterface ();
Discardpanel (panelhandle );
... To enable the Panel to use the callback function in DLL, you must use loadpanelex to load it: If (panelhandle = loadpanelex (0, "testcvi. UIR", panel, _ Cviuserhinst
)
) <0)
Return;
Displaypanel (panelhandle );
Runuserinterface ();
Discardpanel (panelhandle );
... _ Cviuserhinst
Is the default DLL loading handle.
Not only is the main function, but the loadpanel in the whole project code must be modified as above. You also need to make some modifications to the CVI compilation configuration. 1. Set build-> target type to dynamic link library2, option-> build option, and check the function call method to ensure that it is consistent with Visual C ++. Generally, winapi uses the _ stdcall method. Here, select _ stdcall. 3. Build-> Target settings: use the "include file" symbol specially compiled for DLL to set the export function: testcvi with the same name as the UIR panel is not used. H is used to avoid modifying testcvi. UIR automatically overwrites it. Using a separate. h to specify the DLL export function is more secure. The export information about DLL in testcvidll. h is used by the external compiler during compilation. Visual c ++ adopts the C ++ compiling mode by default, while CVI is always in the Standard C compiling mode. Therefore, this header file must be protected using the C macro:

/* If using C ++ compiler to compile the file, adopting C linkage mode
*/
# Ifdef _ cplusplus
Extern "C "{
# Endif

 

/*Your function declaration for DLL
*/

 

# Ifdef _ cplusplus
}
# Endif

4. embed the UIR panel. The earlier version of LabWindows/CVI does not support embedded panels. Therefore, when using the DLL generated by CVI, you also need to copy the UIR panel to the same runtime path (./debug. LabWindows/CVI of version 2009 can be directly configured as an embedded UIR file: by setting custom copy directory on this configuration page, even copying DLL files to the runtime directory of the Visual C ++ project is saved. 5. CTRL + m to generate the debug or release version of DLL and Lib. Ii. Visual C ++ mainly involves compilation settings and link settings. 1. Set the function call method to be consistent with CVI. 2. Add the DLL compilation header Lib. Note that there are multiple ways to add the Lib file: (1) directly add file; (2) Add a directory to the database search path, and then add the path name to the database file. (3) directly add the path name of the database. The addition of header files is similar. Here, the DLL header files are directly copied to the project root directory, so you do not need to do more. In addition, in the Visual C ++ compiler options,/GZ specifies the _ stdcall call method and/GD specifies the _ cdecl call method. The last example is to call the CVI panel program in the MFC program:

 

 

 

FAQs:

1. Link error 2001: the DLL symbol cannot be found. This is because visual c ++ defaults *. if the function calls in the CPP file (including the export function in dl) are named in C ++, use the extern "C" macro to protect them. The idea comes from here
Explanation of DLL calling principle and Analysis of DLL header file format
.

 

Related Resources: 1. Examples in this article
Take the official Ni example
And fixed several errors. Download them here
, And
.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.