|
1. Use ATL to build a COM component with 2 interfaces, and implement lowercase conversion of strings in one of the automation interfaces (such as converting HELLO to hello); implement 2 methods in another common interface: two Subtraction between numbers (such as: ab) and division between two numbers (such as: a / b); parameter types are designed by themselves.
2. Use VC Win32 Console Application for client program development to achieve the purpose of calling these 3 methods in this component. (You must call the lowercase conversion of the string through the IDispatch interface, use QueryInterface () to switch between the two interfaces, and then directly call the subtraction and division calls in the ordinary COM interface through Vtable, and select the original interface pointer or smart pointer by yourself) .
the following
Is the younger user program:
#include <atlbase.h>
#include <comutil.h>
#include <iostream.h>
#import "E:\ATLPjcts\Level_C\Debug\Level_C.dll"
int main (int argc, char * argv [])
{
HRESULT hResult;
hResult = CoInitialize (NULL);
if (FAILED (hResult))
{
cout << "Initialize COM library failed!\n";
return -1;
}
GUID Clisd;
hResult = :: CLSIDFromProgID (L "Level_C.MyIntface",&Clisd);
if (FAILED (hResult))
{
cout << "Can't find the CLSID!\n";
return -2;
}
IDispatch * pIDisp = NULL;
hResult = CoCreateInstance (Clisd, NULL,
CLSCTX_INPROC_SERVER, IID_IDispatch, (void **)&pIDisp);
if (FAILED (hResult))
{
cout << "Create object failed!\n";
return -2;
}
///////////////////////////////////////////////////////// ////////////////////////////////////////
CComBSTR BSFunName1 (L "Upper"); // Prepare to get the sequence number of the Upper function DispID
DISPID dispID1; // DispID
hResult = pIDisp-> GetIDsOfNames (// Get the number of the function according to the function name
IID_NULL,
&BSFunName1, // function name
1, // number of elements in BSFunName3
LOCALE_SYSTEM_DEFAULT, // use system default locale
&dispID1); // return value
if (FAILED (hResult))
{
cout << "GetIDsOfNames failed!\n";
return -2;
}
Ranch
VARIANTARG bstr [1]; // required parameters
bstr [0] .vt = VT_BSTR; bstr [0] .bstrVal = SysAllocString (L "hello world"); // parameter
Ranch
DISPPARAMS dispParams1 = {bstr, NULL, 1, 0}; // wrap the parameters in this structure
VARIANT Result1; // result
Ranch
hResult = pIDisp-> Invoke (// call
dispID1, // function specified by dispID
IID_NULL,
LOCALE_SYSTEM_DEFAULT, // use system default locale
DISPATCH_METHOD, // method is called, not a property
&dispParams1, // parameter
&Result1, // return value
NULL, // don't consider exception handling
NULL); // Do not consider error handling
if (FAILED (hResult))
{
printf ("Invoke failed!\n");
return -2;
}
Ranch
char * p = _com_util :: ConvertBSTRToString (Result1.bstrVal);
cout << p << endl;
//////////////////////////////////////// /////////////// //////////////////////////////////////// ///////////////
IMyMethod * pIMyMethod;
IMyIntface * pIMyIntface;
long a = 890, b = 90;
long result2;
hResult = pIDisp-> QueryInterface (IID_IMyIntface, (void **) pIMyIntface);
if (FAILED (hResult))
{
cout << "queryintface IMyIntface is failed!\n";
return -2;
}
hResult = pIMyIntface-> QueryInterface (IID_IMyMethod, (void **) pIMyMethod);
if (FAILED (hResult))
{
cout << "queryintface IMyMethod is failed!\n";
return -2;
}
result2 = pIMyMethod-> Sub (a, b);
cout << "a-b =" << result2 << endl;
pIDisp-> Release ();
CoUninitialize ();
return 0;
}
The error is:
E:\ATLPjcts\test\test.cpp (73): error C2065: 'IMyMethod': undeclared identifier
E:\ATLPjcts\test\test.cpp (73): error C2065: 'pIMyMethod': undeclared identifier
E:\ATLPjcts\test\test.cpp (73): warning C4552: '*': operator has no effect; expected operator with side-effect
E:\ATLPjcts\test\test.cpp (74): error C2065: 'IMyIntface': undeclared identifier
E:\ATLPjcts\test\test.cpp (74): error C2065: 'pIMyIntface': undeclared identifier
E:\ATLPjcts\test\test.cpp (74): warning C4552: '*': operator has no effect; expected operator with side-effect
E:\ATLPjcts\test\test.cpp (77): error C2065: 'IID_IMyIntface': undeclared identifier
E:\ATLPjcts\test\test.cpp (84): error C2227: left of '-> QueryInterface' must point to class / struct / union
E:\ATLPjcts\test\test.cpp (84): error C2065: 'IID_IMyMethod': undeclared identifier
E:\ATLPjcts\test\test.cpp (90): error C2227: left of '-> Sub' must point to class / struct / union
Boss, please help me, I am grateful! !! |
|