Docsity

Midterm Exam 2 with Solution for Distributed Objects | CSE 775, Exams of Engineering

Toggle sidebar
bg1
CSE775 – Distributed Objects Spring 2009
Midterm #2 Examination
Version 2
Name:_______Instructor’s Solution___________ SUID:____________________
This is a closed book examination. Please place all your books on the floor beside
you. You may keep one page of notes on your desktop in addition to this exam
package. All examinations will be collected promptly at the end of class. Please
be prepared to quickly hand in your examination at that time.
If you have any questions, please do not leave your seat. Raise your hand and I
will come to your desk to discuss your question. I will answer all questions about
the meaning of the wording of any question. I may choose not to answer other
questions.
You will find it helpful to review all questions before beginning. All questions are
given equal weight for grading, but not all questions have the same difficulty.
Therefore, it is very much to your advantage to answer first those questions you
believe to be easiest.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Midterm Exam 2 with Solution for Distributed Objects | CSE 775 and more Exams Engineering in PDF only on Docsity!

Midterm #2 Examination

Version 2

Name:_______Instructor’s Solution___________ SUID:____________________

This is a closed book examination. Please place all your books on the floor beside you. You may keep one page of notes on your desktop in addition to this exam package. All examinations will be collected promptly at the end of class. Please be prepared to quickly hand in your examination at that time. If you have any questions, please do not leave your seat. Raise your hand and I will come to your desk to discuss your question. I will answer all questions about the meaning of the wording of any question. I may choose not to answer other questions. You will find it helpful to review all questions before beginning. All questions are given equal weight for grading, but not all questions have the same difficulty. Therefore, it is very much to your advantage to answer first those questions you believe to be easiest.

  1. Describe all the parts that are required to build a COM component. What are the responsibilities of each part? Answer: There are two ways to answer this question, by describing the component’s logical parts or, by describing its physical parts. We will do both here: Logical Parts: a. IDL that describes the component, interfaces, and typelibrary with GUIDs b. Interfaces derived from IUnknown, e.g., IMyComponent, and IClassFactory c. Classes that implement IMyComponent and IClassFactory d. Server code that implements for an in-proc component: i. DllGetClassObject: COM’s access to class factory ii. DllRegisterServer : register component iii. DllUnregisterServer: unregister component iv. DllCanUnloadNow: mark for unloading e. Server code that implements for an out-of-proc component: i. main or WinMain: entry point for execution ii. _AtlModule: server infrastructure f. COM run-time that provides functions like: i. Co(Un)Initialize(Ex): create and remove apartment ii. CoCreateInstance: activate component iii. CoRegisterClassObject: store pointer to class factory in ROT Physical Parts: g. Component.h - defines ATL class h. Component.cpp - defines functions that implement interface i. Server.idl - Interface Definition Language j. Server.cpp - dll functions as in d., above k. Server_i.h - generated by MIDL, declares interfaces l. Server_i.c - generated by MIDL, defines GUIDs m. dlldata.c, Server_p.c - generated by MIDL, defines proxy code

since communication threading is handled by the rpc channel. In this case each process may use either STA or MTA.

  1. Describe how the IDispatch interface works and why it is used. Answer: IDispatch is intended to be used by code that cannot compile the declarations of an interface to builds its virtual function pointer table. Perhaps the calling code is a scripting language which is not compiled, or perhaps it is used by a client that needs to load a component unknown at compile time. IDispatch provides four functions: a. GetTypeInfoCount returns 1 if TypeInfo interface is available, else 0 b. GetTypeInfo returns TypeInfo interface if available c. GetIDsOfNames maps single method name and its parameter names into DISPIDs d. Invoke call method or access property for an input DISPID TypeInfo contains all the information provided by IDL and serves the same purpose as reflection in .Net. GetIDsOfNames returns a dispatch id that matches a specified function name and may also contain information about the functions parameters. Invoke takes the dispatch id and parameters, wrapped in a DISPPARAMS structure, and calls the corresponding method, returning its result as a variant.
  1. Write client and server code required to implement the query part of the interface discussed in the previous problem. State the COM memory management policy that governs these operations. Answer: Client code: #include "..\FileQuery\FileQuery_i.h" #include #include int _tmain(int argc, _TCHAR* argv[]) { std::cout << "\n Solution for CSE775 - MT2, Q4 - File Queries"; std::cout << "\n ==============================================\n"; ::CoInitialize(NULL); try { CComQIPtr pFiles; pFiles.CoCreateInstance(CLSID_Files); HRESULT hr; if(pFiles) { std::cout << "\n create succeeded\n"; CComBSTR path(L”C:\temp”); int FileCount; BSTR* FileNames; int* FileSizes; hr = pFiles->getFiles(path, &FileCount, &FileNames, &FileSizes); if(!SUCCEEDED(hr)) { std::cout << "\n query failed\n\n"; return 1; } for(int i=0; i<FileCount; ++i) { std::wcout.setf(std::ios::adjustfield, std::ios::right); std::wcout << L"\n " << std::setw(20) << FileNames[i] << L" "; std::wcout << FileSizes[i]; ::SysFreeString(FileNames[i]); } CoTaskMemFree(FileNames); CoTaskMemFree(FileSizes); std::cout << "\n\n"; } } catch(std::exception& ex) { std::cout << "\n\n " << ex.what() << "\n\n"; return 1; } return 0; }

Server Code: #include "Files.h" STDMETHODIMP CFiles::getFiles( BSTR path, int* pFileCount, BSTR** fileNames, int** fileSizes ) { wcout << L”\n Pretending to use path “ << path << L”\n”; pFileCount = 5; BSTR pFileNames = (BSTR)CoTaskMemAlloc((pFileCount)sizeof(BSTR)); pFileNames[0] = SysAllocString(L"A.h"); pFileNames[1] = SysAllocString(L"A.cpp"); pFileNames[2] = SysAllocString(L"A.idl"); pFileNames[3] = SysAllocString(L"LongerFileName.h"); pFileNames[4] = SysAllocString(L"LongerFileName.cpp"); fileNames = pFileNames; int pFileSizes = (int)CoTaskMemAlloc((pFileCount)sizeof(int)); pFileSizes[0] = 12345; pFileSizes[1] = 54321; pFileSizes[2] = 12321; pFileSizes[3] = 123; pFileSizes[4] = 321; *fileSizes = pFileSizes; return S_OK; }

  1. What role does IDL play in marshaling data between two COM apartments? This question bears on both what marshaling needs from the IDL and how marshaling code is generated. Answer: IDL describes the types and consequently sizes of data to be sent between client and server. If client and server reside in different apartments, then COM has to marshal the data. That means that the local call stackframe is decomposed into a series of messages, set through an rpc channel, and recomposed into a stackframe to apply to the server object. For this, the marshaler needs to know sizes of things that are sent through the channel. IDL provides that information, possibly with the help of attributes like size_is(count), and string. It also describes the direction(s) that data must be sent with [in] and [out] parameters. Marshaling code is generated by the MIDL compiler, as it translates an IDL file. In particular, it generates the files dlldata.c and server_p.c, which define the proxy and stub code used for marshaling. MIDL also generates a typelibrary file with extension .tlb, used for typelibrary marshaling. That uses a generic proxy that knows how to send all of the automation types. You tell COM to use typelibrary marshaling by including the oleautomation attribute in the IDL’s object attribute.