0

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<<<<<<<<<
Kara
  • 6,115
  • 16
  • 50
  • 57
Theo
  • 81
  • 9
  • Yes, you are doing it wrong. Use atl3270Tool = p[5] *after* the call. And you have to use the CopyBack argument. When I advised to use vb.net to write this code I certainly didn't mean *that*. – Hans Passant Jun 04 '12 at 18:03

1 Answers1

0

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
Theo
  • 81
  • 9
  • Ok I'm having more trouble. I'm trying to use the newlatebinding.latecall method with copy back to use the "atl3270Tool" (same object that i created with to begin with) to grab some data from a parameter. When I put the copy back = true on the 2 parameters I want it fails ("Invalid callee. (Exception from HRESULT: 0x80020010 (DISP_E_BADCALLEE))"). It doesn't error out if I put true on one of the other 3 parameters. Hans, if you aren't too frustrated with me insisting on doing this in C#. I'd appreciate some direction. – Theo Jun 15 '12 at 19:00
  • ScreenText should be null, not string.empty – Theo Jun 15 '12 at 22:07