调用activex方法

call activex methods

本文关键字:方法 activex 调用      更新时间:2023-10-16

我有一个ocx文件,我想在win32应用程序中使用它,所以我从change USE of MFCUSE MFC in a Static Library,然后这样进行:

#import "C:\Program Files\GeoSDK\LiveX_8300.ocx"
int CALLBACK WinMain(
   HINSTANCE hInstance,
   HINSTANCE hPrevInstance,
   LPSTR lpCmdLine,
   int nCmdShow)
{
   // Register our Window class
   WNDCLASS wndclass;
   wndclass.style = CS_VREDRAW | CS_HREDRAW;
   wndclass.lpfnWndProc = &WindowProc;
   wndclass.cbClsExtra = 0;
   wndclass.cbWndExtra = 0;
   wndclass.hInstance = hInstance;
   wndclass.hIcon = NULL;
   wndclass.hCursor = NULL;
   wndclass.hbrBackground = reinterpret_cast <HBRUSH> (COLOR_BTNFACE + 1);
   wndclass.lpszMenuName = NULL;
   wndclass.lpszClassName =windowClassName;
   ::RegisterClass(&wndclass);
   // Create our main, raw win32 API window
   // We create the window invisible (meaning that we do not provide WS_VISIBLE as the window style parameter), because making it visible and then
   // adding a HwndSource will make it flicker.
   HWND mainWindow = ::CreateWindow(
      windowClassName,
      windowTitle,
      0,
      CW_USEDEFAULT,
      CW_USEDEFAULT,
      windowWidth,
      windowHeight,
      NULL,
      NULL,
      hInstance,
      0);
   ::ShowWindow (mainWindow, nCmdShow);
   ::UpdateWindow( mainWindow );

//ocx
   //_DLiveX p;
    typedef HRESULT (WINAPI *PFonc)(IUnknown*, HWND,IUnknown**);   
    HINSTANCE hDLL2 = ::LoadLibrary(TEXT("atl.dll"));   
    if (!hDLL2) 
      return 1;   
    PFonc AtlAxAttachControl = (PFonc) ::GetProcAddress(hDLL2,"AtlAxAttachControl");
    CLSID clsid = GetClsid();
    IID DIID__DLiveX = GetDIID__DLiveX();
    RECT rect;
    ::GetClientRect(mainWindow,&rect);
    container = ::CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT","",WS_CHILD | WS_VISIBLE,100,100,rect.right,rect.bottom,mainWindow,0,hInstance,0);
AtlAxWinInit();
    HRESULT hr = ::CoInitialize(NULL);
    LIVEXLib::_DLiveX *p;
    hr = CoCreateInstance(clsid,
                    0,
                    CLSCTX_ALL,
                    DIID__DLiveX,
                    reinterpret_cast<void**>(&p)) ;

    if(FAILED(AtlAxAttachControl((IUnknown*)p, container, 0))){
                    p->Release();
                    return -1;
        MessageBox(HWND_DESKTOP, "FAILED(AtlAxAttachControl(pitd, container, NULL))", "Error", MB_ICONERROR | MB_OK);
                }
    while(hr == S_OK)
        p->CreateX();

当我出色地完成我的程序时,我得到了连接到wind的activex,但当我调用任何方法时,我都得到了这个错误Unhandled exception at 0x752a812f in test1.exe: Microsoft C++ exception: _com_error at memory location 0x0012fdbc..我知道我应该调用SetProperty方法或类似的方法,但我不知道是如何

由于使用MFC,请使用COleControl。MFC的全部目的是提供类,这样您就不必自己做所有事情。