How to call methods of an appobject directly?
-
Hello everybody, I am a beginner in com programming, and i have a question: I have created an ATL 3.0 project including a simple object named MyObj with the attribute of 'appobject' on it's coclass. It has a property named MyString that holds string values. I have successfully built it and then tested it from vb. I have access to MyString property directly without instantiating MyObj. everythings is Ok. but I have problem with VC. From a Console application which has access to that type library (via import directive) MyString property cannot be accessed without qualification. With the statement: MyString = "Hello world!"; compiler generates following error: error C2065: 'MyString' : undeclared identifier what is the problem? do you think i should use a special syntax? if so, would you please help me to know how i can use it? thanks in advance Roozbeh
-
Hello everybody, I am a beginner in com programming, and i have a question: I have created an ATL 3.0 project including a simple object named MyObj with the attribute of 'appobject' on it's coclass. It has a property named MyString that holds string values. I have successfully built it and then tested it from vb. I have access to MyString property directly without instantiating MyObj. everythings is Ok. but I have problem with VC. From a Console application which has access to that type library (via import directive) MyString property cannot be accessed without qualification. With the statement: MyString = "Hello world!"; compiler generates following error: error C2065: 'MyString' : undeclared identifier what is the problem? do you think i should use a special syntax? if so, would you please help me to know how i can use it? thanks in advance Roozbeh
Hi, Not to sure why you are getting a problem. Here is an example of how to use it in VC++ (see if it differs to yours) First, the #import
#import "D:\My Projects\C++\MyObj\MyObj.tlb"
using namespace MyObjLib;Add the namespace to avoid any conflicts with other COM objects you may use that will have the same name. Now when using it:
MyObjLib::IMyObjPtr pMyObj = NULL; try { if (SUCCEEDED(pMyObj.CreateInstance(\_\_uuidof(MyObjLib::MyObj))) && pMyObj != NULL) { pMyObj->MyString = \_T("Hello"); } } catch(\_com\_error &e) { }
This ultimately will use the wrapper method PutMyString( _bstr_t pVal ) that is defined in the two new files generated by the #import statement. These should be in your Debug/Release directories and called MyObj.tlh and MyObj.tli. Ones the header file, the other the implementation file. Have a look through them to see how your COM object has been wrapped. Hope this has helped, Andy