[Insight-users] ITK and managed c++ 2005 - The Solution

Karthik Krishnan Karthik.Krishnan at kitware.com
Mon Oct 2 11:18:41 EDT 2006


Please put this on the Wiki if possible. A lot of users have asked for 
this before.

either in
  http://www.itk.org/Wiki/ITK
or in
  http://www.itk.org/Wiki/ITK_FAQ (if its not too long and qualifies as 
a FAQ entry)

Thank you very much
-karthik

Tadeusz Dracz wrote:

>For those interested in the solution, I have eventually managed to
>successfully compile and run C++ managed Windows Forms application with ITK
>(without showing the console window and without errors popping out at the
>runtime).
>
>The trick is to place all ITK code in a separate C++ managed class library
>and make your project dependent only on this library, not on the ITK.
>
>Best, 
>Tadeusz
>
>
>
>________________________________
>
>From: Dan Mueller [mailto:d.mueller at qut.edu.au] 
>Sent: Sunday, October 01, 2006 11:15 PM
>To: Tadeusz Dracz
>Cc: insight-users at itk.org
>Subject: Re: [Insight-users] ITK and managed c++ 2005
>
>
>Hi Tadeusz,
>
>I am indeed using System.Windows.Forms; however not from a C++ application,
>but a C# application. So I think there is definitely some hope!
>
>Okay, let's work this out. I have tried the minimal example you sent me and
>can confirm that it does produce the 'System.TypeInitializationException'
>you were reporting. I think the issue is with the vcproj file. Are you using
>CMake to configure your project? I *highly* recommend using it - in the long
>run it will save you heaps of time! CMake can be downloaded from
>http://www.cmake.org.
>
>Using CMake allows for all the myriad of compilation and linking options to
>be taken care of automatically (almost). Because we are using C++/CLI (ie.
>Managed C++ 2005) we need to set some of the options by hand (if anyone out
>there knowns how to set the below options actually using CMake I would love
>to here from you!). Please find attached a CMake script for configuring your
>minimal example. To build the example, follow these steps:
>
>    1. Open CMake and set the source and build paths (usually not the same
>place to keep things neat ie. 'out-of-source-build' )
>    2. Configure the project, making sure the ITK_DIR is automatically found
>(if not you will need to browse to it by hand)
>    3. Open the generated solution file
>    4. Right-click on the project file and select "Properties".
>    5. Navigate to "Configuration Properties > General" and set "Common
>Language Runtime support" to on (ie. /clr).
>        I have found that just adding the /clr switch as an additional
>compilation option using CMake does not work...anyone out there have any
>ideas?
>        Instead you must change the "ManagedExtensions" property value in
>the vcproj file from "0" to "1".
>    6. Navigate to "Configuration Properties > C/C++ > Code Generation" and
>change "Enable C++ Exceptions" to "Yes With SEH Exceptions" (ie. /EHa)
>        We need to do this because the /EHsc switch is not compatiable with
>the /clr switch.
>        Again I have found that just adding to /EHa switch with CMake does
>not work. It seems that /EHsc is deeply entrenched in CMake - I managed to
>trace it back to
>        CMAKE_CXX_FLAGS_INIT in
>share/CMake/Modules/Platform/Windows-cl.cmake...but I can't work out how to
>override it with /EHa.
>    7. Compile the project!
>
>I have added some more complexity to the minimal example just to make sure
>everything is working okay. Using this approach I can instantiate the
>ImageFileReader object with no problems.
>
>FYI:  I use a little custom program to 'fix' the output of the CMake project
>files. It's not an ideal solution, but it does allow me to use CMake to
>manage the compilation and linkage options. I can forward it to you if you
>are interested. Again, if anyone on the mailing list knows how to set these
>options using native CMake, I'd like to hear from them!
>
>Anyway, hope this helps!
>
>Cheers
>
>Dan
>
>
>Tadeusz Dracz wrote: 
>
>	Hi Dan, 
>	 
>	Do you use managed windows forms C++ application? Because my C++
>managed console applications run ok with ITK. If you do, there would be at
>least some hope for me that it is possible.
>	 
>	I have attached the simplest managed windows forms C++ application.
>I have removed all additional files and code, to keep it down to minimum.
>>From the standard settings I have changed from /clr:pure to /clr
>(Configuration Properties->General->Common Language Runtime Support),
>otherwise ITK would not link.
>	 
>	Before, I have compiled ITK using standard settings
>(BUILD_SHARED_LIBS is OFF).
>	 
>	There is a workaround that will make it run. This requires changing:
>	Linker->System->Subsystem: Not Set
>	Linker->Advanced->Entry Point: Empty
>	 
>	But this trick causes a console window to appear and this is
>supposed to be a Windows Forms application. 
>	This will look all right in the attached sample, because I have
>removed the form creation code, but it does not look pretty in the real
>application where both the console window and the form show up.
>	 
>	Best, 
>	Tadeusz 
>	
>
>________________________________
>
>	From: Dan Mueller [mailto:d.mueller at qut.edu.au] 
>	Sent: Saturday, September 30, 2006 11:14 PM
>	To: tdracz at o2.pl
>	Cc: insight-users at itk.org
>	Subject: Re: [Insight-users] ITK and managed c++ 2005
>	
>	
>	Hi Tadeusz,
>	
>	I have been using ITK with C++/CLI with no problems... I have
>experienced some "access violation exceptions" similar to yours below - but
>only when using a SmartPointer which has gone out of scope and deleted
>itself.
>	
>	Can you provide some more information, or perhaps a minimal example
>which fails?
>	
>	Cheers
>	
>	Dan
>	
>	tdracz at o2.pl wrote: 
>
>		Hello,
>		
>		I have created a sample C++ managed windows forms
>application that is
>		supposed to use ITK.
>		I have added all includes as in a normal unmanaged c++
>project. It compiles
>		without errors and warnings. 
>		But when executed, it throws an exception just at the start.
>Here is the
>		contents of the Output window:
>		
>		First-chance exception at 0x7c96cc82 in App.exe: 0xC0000005:
>Access
>		violation reading location 0x0d7cebd9.
>		First-chance exception at 0x781389db (msvcr80.dll) in
>App.exe: 0xC0000005:
>		Access violation writing location 0x0d7cebda.
>		A first chance exception of type
>'System.AccessViolationException' occurred
>		in App.exe
>		A first chance exception of type
>		'<CrtImplementationDetails>.ModuleLoadException' occurred in
>msvcm80.dll
>		First-chance exception at 0x7c812a5b in App.exe: Microsoft
>C++ exception:
>		[rethrow] at memory location 0x00000000..
>		An unhandled exception of type
>'System.TypeInitializationException' occurred
>		in Unknown Module.
>		Additional information: The type initializer for '<Module>'
>threw an
>		exception.
>		System.TypeInitializationException' occurred in Unknown
>Module.
>		
>		
>		The application contains just these lines that use ITK:
>		
>		#include "itkImageFileReader.h"
>		typedef unsigned short PixelType;
>		const unsigned int Dimension = 3;
>		typedef itk::Image< PixelType, Dimension > ImageType;
>		
>		Is there anybody that could help in this matter?
>		
>		Regards, 
>		Tadeusz
>		
>		_______________________________________________
>		Insight-users mailing list
>		Insight-users at itk.org
>		http://www.itk.org/mailman/listinfo/insight-users
>		  
>
>	________________________________
>
>
>	using namespace System;
>	#include "itkImageFileReader.h"
>	
>	[STAThreadAttribute]
>	int main(array<System::String ^> ^args)
>	{
>		return 0;
>	}
>	  
>
>
>_______________________________________________
>Insight-users mailing list
>Insight-users at itk.org
>http://www.itk.org/mailman/listinfo/insight-users
>
>  
>


More information about the Insight-users mailing list