Question

This is a follow up to a previous question. I'm trying to convert some Vb.net code to C#. A com object is created (atlDirectorObject.atlDirector) and it is used to create another com object (atl3270Tool) by parameter. atl3270Tool is not getting created in the C# version. Am I going the wrong route trying to reference atl3270Tool through an object array?

'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<<<<<<<<<
Was it helpful?

Solution

Hans is correct. It is best to do this in vb.net. But to those determined to do do this in C#, here's your solution

    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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top