Showing posts with label Visual Studios. Show all posts
Showing posts with label Visual Studios. Show all posts

Tuesday, October 23, 2012

Easy C++ Project for Python Use

For this tutorial, I will be using Visual Studios 2010 (C++) and Python 2.7 with ctypes.

Let's assume your boss wants a program that performs addition, there are many ways to write this code, but for the sake of this this tutorial, you want to use the python and C++.
  • Open Visual Studios
  • Follow the steps in creating a project here.
  • Give the project a name called additioncpp
  • Create a header file called 'additioncpp.h' and enter in the following code:
      int sum(int, int);
  •  In the dllmain.cpp file, comment out everything but the '#include "stdafx.h" line
  • Now we are ready to write the sum() we defined in the additioncpp.h header file.  Since python like 'C', and not 'C++' we need to use exten "C" around the functions
      #include "stdafx.h"
      #define DLLEXPORT extern "C" __declspec(dllexport)

      DLLEXPORT int sum(int a, int b) {
          return a + b;
       }

  • Compile the code and copy the .dll to a new folder
  • Create a new .py file and type in the following:
  • print 'start' 
    from ctypes import cdll
    mydll = cdll.LoadLibrary(r'PATHTODLL\additioncpp.dll')
    print mydll
    print mydll.sum
    print mydll.sum(1,2)
    print 'end'
  • You should see an output like this:
>>> 
>>> <_funcptr 0x10ca47b0="0x10ca47b0" at="at" object="object">
>>> 3
>>> end

Enjoy

Thursday, October 18, 2012

Setting up C++ Project for Python/ArcGIS Use

This article will discuss setting up a Visual Studios 2010 project to create a DLL using C++ which can be called by python using ctypes.

 1. Open Visual Studios
 2. Create a new C++ Win32 Project
 3. Give it a name, example: simplepythoncpp
 4. Press next 
 5. Select ‘Dll’ for the type of project
 6. Here you’ll have a set of created header files (.h) and Source Files (.cpp)
 7. To use ArcObjects, you need to reference the com libraries.  To do this, you need to tell visual studios where these libraries are.  To set the location, do the follow:
      a. In the Solution explorer, right click and select properties
      b. Select ‘VC++ Directories’
      c. Click on ‘Library Directories’ 
      d. Select ‘Edit’
      e. Navigate to the ArcGIS 10.1 install directory and select the ‘com’ folder
 8. Staying in the properties, we need to enable ‘C++/CLI’ 
      a. Click on ‘Command Lin’
      b. Under ‘Additional Options’ type ‘/clr’
 9. Next we need to define some ‘Code Generation’ options
      a. Click on ‘Code Generation’
      b. Under ‘Basic Runtime Checks’ select ‘Default’
 10. Press ‘Apply’ button then ‘OK’
Let’s test our project changes.
 1. Open the ‘stdafx.h’ header file
 2. Leave the default #include statements, and type in the following:

#import "esriSystem.olb" raw_interfaces_only, raw_native_types, no_namespace, named_guids, exclude("OLE_COLOR", "OLE_HANDLE", "VARTYPE")

 3. Compile project
No errors should appear, so your project is setup correct.  
Where do you do from here? Well, all the libraries you need in this header file, and reference them in your .cpp file.