轉自http://blog.csdn.net/wren2004/archive/2009/01/22/3849688.aspx
昨天講述了以下如何在VS2005中使用form的形式來調用SAP ECC6.0中的Function Module,今天將記錄一下在VS2005中以web的形式來調用Function module.
以web形式對function module進行調用與form形式基本一樣,唯一值得注意的地方就是:"An ActiveX control must be run in an STA apartment. Because the attribute STAThread is applied to the Main method of a WinForm class by default with a WinForms Project, the main thread of your app will run in STA mode.".也就是說有些AcrtiveX控制項或者Com組件必須運行在單一執行緒 Apartment下(STA:Single Thread Apartment ),否則的話會拋出“Bad variant type”異常。解決方案為:新開一個線程,並將該線程的運行模式設定為STA,然後再改線程下對Com組件或者ActiveX控制項進行調用。
對應到我們的案例中,如果不使用STA模式運行,我們可以串連到SAP系統,但調用Function Module的時候會拋出“Bad variant type”異常。所以要講調用Function Module的代碼在新開的線程中執行。具體步驟如下:
一,添加對Interop.SAPFunctionsOCX.dll以及Interop.SAPLogonCtrl.dll和Interop.SAPTableFactoryCtrl.dllcom組件的引用。
二,新開一個線程,並將該線程的運行模式設定為STA. 並將登入SAP系統以及調用Function module的方法運行在該線程下!代碼如下:
protected void Button1_Click(object sender, EventArgs e)
{
System.Threading.Thread s = new System.Threading.Thread(new System.Threading.ThreadStart(test)); //Create a new thread and set the method test() run in this thread
s.SetApartmentState(System.Threading.ApartmentState.STA); //Set the run mode 'STA'
s.Start(); //Start the thread
s.Join(); //Wait until thread run OK.
GridView1.DataSource = dt;
GridView1.DataBind();
msg.Text = "Get Data from 'ENQUEUE_READ' OK!";
}
private void test()
{
SAPLogonCtrl.SAPLogonControlClass login = new SAPLogonCtrl.SAPLogonControlClass();
login.ApplicationServer = "";
login.Client = "";
login.Language = "EN";
login.User = username.Text;
login.Password = Psw.Text;
login.SystemNumber = 00;
SAPLogonCtrl.Connection conn = (SAPLogonCtrl.Connection)login.NewConnection();
if (conn.Logon(0, true))
{
SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass();
func.Connection = conn;
SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add("ENQUEUE_READ");
SAPFunctionsOCX.IParameter gclient = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("GCLIENT");
gclient.Value = "301";
SAPFunctionsOCX.IParameter GUNAME = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("GUNAME");
GUNAME.Value = "";
SAPFunctionsOCX.IParameter LOCAL = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("LOCAL");
LOCAL.Value = "0";
ifunc.Call();
SAPTableFactoryCtrl.Tables tables = (SAPTableFactoryCtrl.Tables)ifunc.Tables;
SAPTableFactoryCtrl.Table ENQ = (SAPTableFactoryCtrl.Table)tables.get_Item("ENQ");
int n = ENQ.RowCount;
dt = GetTable();
for (int i = 1; i <= n; i++)
{
DataRow dr = dt.NewRow();
dr["GNAME"] = ENQ.get_Cell(i, "GNAME").ToString();
dr["GUNAME"] = ENQ.get_Cell(i, "GUNAME").ToString();
dr["GARG"] = ENQ.get_Cell(i, "GARG").ToString();
dr["GOBJ"] = ENQ.get_Cell(i, "GOBJ").ToString();
dr["GTDATE"] = ENQ.get_Cell(i, "GTDATE").ToString();
dt.Rows.Add(dr);
}
}
}
通過以上的設定就可以在Web中調用Function Module了。此方式已經實驗成功。園子裡面另外一位仁兄弟到了另外一種方法http://www.cnblogs.com/jirigala/archive/2008/11/26/1341345.html 但是該方法我沒有實驗成功,不知道是我哪設定出問題了還是怎麼回事,如果有知情者請告知,謝謝!
對上文的補充,
1、注意如果SAP的版本不是Unicode模式,不要使用Unicode格式的dll
運行:
regsvr32 "C:\Documents and Settings\wang.binbin\Desktop\GUI\WINDOWS\WIN32\system\SAP\wdtlog.ocx"
2、如果遭遇SAP Data Type NOT supported錯誤,請參照http://msdn.microsoft.com/en-us/library/cc185537(BTS.10).aspx,並非所有的類型都支援RFC,如SAP中的String
輸入一個表:
ptInHeader.AppendGridData(rowCount, ((SAPTableFactoryCtrl.Column)cols.get_Item("REFNO")).Index, 1, rw["ACCEPT_NO"].ToString());//
在remote RFC中使用BDC需要注意call transaction的mode,不能使用mode 'E',否則在外部系統調用此RFC時,如果RFC中的BDC有錯誤的話,就會產生Runtime error DYNPRO_SEND_IN_BACKGROUND has occurred的錯誤。因為BDC出現錯誤時,mode 'E'會轉到錯誤畫面,而這個無法傳遞到外部系統顯示。
在remote RFC中使用BDC時,mode應使用'N',而不能使用'E'。
中文亂碼問題:在系統變數中添加SAP_CODEPAGE 8400
登陸的language使用ZH
如果是SAP模式是Unicode模式,需要將登陸模式也修改為Unicode,否則會出現中文亂碼問題