简体   繁体   中英

Using an ActiveX in WPF without UI

I have been looking at Stackoverflow thread ActiveX control without a form and after some success I have hit a wall.

I seem to be able to create an instance of my AX, but I am not sure how to access the thing to call methods. Here is my implementation:

class DirectAudio : UserControl, IDisposable
{
private myAxControl myAx;
private Thread axThread;

public DirectAudio()
{
    BuildAx();
}

private void BuildAx()
{
    axThread = new Thread((ThreadStart) delegate
    {
        myAx = new PanasonicOCXControl();
        myAx.CreateControl();
        System.Windows.Forms.Application.Run();
    });
    axThread.SetApartmentState((ApartmentState.STA));
    axThread.IsBackground = true;
    axThread.Start();
}

public void Dispose()
{
    axThread.Abort();
    aX.Dispose();
}

public void Connect(CameraConfig cameraConfig)
{
    try
    {
        myAx.Invoke(new Action(() => myAx.Disconnect()));  //how do I do this??
    }
    catch (Exception ex)
    {
        Logger.ToDebug(ex);
    }            
}
}

When that myAx.Invoke in the Connect() method runs I get this:

{"Invoke or BeginInvoke cannot be called on a control until the window handle has been created."}

How should I be calling myAx?

If you want to use ActiveX without UI directly you can try this

[DllImport("ole32.dll", PreserveSig = false)]
[
 return :MarshalAs(UnmanagedType.Interface)
]
public static extern object CoCreateInstance([In] ref Guid clsid, [MarshalAs(UnmanagedType.Interface)] object punkOuter, int context, [In] ref Guid iid);

public object createComOCXObject() {
 Guid IID_IUnknown = new Guid("{00000000-0000-0000-C000-000000000046}");

 var clsid = new Guid("{6bf52a52-394a-11d3-b153-00c04f79faa6}"); //your ocx clsid 

 object ocxObj = CoCreateInstance(ref clsid, (object) null, 1, ref IID_IUnknown);
 return ocxObj;
}

You can refer my answer in this question see link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM