我正在尝试将PDF加载到窗口中。我为此使用Adobe Acrobat activeX和WinApi。我可以用此代码“插入” Web浏览器,但不能使用PDF。执行时出现错误:

"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."


似乎LoadFile原型中存在问题,但是从ITypeLibViewer我得到:

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


所以一切似乎还好。哪里有问题?

UPD1:现在传递文件名的操作如下:

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


但是现在它崩溃了

我的代码:

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);

最佳答案

TypeLib工具显示... typelib信息,这是某种类型的高级定义。

它不会给您原始的二进制接口布局(确切的方法签名,调用约定,对于COM接口方法,必须为__stdcall)。

因此,您对IAcroAXDocShim的定义完全错误,因此由于方法签名不匹配而崩溃。这是正确的部分定义:

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.
};

08-26 17:06