diff options
Diffstat (limited to 'Examples/csharp')
76 files changed, 4075 insertions, 0 deletions
diff --git a/Examples/csharp/arrays/Makefile b/Examples/csharp/arrays/Makefile new file mode 100644 index 0000000..b3446d8 --- /dev/null +++ b/Examples/csharp/arrays/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SRCS = example.c +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -nologo -unsafe -out:runme.exe + +all:: csharp + +csharp:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean:: + $(MAKE) -f $(TOP)/Makefile csharp_clean + +check: all diff --git a/Examples/csharp/arrays/example.c b/Examples/csharp/arrays/example.c new file mode 100644 index 0000000..2498e1f --- /dev/null +++ b/Examples/csharp/arrays/example.c @@ -0,0 +1,22 @@ +/* File : example.c */ + +#include "example.h" + +/* copy the contents of the first array to the second */ +void myArrayCopy( int* sourceArray, int* targetArray, int nitems ) { + int i; + for ( i = 0; i < nitems; i++ ) { + targetArray[ i ] = sourceArray[ i ]; + } +} + +/* swap the contents of the two arrays */ +void myArraySwap( int* array1, int* array2, int nitems ) { + int i, temp; + for ( i = 0; i < nitems; i++ ) { + temp = array1[ i ]; + array1[ i ] = array2[ i ]; + array2[ i ] = temp; + } +} + diff --git a/Examples/csharp/arrays/example.h b/Examples/csharp/arrays/example.h new file mode 100644 index 0000000..113b92c --- /dev/null +++ b/Examples/csharp/arrays/example.h @@ -0,0 +1,4 @@ + +void myArrayCopy( int *sourceArray, int* targetArray, int nitems ); +void myArraySwap( int* array1, int* array2, int nitems ); + diff --git a/Examples/csharp/arrays/example.i b/Examples/csharp/arrays/example.i new file mode 100644 index 0000000..488565a --- /dev/null +++ b/Examples/csharp/arrays/example.i @@ -0,0 +1,45 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} +%include "arrays_csharp.i" + +%apply int INPUT[] { int* sourceArray } +%apply int OUTPUT[] { int* targetArray } + +%apply int INOUT[] { int* array1 } +%apply int INOUT[] { int* array2 } + +%include "example.h" + +%clear int* sourceArray; +%clear int* targetArray; + +%clear int* array1; +%clear int* array2; + + +// Below replicates the above array handling but this time using the pinned (fixed) array typemaps +%csmethodmodifiers "public unsafe"; + +%apply int FIXED[] { int* sourceArray } +%apply int FIXED[] { int* targetArray } + +%inline %{ +void myArrayCopyUsingFixedArrays( int *sourceArray, int* targetArray, int nitems ) { + myArrayCopy(sourceArray, targetArray, nitems); +} +%} + +%apply int FIXED[] { int* array1 } +%apply int FIXED[] { int* array2 } + +%inline %{ +void myArraySwapUsingFixedArrays( int* array1, int* array2, int nitems ) { + myArraySwap(array1, array2, nitems); +} +%} + + diff --git a/Examples/csharp/arrays/runme.cs b/Examples/csharp/arrays/runme.cs new file mode 100644 index 0000000..c2b8a6b --- /dev/null +++ b/Examples/csharp/arrays/runme.cs @@ -0,0 +1,43 @@ +using System; + +public class runme +{ + static void Main() + { + int[] source = { 1, 2, 3 }; + int[] target = new int[ source.Length ]; + + example.myArrayCopy( source, target, target.Length ); + + Console.WriteLine( "Contents of copy target array using default marshaling" ); + PrintArray( target ); + + target = new int[ source.Length ]; + + example.myArrayCopyUsingFixedArrays( source, target, target.Length ); + Console.WriteLine( "Contents of copy target array using fixed arrays" ); + PrintArray( target ); + + target = new int[] { 4, 5, 6 }; + example.myArraySwap( source, target, target.Length ); + Console.WriteLine( "Contents of arrays after swapping using default marshaling" ); + PrintArray( source ); + PrintArray( target ); + + source = new int[] { 1, 2, 3 }; + target = new int[] { 4, 5, 6 }; + + example.myArraySwapUsingFixedArrays( source, target, target.Length ); + Console.WriteLine( "Contents of arrays after swapping using fixed arrays" ); + PrintArray( source ); + PrintArray( target ); + } + + static void PrintArray( int[] a ) + { + foreach ( int i in a ) + Console.Write( "{0} ", i ); + Console.WriteLine(); + } +} + diff --git a/Examples/csharp/callback/Makefile b/Examples/csharp/callback/Makefile new file mode 100644 index 0000000..51b163b --- /dev/null +++ b/Examples/csharp/callback/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -debug -nologo -out:runme.exe + +all:: csharp + +csharp:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp_cpp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean:: + $(MAKE) -f $(TOP)/Makefile csharp_clean + +check: all diff --git a/Examples/csharp/callback/example-cs.csproj b/Examples/csharp/callback/example-cs.csproj new file mode 100644 index 0000000..5fc3452 --- /dev/null +++ b/Examples/csharp/callback/example-cs.csproj @@ -0,0 +1,99 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "runme" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Exe" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "runme" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;TRACE" + DocumentationFile = "" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "TRACE" + DocumentationFile = "" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "true" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References/> + </Build> + <Files> + <Include> + <File + RelPath = "Callback.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Caller.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "example.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "examplePINVOKE.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "runme.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/Examples/csharp/callback/example-vc.vcproj b/Examples/csharp/callback/example-vc.vcproj new file mode 100644 index 0000000..7c0b8ef --- /dev/null +++ b/Examples/csharp/callback/example-vc.vcproj @@ -0,0 +1,158 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="7.10" + Name="example-vc" + ProjectGUID="{C2302635-D489-4678-96B4-70F5309DCBE6}" + Keyword="Win32Proj"> + <Platforms> + <Platform + Name="Win32"/> + </Platforms> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + MinimalRebuild="TRUE" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="4"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="2" + GenerateDebugInformation="TRUE" + ProgramDatabaseFile="$(OutDir)/example-vc.pdb" + SubSystem="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="3"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="1" + GenerateDebugInformation="TRUE" + SubSystem="2" + OptimizeReferences="2" + EnableCOMDATFolding="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> + <File + RelativePath="example.cxx"> + </File> + <File + RelativePath="example_wrap.cxx"> + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> + <File + RelativePath="example.h"> + </File> + </Filter> + <File + RelativePath=".\example.i"> + <FileConfiguration + Name="Debug|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + <FileConfiguration + Name="Release|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + </File> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/Examples/csharp/callback/example.cxx b/Examples/csharp/callback/example.cxx new file mode 100644 index 0000000..450d756 --- /dev/null +++ b/Examples/csharp/callback/example.cxx @@ -0,0 +1,4 @@ +/* File : example.cxx */ + +#include "example.h" + diff --git a/Examples/csharp/callback/example.h b/Examples/csharp/callback/example.h new file mode 100644 index 0000000..38d25a0 --- /dev/null +++ b/Examples/csharp/callback/example.h @@ -0,0 +1,24 @@ +/* File : example.h */ + +#include <cstdio> +#include <iostream> + +class Callback { +public: + virtual ~Callback() { std::cout << "Callback::~Callback()" << std:: endl; } + virtual void run() { std::cout << "Callback::run()" << std::endl; } +}; + + +class Caller { +private: + Callback *_callback; +public: + Caller(): _callback(0) {} + ~Caller() { delCallback(); } + void delCallback() { delete _callback; _callback = 0; } + void setCallback(Callback *cb) { delCallback(); _callback = cb; } + void resetCallback() { _callback = 0; } + void call() { if (_callback) _callback->run(); } +}; + diff --git a/Examples/csharp/callback/example.i b/Examples/csharp/callback/example.i new file mode 100644 index 0000000..90beda0 --- /dev/null +++ b/Examples/csharp/callback/example.i @@ -0,0 +1,13 @@ +/* File : example.i */ +%module(directors="1") example +%{ +#include "example.h" +%} + +%include "std_string.i" + +/* turn on director wrapping Callback */ +%feature("director") Callback; + +%include "example.h" + diff --git a/Examples/csharp/callback/example.sln b/Examples/csharp/callback/example.sln new file mode 100644 index 0000000..28b9851 --- /dev/null +++ b/Examples/csharp/callback/example.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example-cs", "example-cs.csproj", "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + ProjectSection(ProjectDependencies) = postProject + {C2302635-D489-4678-96B4-70F5309DCBE6} = {C2302635-D489-4678-96B4-70F5309DCBE6} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-vc", "example-vc.vcproj", "{C2302635-D489-4678-96B4-70F5309DCBE6}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.ActiveCfg = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.Build.0 = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.ActiveCfg = Release|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.Build.0 = Release|.NET + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.ActiveCfg = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.Build.0 = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.ActiveCfg = Release|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/Examples/csharp/callback/runme.cs b/Examples/csharp/callback/runme.cs new file mode 100644 index 0000000..a104b1a --- /dev/null +++ b/Examples/csharp/callback/runme.cs @@ -0,0 +1,46 @@ +using System; + +public class runme +{ + static void Main() + { + Console.WriteLine("Adding and calling a normal C++ callback"); + Console.WriteLine("----------------------------------------"); + + Caller caller = new Caller(); + using (Callback callback = new Callback()) + { + caller.setCallback(callback); + caller.call(); + caller.resetCallback(); + } + + Console.WriteLine(); + Console.WriteLine("Adding and calling a C# callback"); + Console.WriteLine("------------------------------------"); + + using (Callback callback = new CSharpCallback()) + { + caller.setCallback(callback); + caller.call(); + caller.resetCallback(); + } + + Console.WriteLine(); + Console.WriteLine("C# exit"); + } +} + +public class CSharpCallback : Callback +{ + public CSharpCallback() + : base() + { + } + + public override void run() + { + Console.WriteLine("CSharpCallback.run()"); + } +} + diff --git a/Examples/csharp/check.list b/Examples/csharp/check.list new file mode 100644 index 0000000..5454d85 --- /dev/null +++ b/Examples/csharp/check.list @@ -0,0 +1,11 @@ +# see top-level Makefile.in +arrays +callback +class +enum +extend +funcptr +reference +simple +template +variables diff --git a/Examples/csharp/class/Makefile b/Examples/csharp/class/Makefile new file mode 100644 index 0000000..20f0dd5 --- /dev/null +++ b/Examples/csharp/class/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -nologo -out:runme.exe + +all:: csharp + +csharp:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp_cpp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean:: + $(MAKE) -f $(TOP)/Makefile csharp_clean + +check: all diff --git a/Examples/csharp/class/example-cs.csproj b/Examples/csharp/class/example-cs.csproj new file mode 100644 index 0000000..0b9ea2e --- /dev/null +++ b/Examples/csharp/class/example-cs.csproj @@ -0,0 +1,104 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "runme" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Exe" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "runme" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;TRACE" + DocumentationFile = "" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "TRACE" + DocumentationFile = "" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "true" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References/> + </Build> + <Files> + <Include> + <File + RelPath = "Circle.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "example.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "examplePINVOKE.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "runme.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Shape.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Square.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/Examples/csharp/class/example-vc.vcproj b/Examples/csharp/class/example-vc.vcproj new file mode 100644 index 0000000..7c0b8ef --- /dev/null +++ b/Examples/csharp/class/example-vc.vcproj @@ -0,0 +1,158 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="7.10" + Name="example-vc" + ProjectGUID="{C2302635-D489-4678-96B4-70F5309DCBE6}" + Keyword="Win32Proj"> + <Platforms> + <Platform + Name="Win32"/> + </Platforms> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + MinimalRebuild="TRUE" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="4"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="2" + GenerateDebugInformation="TRUE" + ProgramDatabaseFile="$(OutDir)/example-vc.pdb" + SubSystem="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="3"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="1" + GenerateDebugInformation="TRUE" + SubSystem="2" + OptimizeReferences="2" + EnableCOMDATFolding="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> + <File + RelativePath="example.cxx"> + </File> + <File + RelativePath="example_wrap.cxx"> + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> + <File + RelativePath="example.h"> + </File> + </Filter> + <File + RelativePath=".\example.i"> + <FileConfiguration + Name="Debug|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + <FileConfiguration + Name="Release|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + </File> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/Examples/csharp/class/example.cxx b/Examples/csharp/class/example.cxx new file mode 100644 index 0000000..1e8e203 --- /dev/null +++ b/Examples/csharp/class/example.cxx @@ -0,0 +1,28 @@ +/* File : example.c */ + +#include "example.h" +#define M_PI 3.14159265358979323846 + +/* Move the shape to a new location */ +void Shape::move(double dx, double dy) { + x += dx; + y += dy; +} + +int Shape::nshapes = 0; + +double Circle::area(void) { + return M_PI*radius*radius; +} + +double Circle::perimeter(void) { + return 2*M_PI*radius; +} + +double Square::area(void) { + return width*width; +} + +double Square::perimeter(void) { + return 4*width; +} diff --git a/Examples/csharp/class/example.h b/Examples/csharp/class/example.h new file mode 100644 index 0000000..46d9013 --- /dev/null +++ b/Examples/csharp/class/example.h @@ -0,0 +1,39 @@ +/* File : example.h */ + +class Shape { +public: + Shape() { + nshapes++; + } + virtual ~Shape() { + nshapes--; + }; + double x, y; + void move(double dx, double dy); + virtual double area(void) = 0; + virtual double perimeter(void) = 0; + static int nshapes; +}; + +class Circle : public Shape { +private: + double radius; +public: + Circle(double r) : radius(r) { }; + virtual double area(void); + virtual double perimeter(void); +}; + +class Square : public Shape { +private: + double width; +public: + Square(double w) : width(w) { }; + virtual double area(void); + virtual double perimeter(void); +}; + + + + + diff --git a/Examples/csharp/class/example.i b/Examples/csharp/class/example.i new file mode 100644 index 0000000..75700b3 --- /dev/null +++ b/Examples/csharp/class/example.i @@ -0,0 +1,10 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + diff --git a/Examples/csharp/class/example.sln b/Examples/csharp/class/example.sln new file mode 100644 index 0000000..28b9851 --- /dev/null +++ b/Examples/csharp/class/example.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example-cs", "example-cs.csproj", "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + ProjectSection(ProjectDependencies) = postProject + {C2302635-D489-4678-96B4-70F5309DCBE6} = {C2302635-D489-4678-96B4-70F5309DCBE6} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-vc", "example-vc.vcproj", "{C2302635-D489-4678-96B4-70F5309DCBE6}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.ActiveCfg = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.Build.0 = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.ActiveCfg = Release|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.Build.0 = Release|.NET + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.ActiveCfg = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.Build.0 = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.ActiveCfg = Release|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/Examples/csharp/class/runme.cs b/Examples/csharp/class/runme.cs new file mode 100644 index 0000000..9088031 --- /dev/null +++ b/Examples/csharp/class/runme.cs @@ -0,0 +1,66 @@ +// This example illustrates how C++ classes can be used from C# using SWIG. +// The C# class gets mapped onto the C++ class and behaves as if it is a C# class. + +using System; + +public class runme +{ + static void Main() + { + // ----- Object creation ----- + + Console.WriteLine( "Creating some objects:" ); + + using (Square s = new Square(10)) + using (Circle c = new Circle(10)) + { + Console.WriteLine( " Created circle " + c ); + Console.WriteLine( " Created square " + s ); + + // ----- Access a static member ----- + + Console.WriteLine( "\nA total of " + Shape.nshapes + " shapes were created" ); + + // ----- Member data access ----- + + // Notice how we can do this using functions specific to + // the 'Circle' class. + c.x = 20; + c.y = 30; + + // Now use the same functions in the base class + Shape shape = s; + shape.x = -10; + shape.y = 5; + + Console.WriteLine( "\nHere is their current position:" ); + Console.WriteLine( " Circle = (" + c.x + " " + c.y + ")" ); + Console.WriteLine( " Square = (" + s.x + " " + s.y + ")" ); + + // ----- Call some methods ----- + + Console.WriteLine( "\nHere are some properties of the shapes:" ); + Shape[] shapes = {c,s}; + // for (int i=0; i<shapes.Size; i++) + for (int i=0; i<2; i++) + { + Console.WriteLine( " " + shapes[i].ToString() ); + Console.WriteLine( " area = " + shapes[i].area() ); + Console.WriteLine( " perimeter = " + shapes[i].perimeter() ); + } + + // Notice how the area() and perimeter() functions really + // invoke the appropriate virtual method on each object. + + // ----- Delete everything ----- + + Console.WriteLine( "\nGuess I'll clean up now" ); + + } + // Note: when this using scope is exited the C# Dispose() methods + // are called which in turn call the C++ destructors + + Console.WriteLine( Shape.nshapes + " shapes remain" ); + Console.WriteLine( "Goodbye" ); + } +} diff --git a/Examples/csharp/enum/Makefile b/Examples/csharp/enum/Makefile new file mode 100644 index 0000000..20f0dd5 --- /dev/null +++ b/Examples/csharp/enum/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -nologo -out:runme.exe + +all:: csharp + +csharp:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp_cpp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean:: + $(MAKE) -f $(TOP)/Makefile csharp_clean + +check: all diff --git a/Examples/csharp/enum/example-cs.csproj b/Examples/csharp/enum/example-cs.csproj new file mode 100644 index 0000000..9d37ce2 --- /dev/null +++ b/Examples/csharp/enum/example-cs.csproj @@ -0,0 +1,99 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "runme" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Exe" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "runme" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;TRACE" + DocumentationFile = "" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "TRACE" + DocumentationFile = "" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "true" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References/> + </Build> + <Files> + <Include> + <File + RelPath = "color.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "example.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "examplePINVOKE.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Foo.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "runme.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/Examples/csharp/enum/example-vc.vcproj b/Examples/csharp/enum/example-vc.vcproj new file mode 100644 index 0000000..7c0b8ef --- /dev/null +++ b/Examples/csharp/enum/example-vc.vcproj @@ -0,0 +1,158 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="7.10" + Name="example-vc" + ProjectGUID="{C2302635-D489-4678-96B4-70F5309DCBE6}" + Keyword="Win32Proj"> + <Platforms> + <Platform + Name="Win32"/> + </Platforms> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + MinimalRebuild="TRUE" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="4"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="2" + GenerateDebugInformation="TRUE" + ProgramDatabaseFile="$(OutDir)/example-vc.pdb" + SubSystem="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="3"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="1" + GenerateDebugInformation="TRUE" + SubSystem="2" + OptimizeReferences="2" + EnableCOMDATFolding="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> + <File + RelativePath="example.cxx"> + </File> + <File + RelativePath="example_wrap.cxx"> + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> + <File + RelativePath="example.h"> + </File> + </Filter> + <File + RelativePath=".\example.i"> + <FileConfiguration + Name="Debug|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + <FileConfiguration + Name="Release|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + </File> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/Examples/csharp/enum/example.cxx b/Examples/csharp/enum/example.cxx new file mode 100644 index 0000000..df7bb63 --- /dev/null +++ b/Examples/csharp/enum/example.cxx @@ -0,0 +1,37 @@ +/* File : example.cxx */ + +#include "example.h" +#include <stdio.h> + +void Foo::enum_test(speed s) { + if (s == IMPULSE) { + printf("IMPULSE speed\n"); + } else if (s == WARP) { + printf("WARP speed\n"); + } else if (s == LUDICROUS) { + printf("LUDICROUS speed\n"); + } else { + printf("Unknown speed\n"); + } +} + +void enum_test(color c, Foo::speed s) { + if (c == RED) { + printf("color = RED, "); + } else if (c == BLUE) { + printf("color = BLUE, "); + } else if (c == GREEN) { + printf("color = GREEN, "); + } else { + printf("color = Unknown color!, "); + } + if (s == Foo::IMPULSE) { + printf("speed = IMPULSE speed\n"); + } else if (s == Foo::WARP) { + printf("speed = WARP speed\n"); + } else if (s == Foo::LUDICROUS) { + printf("speed = LUDICROUS speed\n"); + } else { + printf("speed = Unknown speed!\n"); + } +} diff --git a/Examples/csharp/enum/example.h b/Examples/csharp/enum/example.h new file mode 100644 index 0000000..9119cd9 --- /dev/null +++ b/Examples/csharp/enum/example.h @@ -0,0 +1,13 @@ +/* File : example.h */ + +enum color { RED, BLUE, GREEN }; + +class Foo { + public: + Foo() { } + enum speed { IMPULSE=10, WARP=20, LUDICROUS=30 }; + void enum_test(speed s); +}; + +void enum_test(color c, Foo::speed s); + diff --git a/Examples/csharp/enum/example.i b/Examples/csharp/enum/example.i new file mode 100644 index 0000000..23ee8a8 --- /dev/null +++ b/Examples/csharp/enum/example.i @@ -0,0 +1,11 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ + +%include "example.h" + diff --git a/Examples/csharp/enum/example.sln b/Examples/csharp/enum/example.sln new file mode 100644 index 0000000..28b9851 --- /dev/null +++ b/Examples/csharp/enum/example.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example-cs", "example-cs.csproj", "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + ProjectSection(ProjectDependencies) = postProject + {C2302635-D489-4678-96B4-70F5309DCBE6} = {C2302635-D489-4678-96B4-70F5309DCBE6} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-vc", "example-vc.vcproj", "{C2302635-D489-4678-96B4-70F5309DCBE6}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.ActiveCfg = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.Build.0 = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.ActiveCfg = Release|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.Build.0 = Release|.NET + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.ActiveCfg = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.Build.0 = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.ActiveCfg = Release|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/Examples/csharp/enum/runme.cs b/Examples/csharp/enum/runme.cs new file mode 100644 index 0000000..42ba896 --- /dev/null +++ b/Examples/csharp/enum/runme.cs @@ -0,0 +1,31 @@ +using System; + +public class runme +{ + static void Main() + { + // Print out the value of some enums + Console.WriteLine("*** color ***"); + Console.WriteLine(" " + color.RED + " = " + (int)color.RED); + Console.WriteLine(" " + color.BLUE + " = " + (int)color.BLUE); + Console.WriteLine(" " + color.GREEN + " = " + (int)color.GREEN); + + Console.WriteLine("\n*** Foo::speed ***"); + Console.WriteLine(" Foo::" + Foo.speed.IMPULSE + " = " + (int)Foo.speed.IMPULSE); + Console.WriteLine(" Foo::" + Foo.speed.WARP + " = " + (int)Foo.speed.WARP); + Console.WriteLine(" Foo::" + Foo.speed.LUDICROUS + " = " + (int)Foo.speed.LUDICROUS); + + Console.WriteLine("\nTesting use of enums with functions\n"); + + example.enum_test(color.RED, Foo.speed.IMPULSE); + example.enum_test(color.BLUE, Foo.speed.WARP); + example.enum_test(color.GREEN, Foo.speed.LUDICROUS); + + Console.WriteLine( "\nTesting use of enum with class method" ); + Foo f = new Foo(); + + f.enum_test(Foo.speed.IMPULSE); + f.enum_test(Foo.speed.WARP); + f.enum_test(Foo.speed.LUDICROUS); + } +} diff --git a/Examples/csharp/extend/Makefile b/Examples/csharp/extend/Makefile new file mode 100644 index 0000000..20f0dd5 --- /dev/null +++ b/Examples/csharp/extend/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -nologo -out:runme.exe + +all:: csharp + +csharp:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp_cpp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean:: + $(MAKE) -f $(TOP)/Makefile csharp_clean + +check: all diff --git a/Examples/csharp/extend/example-cs.csproj b/Examples/csharp/extend/example-cs.csproj new file mode 100644 index 0000000..4e88746 --- /dev/null +++ b/Examples/csharp/extend/example-cs.csproj @@ -0,0 +1,104 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "runme" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Exe" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "runme" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;TRACE" + DocumentationFile = "" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "TRACE" + DocumentationFile = "" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "true" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References/> + </Build> + <Files> + <Include> + <File + RelPath = "Employee.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "EmployeeList.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Manager.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "example.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "examplePINVOKE.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "runme.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/Examples/csharp/extend/example-vc.vcproj b/Examples/csharp/extend/example-vc.vcproj new file mode 100644 index 0000000..7c0b8ef --- /dev/null +++ b/Examples/csharp/extend/example-vc.vcproj @@ -0,0 +1,158 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="7.10" + Name="example-vc" + ProjectGUID="{C2302635-D489-4678-96B4-70F5309DCBE6}" + Keyword="Win32Proj"> + <Platforms> + <Platform + Name="Win32"/> + </Platforms> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + MinimalRebuild="TRUE" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="4"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="2" + GenerateDebugInformation="TRUE" + ProgramDatabaseFile="$(OutDir)/example-vc.pdb" + SubSystem="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="3"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="1" + GenerateDebugInformation="TRUE" + SubSystem="2" + OptimizeReferences="2" + EnableCOMDATFolding="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> + <File + RelativePath="example.cxx"> + </File> + <File + RelativePath="example_wrap.cxx"> + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> + <File + RelativePath="example.h"> + </File> + </Filter> + <File + RelativePath=".\example.i"> + <FileConfiguration + Name="Debug|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + <FileConfiguration + Name="Release|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + </File> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/Examples/csharp/extend/example.cxx b/Examples/csharp/extend/example.cxx new file mode 100644 index 0000000..450d756 --- /dev/null +++ b/Examples/csharp/extend/example.cxx @@ -0,0 +1,4 @@ +/* File : example.cxx */ + +#include "example.h" + diff --git a/Examples/csharp/extend/example.h b/Examples/csharp/extend/example.h new file mode 100644 index 0000000..b27ab97 --- /dev/null +++ b/Examples/csharp/extend/example.h @@ -0,0 +1,56 @@ +/* File : example.h */ + +#include <cstdio> +#include <iostream> +#include <vector> +#include <string> +#include <cmath> + +class Employee { +private: + std::string name; +public: + Employee(const char* n): name(n) {} + virtual std::string getTitle() { return getPosition() + " " + getName(); } + virtual std::string getName() { return name; } + virtual std::string getPosition() const { return "Employee"; } + virtual ~Employee() { printf("~Employee() @ %p\n", this); } +}; + + +class Manager: public Employee { +public: + Manager(const char* n): Employee(n) {} + virtual std::string getPosition() const { return "Manager"; } +}; + + +class EmployeeList { + std::vector<Employee*> list; +public: + EmployeeList() { + list.push_back(new Employee("Bob")); + list.push_back(new Employee("Jane")); + list.push_back(new Manager("Ted")); + } + void addEmployee(Employee *p) { + list.push_back(p); + std::cout << "New employee added. Current employees are:" << std::endl; + std::vector<Employee*>::iterator i; + for (i=list.begin(); i!=list.end(); i++) { + std::cout << " " << (*i)->getTitle() << std::endl; + } + } + const Employee *get_item(int i) { + return list[i]; + } + ~EmployeeList() { + std::vector<Employee*>::iterator i; + std::cout << "~EmployeeList, deleting " << list.size() << " employees." << std::endl; + for (i=list.begin(); i!=list.end(); i++) { + delete *i; + } + std::cout << "~EmployeeList empty." << std::endl; + } +}; + diff --git a/Examples/csharp/extend/example.i b/Examples/csharp/extend/example.i new file mode 100644 index 0000000..c8ec32e --- /dev/null +++ b/Examples/csharp/extend/example.i @@ -0,0 +1,15 @@ +/* File : example.i */ +%module(directors="1") example +%{ +#include "example.h" +%} + +%include "std_vector.i" +%include "std_string.i" + +/* turn on director wrapping for Manager */ +%feature("director") Employee; +%feature("director") Manager; + +%include "example.h" + diff --git a/Examples/csharp/extend/example.sln b/Examples/csharp/extend/example.sln new file mode 100644 index 0000000..28b9851 --- /dev/null +++ b/Examples/csharp/extend/example.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example-cs", "example-cs.csproj", "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + ProjectSection(ProjectDependencies) = postProject + {C2302635-D489-4678-96B4-70F5309DCBE6} = {C2302635-D489-4678-96B4-70F5309DCBE6} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-vc", "example-vc.vcproj", "{C2302635-D489-4678-96B4-70F5309DCBE6}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.ActiveCfg = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.Build.0 = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.ActiveCfg = Release|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.Build.0 = Release|.NET + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.ActiveCfg = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.Build.0 = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.ActiveCfg = Release|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/Examples/csharp/extend/runme.cs b/Examples/csharp/extend/runme.cs new file mode 100644 index 0000000..825dcdb --- /dev/null +++ b/Examples/csharp/extend/runme.cs @@ -0,0 +1,77 @@ +// This file illustrates the cross language polymorphism using directors. + +using System; + +// CEO class, which overrides Employee::getPosition(). + +class CEO : Manager { + public CEO(String name) : base(name) { + } + public override String getPosition() { + return "CEO"; + } + // Public method to stop the SWIG proxy base class from thinking it owns the underlying C++ memory. + public void disownMemory() { + swigCMemOwn = false; + } +} + + +public class runme +{ + static void Main() + { + // Create an instance of CEO, a class derived from the C# proxy of the + // underlying C++ class. The calls to getName() and getPosition() are standard, + // the call to getTitle() uses the director wrappers to call CEO.getPosition(). + + CEO e = new CEO("Alice"); + Console.WriteLine( e.getName() + " is a " + e.getPosition() ); + Console.WriteLine( "Just call her \"" + e.getTitle() + "\"" ); + Console.WriteLine( "----------------------" ); + + // Create a new EmployeeList instance. This class does not have a C++ + // director wrapper, but can be used freely with other classes that do. + + using (EmployeeList list = new EmployeeList()) { + + // EmployeeList owns its items, so we must surrender ownership of objects we add. + e.disownMemory(); + list.addEmployee(e); + Console.WriteLine( "----------------------" ); + + // Now we access the first four items in list (three are C++ objects that + // EmployeeList's constructor adds, the last is our CEO). The virtual + // methods of all these instances are treated the same. For items 0, 1, and + // 2, all methods resolve in C++. For item 3, our CEO, getTitle calls + // getPosition which resolves in C#. The call to getPosition is + // slightly different, however, because of the overidden getPosition() call, since + // now the object reference has been "laundered" by passing through + // EmployeeList as an Employee*. Previously, C# resolved the call + // immediately in CEO, but now C# thinks the object is an instance of + // class Employee. So the call passes through the + // Employee proxy class and on to the C wrappers and C++ director, + // eventually ending up back at the C# CEO implementation of getPosition(). + // The call to getTitle() for item 3 runs the C++ Employee::getTitle() + // method, which in turn calls getPosition(). This virtual method call + // passes down through the C++ director class to the C# implementation + // in CEO. All this routing takes place transparently. + + Console.WriteLine( "(position, title) for items 0-3:" ); + + Console.WriteLine( " " + list.get_item(0).getPosition() + ", \"" + list.get_item(0).getTitle() + "\"" ); + Console.WriteLine( " " + list.get_item(1).getPosition() + ", \"" + list.get_item(1).getTitle() + "\"" ); + Console.WriteLine( " " + list.get_item(2).getPosition() + ", \"" + list.get_item(2).getTitle() + "\"" ); + Console.WriteLine( " " + list.get_item(3).getPosition() + ", \"" + list.get_item(3).getTitle() + "\"" ); + Console.WriteLine( "----------------------" ); + + // The using statement ensures the EmployeeList.Dispose() will be called, which will delete all the Employee* + // items it contains. The last item is our CEO, which gets destroyed as well. + } + Console.WriteLine( "----------------------" ); + + // All done. + + Console.WriteLine( "C# exit" ); + } +} diff --git a/Examples/csharp/funcptr/Makefile b/Examples/csharp/funcptr/Makefile new file mode 100644 index 0000000..2233004 --- /dev/null +++ b/Examples/csharp/funcptr/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SRCS = example.c +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -nologo -out:runme.exe + +all:: csharp + +csharp:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean:: + $(MAKE) -f $(TOP)/Makefile csharp_clean + +check: all diff --git a/Examples/csharp/funcptr/example-cs.csproj b/Examples/csharp/funcptr/example-cs.csproj new file mode 100644 index 0000000..5ff0d9d --- /dev/null +++ b/Examples/csharp/funcptr/example-cs.csproj @@ -0,0 +1,94 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "runme" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Exe" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "runme" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;TRACE" + DocumentationFile = "" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "TRACE" + DocumentationFile = "" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "true" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References/> + </Build> + <Files> + <Include> + <File + RelPath = "example.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "examplePINVOKE.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "runme.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "SWIGTYPE_p_f_int_int__int.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/Examples/csharp/funcptr/example-vc.vcproj b/Examples/csharp/funcptr/example-vc.vcproj new file mode 100644 index 0000000..03047bf --- /dev/null +++ b/Examples/csharp/funcptr/example-vc.vcproj @@ -0,0 +1,160 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="7.10" + Name="example-vc" + ProjectGUID="{C2302635-D489-4678-96B4-70F5309DCBE6}" + Keyword="Win32Proj"> + <Platforms> + <Platform + Name="Win32"/> + </Platforms> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + MinimalRebuild="TRUE" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="4"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="2" + GenerateDebugInformation="TRUE" + ProgramDatabaseFile="$(OutDir)/example-vc.pdb" + SubSystem="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="3"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="1" + GenerateDebugInformation="TRUE" + SubSystem="2" + OptimizeReferences="2" + EnableCOMDATFolding="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> + <File + RelativePath=".\example.c"> + </File> + <File + RelativePath=".\example_wrap.c"> + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> + <File + RelativePath=".\example.h"> + </File> + </Filter> + <File + RelativePath=".\example.i"> + <FileConfiguration + Name="Debug|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -csharp "$(InputPath)" +@echo off +" + Outputs="$(InputName)_wrap.c"/> + </FileConfiguration> + <FileConfiguration + Name="Release|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -csharp "$(InputPath)" +@echo off +" + Outputs="$(InputName)_wrap.c"/> + </FileConfiguration> + </File> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/Examples/csharp/funcptr/example.c b/Examples/csharp/funcptr/example.c new file mode 100644 index 0000000..5c4a3da --- /dev/null +++ b/Examples/csharp/funcptr/example.c @@ -0,0 +1,19 @@ +/* File : example.c */ + +int do_op(int a, int b, int (*op)(int,int)) { + return (*op)(a,b); +} + +int add(int a, int b) { + return a+b; +} + +int sub(int a, int b) { + return a-b; +} + +int mul(int a, int b) { + return a*b; +} + +int (*funcvar)(int,int) = add; diff --git a/Examples/csharp/funcptr/example.h b/Examples/csharp/funcptr/example.h new file mode 100644 index 0000000..9936e24 --- /dev/null +++ b/Examples/csharp/funcptr/example.h @@ -0,0 +1,9 @@ +/* file: example.h */ + +extern int do_op(int,int, int (*op)(int,int)); +extern int add(int,int); +extern int sub(int,int); +extern int mul(int,int); + +extern int (*funcvar)(int,int); + diff --git a/Examples/csharp/funcptr/example.i b/Examples/csharp/funcptr/example.i new file mode 100644 index 0000000..8b3bef6 --- /dev/null +++ b/Examples/csharp/funcptr/example.i @@ -0,0 +1,16 @@ +/* File : example.i */ +%module example +%{ +#include "example.h" +%} + +/* Wrap a function taking a pointer to a function */ +extern int do_op(int a, int b, int (*op)(int, int)); + +/* Now install a bunch of "ops" as constants */ +%constant int (*ADD)(int,int) = add; +%constant int (*SUB)(int,int) = sub; +%constant int (*MUL)(int,int) = mul; + +extern int (*funcvar)(int,int); + diff --git a/Examples/csharp/funcptr/example.sln b/Examples/csharp/funcptr/example.sln new file mode 100644 index 0000000..28b9851 --- /dev/null +++ b/Examples/csharp/funcptr/example.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example-cs", "example-cs.csproj", "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + ProjectSection(ProjectDependencies) = postProject + {C2302635-D489-4678-96B4-70F5309DCBE6} = {C2302635-D489-4678-96B4-70F5309DCBE6} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-vc", "example-vc.vcproj", "{C2302635-D489-4678-96B4-70F5309DCBE6}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.ActiveCfg = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.Build.0 = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.ActiveCfg = Release|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.Build.0 = Release|.NET + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.ActiveCfg = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.Build.0 = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.ActiveCfg = Release|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/Examples/csharp/funcptr/runme.cs b/Examples/csharp/funcptr/runme.cs new file mode 100644 index 0000000..ba2e228 --- /dev/null +++ b/Examples/csharp/funcptr/runme.cs @@ -0,0 +1,27 @@ + +using System; +using System.Reflection; + +public class runme { + + public static void Main(String[] args) { + + + int a = 37; + int b = 42; + + // Now call our C function with a bunch of callbacks + + Console.WriteLine( "Trying some C callback functions" ); + Console.WriteLine( " a = " + a ); + Console.WriteLine( " b = " + b ); + Console.WriteLine( " ADD(a,b) = " + example.do_op(a,b,example.ADD) ); + Console.WriteLine( " SUB(a,b) = " + example.do_op(a,b,example.SUB) ); + Console.WriteLine( " MUL(a,b) = " + example.do_op(a,b,example.MUL) ); + + Console.WriteLine( "Here is what the C callback function classes are called in C#" ); + Console.WriteLine( " ADD = " + example.ADD.GetType() ); + Console.WriteLine( " SUB = " + example.SUB.GetType() ); + Console.WriteLine( " MUL = " + example.MUL.GetType() ); + } +} diff --git a/Examples/csharp/reference/Makefile b/Examples/csharp/reference/Makefile new file mode 100644 index 0000000..20f0dd5 --- /dev/null +++ b/Examples/csharp/reference/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -nologo -out:runme.exe + +all:: csharp + +csharp:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp_cpp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean:: + $(MAKE) -f $(TOP)/Makefile csharp_clean + +check: all diff --git a/Examples/csharp/reference/example-cs.csproj b/Examples/csharp/reference/example-cs.csproj new file mode 100644 index 0000000..e55912b --- /dev/null +++ b/Examples/csharp/reference/example-cs.csproj @@ -0,0 +1,99 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "runme" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Exe" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "runme" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;TRACE" + DocumentationFile = "" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "TRACE" + DocumentationFile = "" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "true" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References/> + </Build> + <Files> + <Include> + <File + RelPath = "example.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "examplePINVOKE.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "runme.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Vector.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "VectorArray.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/Examples/csharp/reference/example-vc.vcproj b/Examples/csharp/reference/example-vc.vcproj new file mode 100644 index 0000000..7c0b8ef --- /dev/null +++ b/Examples/csharp/reference/example-vc.vcproj @@ -0,0 +1,158 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="7.10" + Name="example-vc" + ProjectGUID="{C2302635-D489-4678-96B4-70F5309DCBE6}" + Keyword="Win32Proj"> + <Platforms> + <Platform + Name="Win32"/> + </Platforms> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + MinimalRebuild="TRUE" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="4"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="2" + GenerateDebugInformation="TRUE" + ProgramDatabaseFile="$(OutDir)/example-vc.pdb" + SubSystem="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="3"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="1" + GenerateDebugInformation="TRUE" + SubSystem="2" + OptimizeReferences="2" + EnableCOMDATFolding="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> + <File + RelativePath="example.cxx"> + </File> + <File + RelativePath="example_wrap.cxx"> + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> + <File + RelativePath="example.h"> + </File> + </Filter> + <File + RelativePath=".\example.i"> + <FileConfiguration + Name="Debug|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + <FileConfiguration + Name="Release|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + </File> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/Examples/csharp/reference/example.cxx b/Examples/csharp/reference/example.cxx new file mode 100644 index 0000000..8a513bf --- /dev/null +++ b/Examples/csharp/reference/example.cxx @@ -0,0 +1,46 @@ +/* File : example.cxx */ + +/* Deal with Microsoft's attempt at deprecating C standard runtime functions */ +#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +# define _CRT_SECURE_NO_DEPRECATE +#endif + +#include "example.h" +#include <stdio.h> +#include <stdlib.h> + +Vector operator+(const Vector &a, const Vector &b) { + Vector r; + r.x = a.x + b.x; + r.y = a.y + b.y; + r.z = a.z + b.z; + return r; +} + +char *Vector::print() { + static char temp[512]; + sprintf(temp,"Vector %p (%g,%g,%g)", this, x,y,z); + return temp; +} + +VectorArray::VectorArray(int size) { + items = new Vector[size]; + maxsize = size; +} + +VectorArray::~VectorArray() { + delete [] items; +} + +Vector &VectorArray::operator[](int index) { + if ((index < 0) || (index >= maxsize)) { + printf("Panic! Array index out of bounds.\n"); + exit(1); + } + return items[index]; +} + +int VectorArray::size() { + return maxsize; +} + diff --git a/Examples/csharp/reference/example.h b/Examples/csharp/reference/example.h new file mode 100644 index 0000000..4915adb --- /dev/null +++ b/Examples/csharp/reference/example.h @@ -0,0 +1,26 @@ +/* File : example.h */ + +class Vector { +private: + double x,y,z; +public: + Vector() : x(0), y(0), z(0) { }; + Vector(double x, double y, double z) : x(x), y(y), z(z) { }; + friend Vector operator+(const Vector &a, const Vector &b); + char *print(); +}; + +class VectorArray { +private: + Vector *items; + int maxsize; +public: + VectorArray(int maxsize); + ~VectorArray(); + Vector &operator[](int); + int size(); +}; + + + + diff --git a/Examples/csharp/reference/example.i b/Examples/csharp/reference/example.i new file mode 100644 index 0000000..6daa3b1 --- /dev/null +++ b/Examples/csharp/reference/example.i @@ -0,0 +1,46 @@ +/* File : example.i */ + +/* This file has a few "typical" uses of C++ references. */ + +%module example + +%{ +#include "example.h" +%} + +class Vector { +public: + Vector(double x, double y, double z); + ~Vector(); + char *print(); +}; + +/* This helper function calls an overloaded operator */ +%inline %{ +Vector addv(Vector &a, Vector &b) { + return a+b; +} +%} + +/* Wrapper around an array of vectors class */ + +class VectorArray { +public: + VectorArray(int maxsize); + ~VectorArray(); + int size(); + + /* This wrapper provides an alternative to the [] operator */ + %extend { + Vector &get(int index) { + return (*$self)[index]; + } + void set(int index, Vector &a) { + (*$self)[index] = a; + } + } +}; + + + + diff --git a/Examples/csharp/reference/example.sln b/Examples/csharp/reference/example.sln new file mode 100644 index 0000000..28b9851 --- /dev/null +++ b/Examples/csharp/reference/example.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example-cs", "example-cs.csproj", "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + ProjectSection(ProjectDependencies) = postProject + {C2302635-D489-4678-96B4-70F5309DCBE6} = {C2302635-D489-4678-96B4-70F5309DCBE6} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-vc", "example-vc.vcproj", "{C2302635-D489-4678-96B4-70F5309DCBE6}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.ActiveCfg = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.Build.0 = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.ActiveCfg = Release|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.Build.0 = Release|.NET + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.ActiveCfg = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.Build.0 = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.ActiveCfg = Release|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/Examples/csharp/reference/runme.cs b/Examples/csharp/reference/runme.cs new file mode 100644 index 0000000..499ddea --- /dev/null +++ b/Examples/csharp/reference/runme.cs @@ -0,0 +1,73 @@ +// This example illustrates the manipulation of C++ references in C#. + +using System; + +public class runme { + + public static void Main() + { + Console.WriteLine( "Creating some objects:" ); + Vector a = new Vector(3,4,5); + Vector b = new Vector(10,11,12); + + Console.WriteLine( " Created " + a.print() ); + Console.WriteLine( " Created " + b.print() ); + + // ----- Call an overloaded operator ----- + + // This calls the wrapper we placed around + // + // operator+(const Vector &a, const Vector &) + // + // It returns a new allocated object. + + Console.WriteLine( "Adding a+b" ); + Vector c = example.addv(a,b); + Console.WriteLine( " a+b = " + c.print() ); + + // Note: Unless we free the result, a memory leak will occur if the -noproxy commandline + // is used as the proxy classes define finalizers which call the Dispose() method. When + // -noproxy is not specified the memory management is controlled by the garbage collector. + // You can still call Dispose(). It will free the c++ memory immediately, but not the + // C# memory! You then must be careful not to call any member functions as it will + // use a NULL c pointer on the underlying c++ object. We set the C# object to null + // which will then throw a C# exception should we attempt to use it again. + c.Dispose(); + c = null; + + // ----- Create a vector array ----- + + Console.WriteLine( "Creating an array of vectors" ); + VectorArray va = new VectorArray(10); + Console.WriteLine( " va = " + va.ToString() ); + + // ----- Set some values in the array ----- + + // These operators copy the value of Vector a and Vector b to the vector array + va.set(0,a); + va.set(1,b); + + // This works, but it would cause a memory leak if -noproxy was used! + + va.set(2,example.addv(a,b)); + + + // Get some values from the array + + Console.WriteLine( "Getting some array values" ); + for (int i=0; i<5; i++) + Console.WriteLine( " va(" + i + ") = " + va.get(i).print() ); + + // Watch under resource meter to check on this + Console.WriteLine( "Making sure we don't leak memory." ); + for (int i=0; i<1000000; i++) + c = va.get(i%10); + + // ----- Clean up ----- + // This could be omitted. The garbage collector would then clean up for us. + Console.WriteLine( "Cleaning up" ); + va.Dispose(); + a.Dispose(); + b.Dispose(); + } +} diff --git a/Examples/csharp/simple/Makefile b/Examples/csharp/simple/Makefile new file mode 100644 index 0000000..2233004 --- /dev/null +++ b/Examples/csharp/simple/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SRCS = example.c +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -nologo -out:runme.exe + +all:: csharp + +csharp:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean:: + $(MAKE) -f $(TOP)/Makefile csharp_clean + +check: all diff --git a/Examples/csharp/simple/example-cs.csproj b/Examples/csharp/simple/example-cs.csproj new file mode 100644 index 0000000..d801057 --- /dev/null +++ b/Examples/csharp/simple/example-cs.csproj @@ -0,0 +1,89 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "runme" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Exe" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "runme" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;TRACE" + DocumentationFile = "" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "TRACE" + DocumentationFile = "" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "true" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References/> + </Build> + <Files> + <Include> + <File + RelPath = "example.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "examplePINVOKE.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "runme.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/Examples/csharp/simple/example-vc.vcproj b/Examples/csharp/simple/example-vc.vcproj new file mode 100644 index 0000000..14f5004 --- /dev/null +++ b/Examples/csharp/simple/example-vc.vcproj @@ -0,0 +1,222 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="8.00" + Name="example-vc" + ProjectGUID="{C2302635-D489-4678-96B4-70F5309DCBE6}" + Keyword="Win32Proj" + > + <Platforms> + <Platform + Name="Win32" + /> + </Platforms> + <ToolFiles> + </ToolFiles> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="2" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + MinimalRebuild="true" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="true" + DebugInformationFormat="4" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="2" + GenerateDebugInformation="true" + ProgramDatabaseFile="$(OutDir)/example-vc.pdb" + SubSystem="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCWebDeploymentTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="2" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="true" + DebugInformationFormat="3" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="1" + GenerateDebugInformation="true" + SubSystem="2" + OptimizeReferences="2" + EnableCOMDATFolding="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCWebDeploymentTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" + > + <File + RelativePath=".\example.c" + > + </File> + <File + RelativePath=".\example_wrap.c" + > + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}" + > + </Filter> + <File + RelativePath=".\example.i" + > + <FileConfiguration + Name="Debug|Win32" + > + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG...
echo on
..\..\..\swig.exe -csharp "$(InputPath)"
@echo off
" + Outputs="$(InputName)_wrap.c" + /> + </FileConfiguration> + <FileConfiguration + Name="Release|Win32" + > + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG...
echo on
..\..\..\swig.exe -csharp "$(InputPath)"
@echo off
" + Outputs="$(InputName)_wrap.c" + /> + </FileConfiguration> + </File> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/Examples/csharp/simple/example.c b/Examples/csharp/simple/example.c new file mode 100644 index 0000000..1c2af78 --- /dev/null +++ b/Examples/csharp/simple/example.c @@ -0,0 +1,18 @@ +/* File : example.c */ + +/* A global variable */ +double Foo = 3.0; + +/* Compute the greatest common divisor of positive integers */ +int gcd(int x, int y) { + int g; + g = y; + while (x > 0) { + g = x; + x = y % x; + y = g; + } + return g; +} + + diff --git a/Examples/csharp/simple/example.i b/Examples/csharp/simple/example.i new file mode 100644 index 0000000..24093b9 --- /dev/null +++ b/Examples/csharp/simple/example.i @@ -0,0 +1,7 @@ +/* File : example.i */ +%module example + +%inline %{ +extern int gcd(int x, int y); +extern double Foo; +%} diff --git a/Examples/csharp/simple/example.sln b/Examples/csharp/simple/example.sln new file mode 100644 index 0000000..9967f7e --- /dev/null +++ b/Examples/csharp/simple/example.sln @@ -0,0 +1,26 @@ +Microsoft Visual Studio Solution File, Format Version 9.00 +# Visual C++ Express 2005 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example-cs", "example-cs.csproj", "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + ProjectSection(ProjectDependencies) = postProject + {C2302635-D489-4678-96B4-70F5309DCBE6} = {C2302635-D489-4678-96B4-70F5309DCBE6} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-vc", "example-vc.vcproj", "{C2302635-D489-4678-96B4-70F5309DCBE6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug|Win32.ActiveCfg = Debug|Any CPU + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release|Win32.ActiveCfg = Release|Any CPU + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug|Win32.ActiveCfg = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug|Win32.Build.0 = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release|Win32.ActiveCfg = Release|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Examples/csharp/simple/runme.cs b/Examples/csharp/simple/runme.cs new file mode 100644 index 0000000..26106c6 --- /dev/null +++ b/Examples/csharp/simple/runme.cs @@ -0,0 +1,25 @@ +using System; + +public class runme +{ + static void Main() + { + // Call our gcd() function + + int x = 42; + int y = 105; + int g = example.gcd(x,y); + Console.WriteLine("The gcd of " + x + " and " + y + " is " + g); + + // Manipulate the Foo global variable + + // Output its current value + Console.WriteLine("Foo = " + example.Foo); + + // Change its value + example.Foo = 3.1415926; + + // See if the change took effect + Console.WriteLine("Foo = " + example.Foo); + } +} diff --git a/Examples/csharp/template/Makefile b/Examples/csharp/template/Makefile new file mode 100644 index 0000000..3f3bbe6 --- /dev/null +++ b/Examples/csharp/template/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -nologo -out:runme.exe + +all:: csharp + +csharp:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp_cpp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean:: + $(MAKE) -f $(TOP)/Makefile csharp_clean + +check: all diff --git a/Examples/csharp/template/example-cs.csproj b/Examples/csharp/template/example-cs.csproj new file mode 100644 index 0000000..b51122b --- /dev/null +++ b/Examples/csharp/template/example-cs.csproj @@ -0,0 +1,109 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "runme" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Exe" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "runme" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;TRACE" + DocumentationFile = "" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "TRACE" + DocumentationFile = "" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "true" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References/> + </Build> + <Files> + <Include> + <File + RelPath = "example.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "examplePINVOKE.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "runme.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "SWIGTYPE_p_double.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "SWIGTYPE_p_int.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "vecdouble.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "vecint.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/Examples/csharp/template/example-vc.vcproj b/Examples/csharp/template/example-vc.vcproj new file mode 100644 index 0000000..867e694 --- /dev/null +++ b/Examples/csharp/template/example-vc.vcproj @@ -0,0 +1,157 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="7.10" + Name="example-vc" + ProjectGUID="{C2302635-D489-4678-96B4-70F5309DCBE6}" + Keyword="Win32Proj"> + <Platforms> + <Platform + Name="Win32"/> + </Platforms> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + MinimalRebuild="TRUE" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="4"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="2" + GenerateDebugInformation="TRUE" + ProgramDatabaseFile="$(OutDir)/example-vc.pdb" + SubSystem="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="3"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="1" + GenerateDebugInformation="TRUE" + SubSystem="2" + OptimizeReferences="2" + EnableCOMDATFolding="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> + <File + RelativePath="example_wrap.cxx"> + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> + <File + RelativePath="example.h"> + </File> + </Filter> + <File + RelativePath=".\example.i"> + <FileConfiguration + Name="Debug|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off +" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + <FileConfiguration + Name="Release|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off +" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + </File> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/Examples/csharp/template/example.h b/Examples/csharp/template/example.h new file mode 100644 index 0000000..7401df6 --- /dev/null +++ b/Examples/csharp/template/example.h @@ -0,0 +1,32 @@ +/* File : example.h */ + +// Some template definitions + +template<class T> T max(T a, T b) { return a>b ? a : b; } + +template<class T> class vector { + T *v; + int sz; + public: + vector(int _sz) { + v = new T[_sz]; + sz = _sz; + } + T &get(int index) { + return v[index]; + } + void set(int index, T &val) { + v[index] = val; + } +#ifdef SWIG + %extend { + T getitem(int index) { + return $self->get(index); + } + void setitem(int index, T val) { + $self->set(index,val); + } + } +#endif +}; + diff --git a/Examples/csharp/template/example.i b/Examples/csharp/template/example.i new file mode 100644 index 0000000..8f94c4d --- /dev/null +++ b/Examples/csharp/template/example.i @@ -0,0 +1,17 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + +/* Now instantiate some specific template declarations */ + +%template(maxint) max<int>; +%template(maxdouble) max<double>; +%template(vecint) vector<int>; +%template(vecdouble) vector<double>; + diff --git a/Examples/csharp/template/example.sln b/Examples/csharp/template/example.sln new file mode 100644 index 0000000..28b9851 --- /dev/null +++ b/Examples/csharp/template/example.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example-cs", "example-cs.csproj", "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + ProjectSection(ProjectDependencies) = postProject + {C2302635-D489-4678-96B4-70F5309DCBE6} = {C2302635-D489-4678-96B4-70F5309DCBE6} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-vc", "example-vc.vcproj", "{C2302635-D489-4678-96B4-70F5309DCBE6}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.ActiveCfg = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.Build.0 = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.ActiveCfg = Release|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.Build.0 = Release|.NET + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.ActiveCfg = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.Build.0 = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.ActiveCfg = Release|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/Examples/csharp/template/runme.cs b/Examples/csharp/template/runme.cs new file mode 100644 index 0000000..b2a8e6a --- /dev/null +++ b/Examples/csharp/template/runme.cs @@ -0,0 +1,39 @@ +// This example illustrates how C++ templates can be used from C#. + +using System; + +public class runme { + + public static void Main() + { + // Call some templated functions + Console.WriteLine(example.maxint(3,7)); + Console.WriteLine(example.maxdouble(3.14,2.18)); + + // Create some class + + vecint iv = new vecint(100); + vecdouble dv = new vecdouble(1000); + + for (int i=0; i<100; i++) + iv.setitem(i,2*i); + + for (int i=0; i<1000; i++) + dv.setitem(i, 1.0/(i+1)); + + { + int sum = 0; + for (int i=0; i<100; i++) + sum = sum + iv.getitem(i); + + Console.WriteLine(sum); + } + + { + double sum = 0.0; + for (int i=0; i<1000; i++) + sum = sum + dv.getitem(i); + Console.WriteLine(sum); + } + } +} diff --git a/Examples/csharp/variables/Makefile b/Examples/csharp/variables/Makefile new file mode 100644 index 0000000..2233004 --- /dev/null +++ b/Examples/csharp/variables/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SRCS = example.c +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -nologo -out:runme.exe + +all:: csharp + +csharp:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean:: + $(MAKE) -f $(TOP)/Makefile csharp_clean + +check: all diff --git a/Examples/csharp/variables/example-cs.csproj b/Examples/csharp/variables/example-cs.csproj new file mode 100644 index 0000000..f0e0159 --- /dev/null +++ b/Examples/csharp/variables/example-cs.csproj @@ -0,0 +1,99 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "runme" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Exe" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "runme" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;TRACE" + DocumentationFile = "" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "TRACE" + DocumentationFile = "" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "true" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References/> + </Build> + <Files> + <Include> + <File + RelPath = "example.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "examplePINVOKE.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "runme.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "SWIGTYPE_p_int.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "SWIGTYPE_p_Point.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/Examples/csharp/variables/example-vc.vcproj b/Examples/csharp/variables/example-vc.vcproj new file mode 100644 index 0000000..be0dfc7 --- /dev/null +++ b/Examples/csharp/variables/example-vc.vcproj @@ -0,0 +1,157 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="7.10" + Name="example-vc" + ProjectGUID="{C2302635-D489-4678-96B4-70F5309DCBE6}" + Keyword="Win32Proj"> + <Platforms> + <Platform + Name="Win32"/> + </Platforms> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + MinimalRebuild="TRUE" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="4"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="2" + GenerateDebugInformation="TRUE" + ProgramDatabaseFile="$(OutDir)/example-vc.pdb" + SubSystem="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="3"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="1" + GenerateDebugInformation="TRUE" + SubSystem="2" + OptimizeReferences="2" + EnableCOMDATFolding="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> + <File + RelativePath=".\example.c"> + </File> + <File + RelativePath=".\example_wrap.c"> + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> + </Filter> + <File + RelativePath=".\example.i"> + <FileConfiguration + Name="Debug|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -csharp "$(InputPath)" +@echo off +" + Outputs="$(InputName)_wrap.c"/> + </FileConfiguration> + <FileConfiguration + Name="Release|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -csharp "$(InputPath)" +@echo off +" + Outputs="$(InputName)_wrap.c"/> + </FileConfiguration> + </File> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/Examples/csharp/variables/example.c b/Examples/csharp/variables/example.c new file mode 100644 index 0000000..aa4ffe9 --- /dev/null +++ b/Examples/csharp/variables/example.c @@ -0,0 +1,91 @@ +/* File : example.c */ + +/* I'm a file containing some C global variables */ + +/* Deal with Microsoft's attempt at deprecating C standard runtime functions */ +#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +# define _CRT_SECURE_NO_DEPRECATE +#endif + +#include <stdio.h> +#include <stdlib.h> +#include "example.h" + +int ivar = 0; +short svar = 0; +long lvar = 0; +unsigned int uivar = 0; +unsigned short usvar = 0; +unsigned long ulvar = 0; +signed char scvar = 0; +unsigned char ucvar = 0; +char cvar = 0; +float fvar = 0; +double dvar = 0; +char *strvar = 0; +const char cstrvar[] = "Goodbye"; +int *iptrvar = 0; +char name[256] = "Dave"; +char path[256] = "/home/beazley"; + + +/* Global variables involving a structure */ +Point *ptptr = 0; +Point pt = { 10, 20 }; + +/* A variable that we will make read-only in the interface */ +int status = 1; + +/* A debugging function to print out their values */ + +void print_vars() { + printf("ivar = %d\n", ivar); + printf("svar = %d\n", svar); + printf("lvar = %ld\n", lvar); + printf("uivar = %u\n", uivar); + printf("usvar = %u\n", usvar); + printf("ulvar = %lu\n", ulvar); + printf("scvar = %d\n", scvar); + printf("ucvar = %u\n", ucvar); + printf("fvar = %g\n", fvar); + printf("dvar = %g\n", dvar); + printf("cvar = %c\n", cvar); + printf("strvar = %s\n", strvar ? strvar : "(null)"); + printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)"); + printf("iptrvar = %p\n", iptrvar); + printf("name = %s\n", name); + printf("ptptr = %p (%d, %d)\n", ptptr, ptptr ? ptptr->x : 0, ptptr ? ptptr->y : 0); + printf("pt = (%d, %d)\n", pt.x, pt.y); + printf("status = %d\n", status); +} + +/* A function to create an integer (to test iptrvar) */ + +int *new_int(int value) { + int *ip = (int *) malloc(sizeof(int)); + *ip = value; + return ip; +} + +/* A function to create a point */ + +Point *new_Point(int x, int y) { + Point *p = (Point *) malloc(sizeof(Point)); + p->x = x; + p->y = y; + return p; +} + +char * Point_print(Point *p) { + static char buffer[256]; + if (p) { + sprintf(buffer,"(%d,%d)", p->x,p->y); + } else { + sprintf(buffer,"null"); + } + return buffer; +} + +void pt_print() { + printf("(%d, %d)\n", pt.x, pt.y); +} diff --git a/Examples/csharp/variables/example.h b/Examples/csharp/variables/example.h new file mode 100644 index 0000000..0f7e895 --- /dev/null +++ b/Examples/csharp/variables/example.h @@ -0,0 +1,6 @@ +/* File: example.h */ + +typedef struct { + int x,y; +} Point; + diff --git a/Examples/csharp/variables/example.i b/Examples/csharp/variables/example.i new file mode 100644 index 0000000..591b871 --- /dev/null +++ b/Examples/csharp/variables/example.i @@ -0,0 +1,49 @@ +/* File : example.i */ +%module example +%{ +#include "example.h" +%} + +/* Some global variable declarations */ +%inline %{ +extern int ivar; +extern short svar; +extern long lvar; +extern unsigned int uivar; +extern unsigned short usvar; +extern unsigned long ulvar; +extern signed char scvar; +extern unsigned char ucvar; +extern char cvar; +extern float fvar; +extern double dvar; +extern char *strvar; +extern const char cstrvar[]; +extern int *iptrvar; +extern char name[256]; + +extern Point *ptptr; +extern Point pt; +%} + + +/* Some read-only variables */ + +%immutable; + +%inline %{ +extern int status; +extern char path[256]; +%} + +%mutable; + +/* Some helper functions to make it easier to test */ +%inline %{ +extern void print_vars(); +extern int *new_int(int value); +extern Point *new_Point(int x, int y); +extern char *Point_print(Point *p); +extern void pt_print(); +%} + diff --git a/Examples/csharp/variables/example.sln b/Examples/csharp/variables/example.sln new file mode 100644 index 0000000..28b9851 --- /dev/null +++ b/Examples/csharp/variables/example.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example-cs", "example-cs.csproj", "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + ProjectSection(ProjectDependencies) = postProject + {C2302635-D489-4678-96B4-70F5309DCBE6} = {C2302635-D489-4678-96B4-70F5309DCBE6} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-vc", "example-vc.vcproj", "{C2302635-D489-4678-96B4-70F5309DCBE6}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.ActiveCfg = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.Build.0 = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.ActiveCfg = Release|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.Build.0 = Release|.NET + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.ActiveCfg = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.Build.0 = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.ActiveCfg = Release|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/Examples/csharp/variables/runme.cs b/Examples/csharp/variables/runme.cs new file mode 100644 index 0000000..3d19b6a --- /dev/null +++ b/Examples/csharp/variables/runme.cs @@ -0,0 +1,82 @@ +// This example illustrates global variable access from C#. + +using System; +using System.Reflection; + +public class runme { + + public static void Main() { + + // Try to set the values of some global variables + + example.ivar = 42; + example.svar = -31000; + example.lvar = 65537; + example.uivar = 123456; + example.usvar = 61000; + example.ulvar = 654321; + example.scvar = -13; + example.ucvar = 251; + example.cvar = 'S'; + example.fvar = (float)3.14159; + example.dvar = 2.1828; + example.strvar = "Hello World"; + example.iptrvar= example.new_int(37); + example.ptptr = example.new_Point(37,42); + example.name = "Bill"; + + // Now print out the values of the variables + + Console.WriteLine( "Variables (values printed from C#)" ); + + Console.WriteLine( "ivar =" + example.ivar ); + Console.WriteLine( "svar =" + example.svar ); + Console.WriteLine( "lvar =" + example.lvar ); + Console.WriteLine( "uivar =" + example.uivar ); + Console.WriteLine( "usvar =" + example.usvar ); + Console.WriteLine( "ulvar =" + example.ulvar ); + Console.WriteLine( "scvar =" + example.scvar ); + Console.WriteLine( "ucvar =" + example.ucvar ); + Console.WriteLine( "fvar =" + example.fvar ); + Console.WriteLine( "dvar =" + example.dvar ); + Console.WriteLine( "cvar =" + example.cvar ); + Console.WriteLine( "strvar =" + example.strvar ); + Console.WriteLine( "cstrvar =" + example.cstrvar ); + Console.WriteLine( "iptrvar =" + example.iptrvar ); + Console.WriteLine( "name =" + example.name ); + Console.WriteLine( "ptptr =" + example.ptptr + example.Point_print(example.ptptr) ); + Console.WriteLine( "pt =" + example.pt + example.Point_print(example.pt) ); + + Console.WriteLine( "\nVariables (values printed from C)" ); + + example.print_vars(); + + Console.WriteLine( "\nNow I'm going to try and modify some read only variables" ); + Console.WriteLine( "\nChecking that the read only variables are readonly..." ); + + example ex = new example(); + Type t = ex.GetType(); + + Console.WriteLine( " 'path'" ); + PropertyInfo pi = t.GetProperty("path"); + if (pi.CanWrite) + Console.WriteLine("Oh dear this variable is not read only\n"); + else + Console.WriteLine("Good."); + + Console.WriteLine( " 'status'" ); + pi = t.GetProperty("status"); + if (pi.CanWrite) + Console.WriteLine("Oh dear this variable is not read only"); + else + Console.WriteLine("Good."); + + Console.WriteLine( "\nI'm going to try and update a structure variable.\n" ); + + example.pt = example.ptptr; + + Console.WriteLine( "The new value is" ); + example.pt_print(); + Console.WriteLine( "You should see the value" + example.Point_print(example.ptptr) ); + } +} |