当我为Com对象尝试CoCreateInstance时,编译器会抛出错误。
该行:
hr = CoCreateInstance(CLSID_AccPropServices, nullptr, CLSCTX_INPROC, IID_PPV_ARGS(_pAccPropServices));
错误:
错误1错误C2100:非法间接
错误2错误C2784:'void ** IID_PPV_ARGS_Helper(T **)':无法从'IAccPropServices *'中推断'T **'的模板参数
我从中获取了源代码 this链接。
还试图:
HRESULT hr;
if (hr = CoInitialize(NULL))
{
return FALSE;
}
...
IAccPropServices* _pAccPropServices = NULL;
hr = CoCreateInstance(CLSID_AccPropServices, nullptr, CLSCTX_INPROC, IID_IAccPropServices, (void**)_pAccPropServices);
if (FAILED(hr))
{
MessageBox(NULL, _com_error(hr).ErrorMessage(), L"", 0);
return hr;
}
返回以下错误:
程序'[2348]已'...'退出,代码为-2147024809(0x80070057)。
如何解决这个问题?
更新
我要做的是在winapi中为编辑框设置可访问的名称。对于此操作,我必须comnitializecom对象库并创建IAccPropServices接口的实例。我成功地通过CoInitialize(NULL)函数初始化com库。但是我没能通过CoCreateInstance()函数创建IAccPropServices接口的实例。
我提供了孔页:
#include "Main.h"
#include "resource.h"
#include <comdef.h>
#include <initguid.h>
#include "objbase.h"
#include "uiautomation.h"
#define NDEBUG
#include <assert.h>
//
LRESULT CALLBACK ControlWndProc(HWND, UINT, WPARAM, LPARAM);
HWND hWnd;;
HWND hWndEdit1;
HWND hWndEdit2;
void NextFocus()
{
if (hWndEdit1 != GetFocus())
{
SetFocus(hWndEdit1);
}
else
{
SetFocus(hWndEdit2);
}
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
HRESULT hr;
if (hr = CoInitialize(NULL))
{
return FALSE;
}
LPTSTR windowClass = TEXT("WinApp");
LPTSTR windowTitle = TEXT("Windows Application");
WNDCLASSEX wc;
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = ControlWndProc;
wc.lpszClassName = windowClass;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, L"failed to register class.", L"", 0);
}
if (!(hWnd = CreateWindowEx(WS_EX_CONTROLPARENT, windowClass, windowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL)))
{
MessageBox(NULL, L"failed to create window..", L"", 0);
return EXIT_FAILURE;
}
hWndEdit1 = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("Test"), WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_TABSTOP | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_LEFT | ES_MULTILINE | ES_WANTRETURN | ES_OEMCONVERT, 4, 8, 500, 300, hWnd, NULL, NULL, NULL);
hWndEdit2 = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("Test"), WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_TABSTOP | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_LEFT | ES_MULTILINE | ES_WANTRETURN | ES_OEMCONVERT, 4, 316, 500, 150, hWnd, NULL, NULL, NULL);
// attention
IAccPropServices* _pAccPropServices = NULL;
hr = CoCreateInstance(CLSID_AccPropServices, nullptr, CLSCTX_INPROC, IID_IAccPropServices, (void**)_pAccPropServices);
if (FAILED(hr))
{
MessageBox(NULL, _com_error(hr).ErrorMessage(), L"", 0);
return hr; // returns with error code The program '[2348] has '...' exited with code -2147024809 (0x80070057).
}
WCHAR accessibleName[80];
hr = _pAccPropServices->SetHwndPropStr(hWndEdit1, OBJID_CLIENT, CHILDID_SELF, Name_Property_GUID, L"Query");
//
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
SetFocus(hWndEdit1);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (msg.message == WM_KEYDOWN && msg.wParam == VK_F6)
{
NextFocus();
}
else if (msg.message == WM_KEYDOWN && msg.wParam == VK_F5)
{
SendMessage(hWndEdit1, WM_SETTEXT, 0, (LPARAM)L"hello");
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
CoUninitialize();
return EXIT_SUCCESS;
}
LRESULT CALLBACK ControlWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == IDEXIT)
{
}
break;
case WM_INITDIALOG:
break;
case WM_CREATE:
break;
case WM_DESTROY:
PostQuitMessage(EXIT_SUCCESS);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}