gpt4 book ai didi

c++ - AccessibleObjectFromWindow 返回 E_FAIL 代码

转载 作者:太空狗 更新时间:2023-10-29 21:49:16 25 4
gpt4 key购买 nike

请检查以下代码,它应该连接到当前正在运行的 Excel:

#include <windows.h>
#include <oleacc.h>
#import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\MSO.DLL" no_implementation rename("RGB", "ExclRGB") rename("DocumentProperties", "ExclDocumentProperties") rename("SearchPath", "ExclSearchPath")
#import "C:\Program Files (x86)\Common Files\microsoft shared\VBA\VBA6\VBE6EXT.OLB" no_implementation
#import "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE" rename("DialogBox", "ExclDialogBox") rename("RGB", "ExclRGB") rename("CopyFile", "ExclCopyFile") rename("ReplaceText", "ExclReplaceText")

BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM)
{
WCHAR szClassName[64];
if(GetClassNameW(hwnd, szClassName, 64))
{
if(_wcsicmp(szClassName, L"EXCEL7") == 0)
{
//Get AccessibleObject
Excel::Window* pWindow = NULL;
HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, __uuidof(Excel::Window), (void**)&pWindow);
if(hr == S_OK)
{
//Excel object is now in pWindow pointer, from this you can obtain the document or application
Excel::_Application* pApp = NULL;
pApp = pWindow->GetApplication();
pWindow->Release();
}
return false; // Stops enumerating through children
}
}
return true;
}

int main( int argc, CHAR* argv[])
{
//The main window in Microsoft Excel has a class name of XLMAIN
HWND excelWindow = FindWindow(L"XLMAIN", NULL);
//Use the EnumChildWindows function to iterate through all child windows until we find _WwG
EnumChildWindows(excelWindow, (WNDENUMPROC) EnumChildProc, (LPARAM)1);
return 0;
}

事实是 Excel 确实在当前时刻运行,但 AccessibleObjectFromWindow 返回 E_FAIL。我还尝试循环运行此代码并切换到 Excel 以专注于它的应用程序。同理,AccessibleObjectFromWindow 返回 E_FAIL。我现在正在互联网上搜索答案,但之前找到的所有答案都没有给我任何帮助。因此,如果有人可以提供解释,我们将不胜感激。

最佳答案

从一些代码示例中找到答案:

int main( int argc, CHAR* argv[])
{
CoInitialize( NULL );
...

在开始使用任何其他对象之前通过调用 CoInitialize(NULL) 解决了该问题。

关于c++ - AccessibleObjectFromWindow 返回 E_FAIL 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8594304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com