主题:调用 COM 的问题
小弟在网上下了一个按键精灵里的插件 FindPicture.dll, 它是用 COM 写的,我在调用的时候出现问题,请大虾们帮帮忙,万分感激!
[code=c]
void CQQDlg::OnFindPicture()
{
if (S_OK != CoInitialize(NULL))
{
MessageBox("Failed");
return;
}
IQMPluginPtr pPlugin(__uuidof(IDispatch)); // 执行这句时出问题
_variant_t hBmp = pPlugin->LoadBMP("Lory.bmp");
VARIANT x, y;
pPlugin->FindPicture(0, 0, 0, 840, 480, hBmp, 30, 0, &x, &y);
pPlugin->FreeBMP(&hBmp);
CoUninitialize();
}
[/code]
我在代码中加 MessageBox 函数得出在 [color=FF0000]IQMPluginPtr pPlugin(__uuidof(IDispatch));[/color] 处出现运行时错误,提示为:
Runtime Error!
Program:
This application has requested the Runtime to terminate it in an unusal way.
Please contact the application's support team for more information.
我试过了其他几个插件,也是同样的错误。
我操作 COM 的方法是:在 stdafx.h 中加入 #import "C:\WINDOWS\system32\FindPicture.dll" no_namespace
编译后会生成两个文件,文件中的内容为:
[code=c]
// FindPicture.plh
// Created by Microsoft (R) C/C++ Compiler Version 12.00.8168.0 (6f3e2e4e).
//
// d:\程序设计\c++ source code\com\release\FindPicture.tlh
//
// C++ source equivalent of Win32 type library C:\WINDOWS\system32\FindPicture.dll
// compiler-generated file created 10/24/08 at 23:14:50 - DO NOT EDIT!
#pragma once
#pragma pack(push, 8)
#include <comdef.h>
//
// Forward references and typedefs
//
struct __declspec(uuid("1e7051ce-6430-4ba9-a965-3eac6d1ac422"))
/* dispinterface */ IQMPlugin;
struct __declspec(uuid("ee1035ba-1025-4cde-ad4c-a04b3878bf00"))
/* dispinterface */ IQMPluginStandard;
struct /* coclass */ FindPicture2;
//
// Smart pointer typedef declarations
//
_COM_SMARTPTR_TYPEDEF(IQMPlugin, __uuidof(IDispatch));
_COM_SMARTPTR_TYPEDEF(IQMPluginStandard, __uuidof(IDispatch));
//
// Type library items
//
struct __declspec(uuid("1e7051ce-6430-4ba9-a965-3eac6d1ac422"))
IQMPlugin : IDispatch
{
//
// Wrapper methods for error-handling
//
// Methods:
_variant_t LoadBMP (
const _variant_t & FileNameArray );
long FreeBMP (
VARIANT * AddressArray );
long FindPicture (
long hwnd,
long Left,
long Top,
long Right,
long Bottom,
long Address,
long Cd,
float Similar,
VARIANT * x,
VARIANT * y );
_variant_t FindPictureA (
long hwnd,
long Left,
long Top,
long Right,
long Bottom,
long Address,
long Cd,
float Similar );
long FindPictureN (
long hwnd,
long Left,
long Top,
long Right,
long Bottom,
const _variant_t & AddressArray,
short Cd,
float Similar,
VARIANT * x,
VARIANT * y,
VARIANT * number );
_variant_t FindPictureNA (
long hwnd,
long Left,
long Top,
long Right,
long Bottom,
const _variant_t & AddressArray,
short Cd,
float Similar );
};
struct __declspec(uuid("ee1035ba-1025-4cde-ad4c-a04b3878bf00"))
IQMPluginStandard : IDispatch
{
//
// Wrapper methods for error-handling
//
// Methods:
_bstr_t Get_Plugin_Description (
_bstr_t bstrItemName );
};
struct __declspec(uuid("65643271-126e-4fc8-b430-1c6143484aa9"))
FindPicture2;
// [ default ] dispinterface IQMPlugin
// dispinterface IQMPluginStandard
//
// Wrapper method implementations
//
#include "d:\程序设计\c++ source code\com\release\FindPicture.tli"
#pragma pack(pop)
[/code]
[code=c]
// Created by Microsoft (R) C/C++ Compiler Version 12.00.8168.0 (6f3e2e4e).
//
// d:\程序设计\c++ source code\com\release\FindPicture.tli
//
// Wrapper implementations for Win32 type library C:\WINDOWS\system32\FindPicture.dll
// compiler-generated file created 10/24/08 at 23:14:50 - DO NOT EDIT!
#pragma once
//
// dispinterface IQMPlugin wrapper method implementations
//
inline _variant_t IQMPlugin::LoadBMP ( const _variant_t & FileNameArray ) {
VARIANT _result;
_com_dispatch_method(this, 0x1, DISPATCH_METHOD, VT_VARIANT, (void*)&_result,
L"\x000c", &FileNameArray);
return _variant_t(_result, false);
}
inline long IQMPlugin::FreeBMP ( VARIANT * AddressArray ) {
long _result;
_com_dispatch_method(this, 0x2, DISPATCH_METHOD, VT_I4, (void*)&_result,
L"\x400c", AddressArray);
return _result;
}
inline long IQMPlugin::FindPicture ( long hwnd, long Left, long Top, long Right, long Bottom, long Address, long Cd, float Similar, VARIANT * x, VARIANT * y ) {
long _result;
_com_dispatch_method(this, 0x3, DISPATCH_METHOD, VT_I4, (void*)&_result,
L"\x0003\x0003\x0003\x0003\x0003\x0003\x0003\x0004\x400c\x400c", hwnd, Left, Top, Right, Bottom, Address, Cd, Similar, x, y);
return _result;
}
inline _variant_t IQMPlugin::FindPictureA ( long hwnd, long Left, long Top, long Right, long Bottom, long Address, long Cd, float Similar ) {
VARIANT _result;
_com_dispatch_method(this, 0x4, DISPATCH_METHOD, VT_VARIANT, (void*)&_result,
L"\x0003\x0003\x0003\x0003\x0003\x0003\x0003\x0004", hwnd, Left, Top, Right, Bottom, Address, Cd, Similar);
return _variant_t(_result, false);
}
inline long IQMPlugin::FindPictureN ( long hwnd, long Left, long Top, long Right, long Bottom, const _variant_t & AddressArray, short Cd, float Similar, VARIANT * x, VARIANT * y, VARIANT * number ) {
long _result;
_com_dispatch_method(this, 0x5, DISPATCH_METHOD, VT_I4, (void*)&_result,
L"\x0003\x0003\x0003\x0003\x0003\x000c\x0002\x0004\x400c\x400c\x400c", hwnd, Left, Top, Right, Bottom, &AddressArray, Cd, Similar, x, y, number);
return _result;
}
inline _variant_t IQMPlugin::FindPictureNA ( long hwnd, long Left, long Top, long Right, long Bottom, const _variant_t & AddressArray, short Cd, float Similar ) {
VARIANT _result;
_com_dispatch_method(this, 0x6, DISPATCH_METHOD, VT_VARIANT, (void*)&_result,
L"\x0003\x0003\x0003\x0003\x0003\x000c\x0002\x0004", hwnd, Left, Top, Right, Bottom, &AddressArray, Cd, Similar);
return _variant_t(_result, false);
}
//
// dispinterface IQMPluginStandard wrapper method implementations
//
inline _bstr_t IQMPluginStandard::Get_Plugin_Description ( _bstr_t bstrItemName ) {
BSTR _result;
_com_dispatch_method(this, 0x1, DISPATCH_METHOD, VT_BSTR, (void*)&_result,
L"\x0008", (BSTR)bstrItemName);
return _bstr_t(_result, false);
}
[/code]
[code=c]
void CQQDlg::OnFindPicture()
{
if (S_OK != CoInitialize(NULL))
{
MessageBox("Failed");
return;
}
IQMPluginPtr pPlugin(__uuidof(IDispatch)); // 执行这句时出问题
_variant_t hBmp = pPlugin->LoadBMP("Lory.bmp");
VARIANT x, y;
pPlugin->FindPicture(0, 0, 0, 840, 480, hBmp, 30, 0, &x, &y);
pPlugin->FreeBMP(&hBmp);
CoUninitialize();
}
[/code]
我在代码中加 MessageBox 函数得出在 [color=FF0000]IQMPluginPtr pPlugin(__uuidof(IDispatch));[/color] 处出现运行时错误,提示为:
Runtime Error!
Program:
This application has requested the Runtime to terminate it in an unusal way.
Please contact the application's support team for more information.
我试过了其他几个插件,也是同样的错误。
我操作 COM 的方法是:在 stdafx.h 中加入 #import "C:\WINDOWS\system32\FindPicture.dll" no_namespace
编译后会生成两个文件,文件中的内容为:
[code=c]
// FindPicture.plh
// Created by Microsoft (R) C/C++ Compiler Version 12.00.8168.0 (6f3e2e4e).
//
// d:\程序设计\c++ source code\com\release\FindPicture.tlh
//
// C++ source equivalent of Win32 type library C:\WINDOWS\system32\FindPicture.dll
// compiler-generated file created 10/24/08 at 23:14:50 - DO NOT EDIT!
#pragma once
#pragma pack(push, 8)
#include <comdef.h>
//
// Forward references and typedefs
//
struct __declspec(uuid("1e7051ce-6430-4ba9-a965-3eac6d1ac422"))
/* dispinterface */ IQMPlugin;
struct __declspec(uuid("ee1035ba-1025-4cde-ad4c-a04b3878bf00"))
/* dispinterface */ IQMPluginStandard;
struct /* coclass */ FindPicture2;
//
// Smart pointer typedef declarations
//
_COM_SMARTPTR_TYPEDEF(IQMPlugin, __uuidof(IDispatch));
_COM_SMARTPTR_TYPEDEF(IQMPluginStandard, __uuidof(IDispatch));
//
// Type library items
//
struct __declspec(uuid("1e7051ce-6430-4ba9-a965-3eac6d1ac422"))
IQMPlugin : IDispatch
{
//
// Wrapper methods for error-handling
//
// Methods:
_variant_t LoadBMP (
const _variant_t & FileNameArray );
long FreeBMP (
VARIANT * AddressArray );
long FindPicture (
long hwnd,
long Left,
long Top,
long Right,
long Bottom,
long Address,
long Cd,
float Similar,
VARIANT * x,
VARIANT * y );
_variant_t FindPictureA (
long hwnd,
long Left,
long Top,
long Right,
long Bottom,
long Address,
long Cd,
float Similar );
long FindPictureN (
long hwnd,
long Left,
long Top,
long Right,
long Bottom,
const _variant_t & AddressArray,
short Cd,
float Similar,
VARIANT * x,
VARIANT * y,
VARIANT * number );
_variant_t FindPictureNA (
long hwnd,
long Left,
long Top,
long Right,
long Bottom,
const _variant_t & AddressArray,
short Cd,
float Similar );
};
struct __declspec(uuid("ee1035ba-1025-4cde-ad4c-a04b3878bf00"))
IQMPluginStandard : IDispatch
{
//
// Wrapper methods for error-handling
//
// Methods:
_bstr_t Get_Plugin_Description (
_bstr_t bstrItemName );
};
struct __declspec(uuid("65643271-126e-4fc8-b430-1c6143484aa9"))
FindPicture2;
// [ default ] dispinterface IQMPlugin
// dispinterface IQMPluginStandard
//
// Wrapper method implementations
//
#include "d:\程序设计\c++ source code\com\release\FindPicture.tli"
#pragma pack(pop)
[/code]
[code=c]
// Created by Microsoft (R) C/C++ Compiler Version 12.00.8168.0 (6f3e2e4e).
//
// d:\程序设计\c++ source code\com\release\FindPicture.tli
//
// Wrapper implementations for Win32 type library C:\WINDOWS\system32\FindPicture.dll
// compiler-generated file created 10/24/08 at 23:14:50 - DO NOT EDIT!
#pragma once
//
// dispinterface IQMPlugin wrapper method implementations
//
inline _variant_t IQMPlugin::LoadBMP ( const _variant_t & FileNameArray ) {
VARIANT _result;
_com_dispatch_method(this, 0x1, DISPATCH_METHOD, VT_VARIANT, (void*)&_result,
L"\x000c", &FileNameArray);
return _variant_t(_result, false);
}
inline long IQMPlugin::FreeBMP ( VARIANT * AddressArray ) {
long _result;
_com_dispatch_method(this, 0x2, DISPATCH_METHOD, VT_I4, (void*)&_result,
L"\x400c", AddressArray);
return _result;
}
inline long IQMPlugin::FindPicture ( long hwnd, long Left, long Top, long Right, long Bottom, long Address, long Cd, float Similar, VARIANT * x, VARIANT * y ) {
long _result;
_com_dispatch_method(this, 0x3, DISPATCH_METHOD, VT_I4, (void*)&_result,
L"\x0003\x0003\x0003\x0003\x0003\x0003\x0003\x0004\x400c\x400c", hwnd, Left, Top, Right, Bottom, Address, Cd, Similar, x, y);
return _result;
}
inline _variant_t IQMPlugin::FindPictureA ( long hwnd, long Left, long Top, long Right, long Bottom, long Address, long Cd, float Similar ) {
VARIANT _result;
_com_dispatch_method(this, 0x4, DISPATCH_METHOD, VT_VARIANT, (void*)&_result,
L"\x0003\x0003\x0003\x0003\x0003\x0003\x0003\x0004", hwnd, Left, Top, Right, Bottom, Address, Cd, Similar);
return _variant_t(_result, false);
}
inline long IQMPlugin::FindPictureN ( long hwnd, long Left, long Top, long Right, long Bottom, const _variant_t & AddressArray, short Cd, float Similar, VARIANT * x, VARIANT * y, VARIANT * number ) {
long _result;
_com_dispatch_method(this, 0x5, DISPATCH_METHOD, VT_I4, (void*)&_result,
L"\x0003\x0003\x0003\x0003\x0003\x000c\x0002\x0004\x400c\x400c\x400c", hwnd, Left, Top, Right, Bottom, &AddressArray, Cd, Similar, x, y, number);
return _result;
}
inline _variant_t IQMPlugin::FindPictureNA ( long hwnd, long Left, long Top, long Right, long Bottom, const _variant_t & AddressArray, short Cd, float Similar ) {
VARIANT _result;
_com_dispatch_method(this, 0x6, DISPATCH_METHOD, VT_VARIANT, (void*)&_result,
L"\x0003\x0003\x0003\x0003\x0003\x000c\x0002\x0004", hwnd, Left, Top, Right, Bottom, &AddressArray, Cd, Similar);
return _variant_t(_result, false);
}
//
// dispinterface IQMPluginStandard wrapper method implementations
//
inline _bstr_t IQMPluginStandard::Get_Plugin_Description ( _bstr_t bstrItemName ) {
BSTR _result;
_com_dispatch_method(this, 0x1, DISPATCH_METHOD, VT_BSTR, (void*)&_result,
L"\x0008", (BSTR)bstrItemName);
return _bstr_t(_result, false);
}
[/code]