|
|
我用C#重写的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using Microsoft.VisualBasic.CompilerServices;
using System.IO;
using System.Runtime.InteropServices;
namespace WpfApplication4
{
public class TMsSap: IDisposable
{
private string fServer;
private string fSystem;
private string fSystemNumber;
private string fUser;
private string fPassword;
private string fClient;
private dynamic fLogonControl;
private dynamic fFunctions;
public static dynamic CreateObject(string ProgId)
{
if (ProgId.Length == 0)
{
throw new Exception("ProgId.Length == 0");
}
return Activator.CreateInstance(Type.GetTypeFromProgID(ProgId));
}
private static object LateGetValue(object obj, string propertyName)
{
return RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(obj, null, propertyName, new object[0], null, null, null));
}
private static void LateSetValue(object obj, string propertyName, object value)
{
NewLateBinding.LateSet(obj, null, propertyName, new[] { value }, null, null);
}
private static void LateCallMethod(object obj, string methodName)
{
NewLateBinding.LateCall(obj, null, methodName, new object[0], null, null, null, true);
}
/// <summary>
/// Release a COM object so that it can be cleaned up.
/// </summary>
/// <param name="comObj"></param>
protected void ReleaseCom(dynamic comObj)
{
Marshal.ReleaseComObject(comObj);
}
/// <summary>
/// Clean up all released COM objects.
/// </summary>
protected void CleanAllReleased()
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
public TMsSap()
{
fLogonControl = CreateObject("SAP.LogonControl.1");
fFunctions = CreateObject("SAP.Functions");
}
~TMsSap()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
// tell the GC not to finalize
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (fLogonControl != null) // only dispose once!
{
if (disposing)
{
Console.WriteLine("Not in destructor, OK to reference other objects");
}
// perform cleanup for this object
Console.WriteLine("Disposing...");
ReleaseCom(fLogonControl);
fLogonControl = null;
ReleaseCom(fFunctions);
fFunctions = null;
}
}
public dynamic getFunction(string aName)
{
object[] varArrayArgument = new object[] { aName };
return RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(fFunctions, null, "add", varArrayArgument, null, null, null));
}
public Boolean getNewConnection()
{
dynamic varConnection = LateGetValue(fLogonControl, "newConnection");
LateSetValue(fFunctions, "Connection", varConnection);
varConnection.ApplicationServer = fServer;
varConnection.User = fUser;
varConnection.Password = fPassword;
varConnection.System = fSystem;
varConnection.Client = fClient;
varConnection.SystemNumber = fSystemNumber;
varConnection.Language = "";
return varConnection.LogOn(0, true);
}
public string getProperFileName(string aFileName)
{
return aFileName.Replace('/', '_').Replace(':', '_').Replace('>', '_').Replace('<', '_').Replace('%', '_').Replace('?', '_').Replace('$', '_');
}
public string Server
{
get
{
return fServer;
}
set
{
fServer = value;
}
}
public string System
{
get
{
return fSystem;
}
set
{
fSystem = value;
}
}
public string SystemNumber
{
get
{
return fSystemNumber;
}
set
{
fSystemNumber = value;
}
}
public string User
{
get
{
return fUser;
}
set
{
fUser = value;
}
}
public string Password
{
get
{
return fPassword;
}
set
{
fPassword = value;
}
}
public string Client
{
get
{
return fClient;
}
set
{
fClient = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.CompilerServices;
using Microsoft.VisualBasic.CompilerServices;
using System.IO;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
private Boolean fCancel;
private string fReportFileStoredDirectory;
public MainWindow()
{
InitializeComponent();
}
private void buttonStop_Click(object sender, RoutedEventArgs e)
{
fCancel = true;
}
private void processReport(TMsSap aSap, string aReportName, dynamic aFunctionReadTable)
{
string sFilePath = fReportFileStoredDirectory + aSap.getProperFileName(aReportName) + ".txt";
if (File.Exists(sFilePath))
{
return;
}
bool bCall;
aFunctionReadTable.exports("PROGRAM").value = aReportName;
try
{
bCall = aFunctionReadTable.call();
}
catch (Exception exception1)
{
listBox.Items.Add(aReportName + " " + exception1.Message);
aSap.getNewConnection();
return;
}
if (!bCall)
{
listBox.Items.Add(aReportName + " " + aFunctionReadTable.exception);
aSap.getNewConnection();
}
listBox.Items.Add(aReportName);
dynamic varTableSourceCode = aFunctionReadTable.tables.item("QTAB");
StreamWriter varStreamWriter = File.CreateText(sFilePath);
int iRowCount = varTableSourceCode.rowcount;
varStreamWriter.WriteLine("* " + aReportName);
for (int iIndex = 1; iIndex <= iRowCount; iIndex++)
{
varStreamWriter.WriteLine(varTableSourceCode.value(iIndex, 1));
}
varStreamWriter.Close();
}
private void buttonRun_Click(object sender, RoutedEventArgs e)
{
fReportFileStoredDirectory = textBoxReportFileStoredDirectory.Text;
fCancel = false;
buttonRun.IsEnabled = true;
buttonStop.IsEnabled = true;
try
{
using (TMsSap varSap = new TMsSap())
{
varSap.Server = textBoxServer.Text;
varSap.System = textBoxSystem.Text;
varSap.Client = textBoxClient.Text;
varSap.SystemNumber = textBoxSystemNumber.Text;
varSap.User = textBoxUser.Text;
varSap.Password = passwordBox.Password;
if (!varSap.getNewConnection())
{
MessageBox.Show("Can not get sap connection.");
}
else
{
dynamic varFunctionReadTable = varSap.getFunction("RFC_READ_REPORT");
StreamReader varStreamReader = File.OpenText(textBoxDatabaseTableFileNameListPath.Text);
while (varStreamReader.Peek() != -1)
{
string sReportName = varStreamReader.ReadLine();
processReport(varSap, sReportName, varFunctionReadTable);
}
varStreamReader.Close();
}
}
}
finally
{
buttonRun.IsEnabled = true;
buttonStop.IsEnabled = false;
MessageBox.Show("Done");
}
}
}
}
[ 本帖最后由 DDD888 于 2-8-2010 02:10 编辑 ] |
|