c++winapicomacrobat-sdk

Can't load PDF in window


I'm trying to load PDF into window. I'm using Adobe Acrobat activeX and WinApi for this. I was able "insert" a web browser in this code, but I can't with PDF. While execution I get error:

"Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention."

Seems there is a problem in LoadFile prototype, but from ITypeLibViewer I get:

[id(0x00000002), helpstring("method LoadFile")]
VARIANT_BOOL LoadFile([in] BSTR fileName);

so everything seems ok. Where is a problem?

UPD1: passing fileName now done like this:

BSTR fileName = SysAllocString(L"C:\\Users\\Fetterless\\Desktop\\test.pdf");

But now it crashes

My code:

const IID DIID_DPdf = { 0x3B813CE7, 0x7C10, 0x4F84, { 0xAD, 0x06, 0x9D, 0xF7, 0x6D, 0x97, 0xA9, 0xAA } };
const CLSID CLSID_Pdf = { 0xCA8A9780, 0x280D, 0x11CF, { 0xA2, 0x4D, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00 } };

// Register our Window class
WNDCLASS wndclass;
wndclass.style = CS_VREDRAW | CS_HREDRAW;
wndclass.lpfnWndProc = &WindowProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hIcon = NULL;
wndclass.hCursor = NULL;
wndclass.hbrBackground = reinterpret_cast <HBRUSH> (COLOR_BTNFACE + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = windowClassName;
::RegisterClass(&wndclass);

HWND mainWindow = ::CreateWindow(
    windowClassName,
    windowTitle,
    0,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    windowWidth,
    windowHeight,
    NULL,
    NULL,
    0,
    0);
::ShowWindow(mainWindow, 1);
::UpdateWindow(mainWindow);

typedef HRESULT(WINAPI *PFonc)(IUnknown*, HWND, IUnknown**);
HINSTANCE hDLL2 = ::LoadLibrary(TEXT("atl.dll"));
if (!hDLL2)
    return 1;

PFonc AtlAxAttachControl = (PFonc) ::GetProcAddress(hDLL2, "AtlAxAttachControl");

RECT rect;
::GetClientRect(mainWindow, &rect);

container = ::CreateWindowEx(
    WS_EX_CLIENTEDGE,
    L"EDIT",
    L"", WS_CHILD | WS_VISIBLE,
    0,
    0,
    rect.right,
    rect.bottom,
    mainWindow,
    0,
    0,
    0);

HRESULT hr = ::CoInitialize(0);


MIDL_INTERFACE("3B813CE7-7C10-4F84-AD06-9DF76D97A9AA")
IAcroAXDocShim : public IDispatch
{
public:
    virtual VARIANT_BOOL LoadFile(BSTR fileName) = 0;
};

IAcroAXDocShim *pIpdf;
hr = ::CoCreateInstance(CLSID_Pdf, 0, CLSCTX_INPROC_SERVER, DIID_DPdf, (void**)&pIpdf);
hr = AtlAxAttachControl(pIpdf, container, 0);

if (FAILED(hr)) {
    MessageBox(0, L"FAILED(AtlAxAttachControl(pitd, container, NULL))", L"Error", MB_ICONERROR | MB_OK);
}


VARIANT_BOOL res = pIpdf->LoadFile(L"C:\\Users\\Fetterless\\Desktop\\test.pdf");


::MSG message;
while (::GetMessageA(&message, 0, 0, 0))
{
    switch (message.message) {
    case WM_QUIT:
        break;
    default:
        ::TranslateMessage(&message);
        ::DispatchMessage(&message);
        break;
    }
}

CoUninitialize();
FreeLibrary(hDLL2);

Solution

  • TypeLib tools display ... typelib information, which is some kind of a high level definition of types.

    It will not give you raw binary interface layout (exact method signature, calling convention, which must be __stdcall for COM interface methods).

    So, your definition for IAcroAXDocShim is totally wrong, hence crashes due to method signature mismatch. Here is a correct partial definition of it:

    MIDL_INTERFACE("3B813CE7-7C10-4F84-AD06-9DF76D97A9AA")
    IAcroAXDocShim : public IDispatch
    {
    public:
    
        // LoadFile is the 3rd method. You were "blindly" calling this one instead.
        // Note if you don't need these 2, you could just define them as
        // virtual void DontCallMe() = 0;
        virtual HRESULT __stdcall get_src(BSTR* pVal) = 0;
        virtual HRESULT __stdcall put_src(BSTR pVal) = 0;
        virtual HRESULT __stdcall LoadFile(BSTR fileName, VARIANT_BOOL* ret) = 0;
        // the rest is undefined, but if you don't need it, you don't have to define it.
    };