1

Quick question, I'm developing a small program which I'd like to work with Kinect versions 1 and 2. Is there a preprocessor command I can use so that the c# compiler skips my Kinect v2.0 code if I don't have the Kinect 2.0 sdk installed? (When I'm working on Windows 7 for example).

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Wiredchop
  • 145
  • 1
  • 11

1 Answers1

4

Basically, like this:

#if KINECT1
   // ... kinect1 specifict code
#endif

#if KINECT2
   // ... kinect2 specific code
#endif

Of course, you would have to define those symbols manually, there is no builtin capability in the compiler or framework to detect which version is available, if at all.

You might be able to detect any installed Kinect SDK (version) by using MSBuild. Like for example, look for specific registry keys, paths on your local drives and/or set environment variables and then set those symbols from inside your project files.

For example, include the following fragment on the top of your .csproj file (or put it into a separate file that you <Import>).

 <PropertyGroup Condition="Exists('C:\Program Files\...\Whatever\v1.0')">
     <DefineConstants>KINECT1;$(DefineConstants)</DefineConstants>
 </PropertyGroup>
 <PropertyGroup Condition="Exists('C:\Program Files\...\Whatever\v2.0')">
     <DefineConstants>KINECT2;$(DefineConstants)</DefineConstants>
 </PropertyGroup>

(Mind you, the above if just an example though, no idea what would be good "trigger" to detect the version for kinect).

UPDATE

@Scott Chamberlain comment helped. Actually the Kinect 1.0 SDK sets the KINECTSDK10_DIR environment variable and the 2.0 SDK sets the KINECTSDK20_DIR envrionment variable.

So, you might do something like this (this might help as well):

 <PropertyGroup Condition="'$(KINECTSDK10_DIR)' != '' and Exists('$(KINECTSDK10_DIR)')">
     <DefineConstants>KINECT1;$(DefineConstants)</DefineConstants>
 </PropertyGroup>
 <PropertyGroup Condition="'$(KINECTSDK20_DIR)' != '' and Exists('$(KINECTSDK20_DIR)')">
     <DefineConstants>KINECT2;$(DefineConstants)</DefineConstants>
 </PropertyGroup>
Community
  • 1
  • 1
Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • As a fyi to people implementing the MSBuild solution: The SDK does create the environment variable `KINECTSDK_DIR` (see the "Changes in this release" section of the [readme](http://www.microsoft.com/en-us/kinectforwindows/develop/readme.htm)), the path is something similar to `C:\Program Files\Microsoft SDKs\Kinect\v1.0 Beta2` which should allow you to parse out the version. – Scott Chamberlain Sep 21 '15 at 15:47
  • Oh, cool it sets `KINECTSDK20_DIR`. I can't install the SDK on the machine I was on so I could not check. – Scott Chamberlain Sep 21 '15 at 15:53
  • Thanks for this guys, I've managed to get this to work, couple of notes though, this WOULD NOT work for me unless I added the propertygroup definitions _below_ all the existing propertygroups and above the first item group. I'm not sure whether this may be because the standard csproj file clears existing constants when it defines DEBUG, TRACE etc. – Wiredchop Sep 22 '15 at 12:05