Game Development Reference
In-Depth Information
Accessing from Unmanaged Code
The hardest part is over; you can now work with your exposed component and
start invoking its methods. As covered in the last section, you are now left with a
type library file (.tlb) that you can import into a C++ application (or any other
platform or language supporting COM) to recognize your exposed component.
The unmanaged ClientTest example located on the Companion Web site shows
how to consume the exposed component. The following code describes the source
code to the ClientTest executable (standard Win32 project, unmanaged C++).
Notice how the type library file ( .tlb ) is referenced with the import directive. The
linker will actually generate a second file from this one called a type library header,
which contains some specific C++ wrapper functionality. Both files can be used to
get a better understanding of what is going on behind the scenes of COM Interop.
#include <windows.h>
#include <atlbase.h>
#include <atlcom.h>
#include <comutil.h>
#import “MyComponent.tlb” no_namespace
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
::CoInitialize(NULL);
CComPtr<IMyComponent> myComponent;
// Acquire the unique identifier of the COM server (.NET Component)
CLSID myComponentClassID = __uuidof(MyComponent);
// Acquired a reference to the COM server (.NET Component)
if (SUCCEEDED(myComponent.CoCreateInstance(myComponentClassID,
0, CLSCTX_ALL)))
{
char output[64];
if (SUCCEEDED(myComponent->CustomMethod1()))
{
::MessageBox(0,
“Successfully invoked CustomMethod1()!”,
“Test CustomMethod1()”,
0);
}
Search Nedrilad ::




Custom Search