0

这是对上一个问题的跟进。我正在尝试将一些 Vb.net 代码转换为 C#。创建一个 com 对象 (atlDirectorObject.atlDirector) 并用于通过参数创建另一个 com 对象 (atl3270Tool)。atl3270Tool 未在 C# 版本中创建。尝试通过对象数组引用 atl3270Tool 是否走错了路?

'working vb code
Dim atl3270Tool
Dim ErrMsg As String
Dim atlDirectorObject = CreateObject("atlDirectorObject.atlDirector")
atlDirectorObject.CreateTool("3270", 1, True, True, 0, atl3270Tool, ErrMsg)
'atl3270Tool is working com object at this point = success

//non-working c# code
object atl3270Tool = null;
string ErrMsg = null;
object atlDirectorObject = Activator.CreateInstance(Type.GetTypeFromProgID("atlDirectorObject.atlDirector"));
//atlDirectorObject is a com object now
//attempt to reference atl3270Tool inside an object array
object[] p = { "3270", 1, true, true, 0, atl3270Tool, ErrMsg };
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(atlDirectorObject, null, "CreateTool", p, null, null, null, false);
//>>>>>>>>atl3270Tool is still null at this point<<<<<<<<<
4

1 回答 1

0

汉斯是对的。最好在 vb.net 中执行此操作。但是对于那些决心在 C# 中执行此操作的人,这是您的解决方案

    object atl3270Tool = null, ErrMsg = null;
    object atlDirectorObject = Activator.CreateInstance(Type.GetTypeFromProgID("atlDirectorObject.atlDirector"));
    object[] p = { "3270", 1, true, true, 0, atl3270Tool, ErrMsg };
    object[] p2 = { "xxxx", "", "xxxxxxxxxx", ErrMsg };
    Boolean[] cb = new Boolean[7];
    cb[5] = true; //set array index of atl3270Tool to true
    Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(atlDirectorObject, atlDirectorObject.GetType(), "CreateTool", p, null, null, cb, false);
    atl3270Tool = p[5];
    Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(atl3270Tool, atl3270Tool.GetType(), "ShowScreen", p2, null, null, null, false);
// add code to release com objects
于 2012-06-04T19:18:08.363 回答