Saturday, July 27, 2013

WinCC OA 3.11 C++ Extensions (Part 3)

Options for starting the Visual Studio solution

This is part 3 of the tutorial. You can view other parts here:
Part 1.
Part 2.

You can go with 2 approaches when you start the created Visual Studio solution. Each has its advantages and disadvantages.
The first is based on running batch files in a manner similar to the one used to setup the solution in the first place. Using this approach you don't pollute your list of environment variables.
The second is based on creating user environment variables, which are the exact same variables that the batch files would set. The only difference being that you get to keep them all the time. I favor this approach, as it's more user friendly.


Starting the created Visual Studio solutions using batch files


This will be very similar to the creation of an extension. But instead of creating the solution, we'll just call a batch file that performs an API environment verification. In short, you will need to do the following:
  • open a command prompt window
  • navigate to the folder where you created the solution
    • E.g. pushd d:\work\pvss\CtrlmySampleApplication
  • call vcvarsall.bat. First, you may need to find it.
    • In my case, using a default installation path, it was in: [C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\]. You could also add the folder to the system path if you use it a lot.
    •  E.g: call "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
  • call the batch file that checks whether the environment variables have been set
    • E.g: call "%API_ROOT%/checkAPIenv.cmd"
  • start the Visual Studio solution from the same command prompt window.
    • For Visual Studio Express: start /b vcexpress mySampleApplicationCtrlExt.sln
    • For Visual Studio Pro: start /b devenv mySampleApplicationCtrlExt.sln 
As repeating this can become tedious, just create a bat file and place the commands in it.
While this is not adding any new environment variables, it still needs to be set-up for each project.

Starting the created Visual Studio solutions freely

If you want to be able to double click the Visual Studio solution (*.sln) file and get a project that can be built already, you will need to ensure that some environment variables are set.
These would be set as variables in the checkAPIenv.cmd file. Browsing through the file, would reveal the following variables being set:
  • VC8_DEFS
    • defines some generic preprocessor definitions
    • The value for it should be: -D_CRT_NON_CONFORMING_SWPRINTFS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_USE_32BIT_TIME_T -DNODEF__RPC_H__ -DWINVER=0x0500 -DUSES_NEW_IOSTREAMS -I%API_ROOT%/include/winnt/pvssincl/vc8 -I%API_ROOT%/include/winnt/pvssincl -EHsc
  • API_INCL
    • defines the additional include directories to use
    • The value for it should be: .\;$(API_ROOT)\include\winnt\pvssincl;$(API_ROOT)\include\Basics\Utilities;$(API_ROOT)\include\Basics\Variables;$(API_ROOT)\include\Basics\NoPosix;$(API_ROOT)\include\Basics\DpBasics;$(API_ROOT)\include\BCMNew;$(API_ROOT)\include\Configs;$(API_ROOT)\include\Configs\DrvConfigs\DrvCommon;$(API_ROOT)\include\Configs\DrvConfigs\ConvSmooth;$(API_ROOT)\include\Datapoint;$(API_ROOT)\include\Messages;$(API_ROOT)\include\Manager;$(API_ROOT)\include\ComDrv;$(API_ROOT)\include\Ctrl
  • API_LIB
    • defines the library dependencies added to the build
    • The value for it should be: $(API_ROOT)\lib.winnt\libCtrl.lib;$(API_ROOT)\lib.winnt\libComDrv.lib;$(API_ROOT)\lib.winnt\libManager.lib;$(API_ROOT)\lib.winnt\libMessages.lib;$(API_ROOT)\lib.winnt\libDatapoint.lib;$(API_ROOT)\lib.winnt\libConfigs.lib;$(API_ROOT)\lib.winnt\libBasics.lib;$(API_ROOT)\lib.winnt\libV24.lib;$(API_ROOT)\lib.winnt\bcm.lib;wsock32.lib;netapi32.lib
  • D_PVSS_VER
    • defines the preprocessor definitions related to the WinCC OA version identifiers
    • The value for it should be: -DPVSS_VER_311 -DPVSS_VERS=311000 -DPVI_VERSION_Z=19 -DPVSS_VERS_BUILD=19 "-DPVSS_VERS_DLL=\"311000.19\""
You can either define the content manually, or (assuming you have a version of Windows newer than XP) you can just call SetX via the command line. You would need to run this only once.

Just open a command prompt window with administrative rights, run the following 4 commands and close the command prompt:
  • setx VC8_DEFS -D_CRT_NON_CONFORMING_SWPRINTFS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_USE_32BIT_TIME_T -DNODEF__RPC_H__ -DWINVER=0x0500 -DUSES_NEW_IOSTREAMS -I%API_ROOT%/include/winnt/pvssincl/vc8 -I%API_ROOT%/include/winnt/pvssincl -EHsc
  • setx API_INCL .\;$(API_ROOT)\include\winnt\pvssincl;$(API_ROOT)\include\Basics\Utilities;$(API_ROOT)\include\Basics\Variables;$(API_ROOT)\include\Basics\NoPosix;$(API_ROOT)\include\Basics\DpBasics;$(API_ROOT)\include\BCMNew;$(API_ROOT)\include\Configs;$(API_ROOT)\include\Configs\DrvConfigs\DrvCommon;$(API_ROOT)\include\Configs\DrvConfigs\ConvSmooth;$(API_ROOT)\include\Datapoint;$(API_ROOT)\include\Messages;$(API_ROOT)\include\Manager;$(API_ROOT)\include\ComDrv;$(API_ROOT)\include\Ctrl
  • setx API_LIB $(API_ROOT)\lib.winnt\libCtrl.lib;$(API_ROOT)\lib.winnt\libComDrv.lib;$(API_ROOT)\lib.winnt\libManager.lib;$(API_ROOT)\lib.winnt\libMessages.lib;$(API_ROOT)\lib.winnt\libDatapoint.lib;$(API_ROOT)\lib.winnt\libConfigs.lib;$(API_ROOT)\lib.winnt\libBasics.lib;$(API_ROOT)\lib.winnt\libV24.lib;$(API_ROOT)\lib.winnt\bcm.lib;wsock32.lib;netapi32.lib
  • setx D_PVSS_VER "-DPVSS_VER_311 -DPVSS_VERS=311000 -DPVI_VERSION_Z=19 -DPVSS_VERS_BUILD=19 \"-DPVSS_VERS_DLL=\\\"311000.19\\\"""

Things should be much easier now. The main problem with this second approach appears when you need to work with several WinCC-OA versions at the same time. Because (as you may have guessed), the different WinCC-OA versions use the same environment variable names, but they will need different values for the different versions. That means that when switching between versions, you would also need to update the environment variables.

Choose a method, open the created solution with it, and build the extension. If the build fails, you either missed a step, or I did not explain it correctly :-).

Next up, test the extension in a WinCC-OA control script.

No comments:

Post a Comment