COM-Interop mit C♯
- Zugriff auf COM -Objekte mit C♯ 4.0
Die COM -Objekte können ähnlich wie in VisualBasic verwendet werden, da der nötige Verbindungs-Code von der C♯ -Implementation erzeugt wird.
Eine Referenz auf die verwendeten Office -COM -Objekte »Microsoft.Office.Interop.Excel.dll« muß zum Projekt hinzugefügt werden (man suche nach »Interop.Excel« im Dateisystem).
using Xl = Microsoft.Office.Interop.Excel;
public static class Program
{ public static void Main()
{ var excel = new Xl.Application();
excel.Visible = true;
dynamic workbook = excel.Workbooks.Add();
Xl.Worksheet worksheet = workbook.ActiveSheet;
workbook.SaveAs
( "example.xls",
AccessMode: Xl.XlSaveAsAccessMode.xlShared ); }}
Bei Verwendung von Excel 2000, erst die Referenz so erzeugen:
"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\x64\TlbImp.exe" "C:\Program Files (x86)\Microsoft Office\Office\EXCEL9.OLB"
Dies schreibt die Datei »Excel.dll«, die dann mit »/reference:Excel.dll« referenziert werden kann.
Dann ist das folgende Programm zu verwenden:
/*Office 2000*/ using xl = Excel; //using Microsoft.Office.Interop.Excel; // Office XP version //using Excel; // Office 97/2000 version public static class Program
{ public static void Main()
{ var excel = new xl.Application();
excel.Visible = true;
dynamic workbook = excel.Workbooks.Add();
Excel.Worksheet worksheet = workbook.ActiveSheet;
workbook.SaveAs
( "example.xls",
AccessMode: Excel.XlSaveAsAccessMode.xlShared ); }}
You can download the Office XP PIAs from the Microsoft Downloads Web site, put them into the global assembly cache (GAC), and register them in the registry.
- Neuerungen in C♯ 4.0
Vor Version 4.0 von C♯
using Word = Microsoft.Office.Interop.Word; public static class Program
{ public static void Main()
{ Word.Application wordApplication =
new Word.Application(){ Visible = true }; object missingValue = System.Reflection.Missing.Value; object readOnlyValue = true; object fileName = "example.doc"; wordApplication.Documents.Open
( ref fileName,
ref missingValue,
ref readOnlyValue,
ref missingValue,
ref missingValue,
ref missingValue,
ref missingValue,
ref missingValue,
ref missingValue,
ref missingValue,
ref missingValue,
ref missingValue,
ref missingValue,
ref missingValue,
ref missingValue ); }}
Seit Version 4.0 von C♯
using Word = Microsoft.Office.Interop.Word; class Program
{ static void Main()
{ Word.Application wordApplication = new
Word.Application() {Visible = true}; wordApplication.Documents.Open
( "example.doc", ReadOnly: true ); }}
C♯ 4.0:
- Es gibt optionale Argumente.
- Es gibt benannte Argumente.
- Das Schlüsselwort »ref« ist hier nicht mehr nötig – temporäre Speicher werden stillschweigend generiert und rechtzeitig wieder freigegeben.
- PIAs (Primary Interop Assemblies) werden nicht mehr benötigt, wodurch die Programme kleiner und schneller werden.
- Der Ausdruckstyp »dynamic« ermöglicht den Verzicht auf Cast-Operatoren.
Vor Version 4.0 von C♯
( ( Excel.Range )range.Cells[ 5, 5 ]).Value = "alpha";
Seit Version 4.0 von C♯
excelObj.Cells[ 5, 5 ].Value = "alpha";
- Datenbankzugriffe
/R:System.Data.DLL
public static class Program
{ public static void Main()
{ System.Data.OleDb.OleDbConnection con =
new System.Data.OleDb.OleDbConnection
( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=O:\\test.mdb" ); System.Data.OleDb.OleDbCommand command =
new System.Data.OleDb.OleDbCommand( "select * from test", con ); con.Open(); System.Data.OleDb.OleDbDataReader reader = command.ExecuteReader(); while( reader.Read() )
{ System.Console.WriteLine( reader[ 0 ].ToString() );
System.Console.WriteLine( reader[ 1 ].ToString() );
System.Console.WriteLine( reader[ 2 ].ToString() ); } con.Close(); }}- Direkte Zugriffe auf die Windows-API, Bibliotheken (DLLs) verwenden
using System;
using System.Runtime.InteropServices; // DllImport
public static class clsAPI
{
[DllImport("winmm.dll")]
public static extern long PlaySound( string lpszName, long hModule, long dwFlags );
[STAThread] // Single-Threaded Apartment model, serialize calls
public static void Main()
{ long retval;
System.String fname = "C:\\WINDOWS\\Media\\chimes.wav";
retval = PlaySound( fname, 0, 1 ); }}- Interoperabilität mit C++
»Math.cpp«
namespace Test
{ class Math
{ public:
static int Add(int a, int b) { return a + b; }
static int Subtract(int a, int b) { return a - b; }}; }
»Program.cs«
using System;
using Test; class Program
{ static void Main()
{ int a = 30;
int b = 10;
int Result1 = Test.Math.Add( a, b );
Console.WriteLine( Result1 );
int Result2 = Test.Math.Subtract( a, b );
Console.WriteLine( Result2 ); }}