我一直在关注Stackoverflow线程ActiveX control without a form,并且在取得一些成功后,我遇到了障碍。
我似乎能够创建我的AX实例,但我不知道如何访问调用方法的东西。这是我的实施:
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);
}
}
}
当Connect()方法中的myAx.Invoke运行时,我得到了这个:
{"Invoke or BeginInvoke cannot be called on a control until the window handle has been created."}
我该怎么称呼myAx?
答案 0 :(得分:0)
如果您想直接在不使用UI的情况下使用ActiveX,则可以尝试
[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;
}
您可以在这个问题see link中引用我的答案