First thing, if you are not familiar with how to write or use one of the Microsoft's oldest 'open' component-oriented design - COM, you might want to start from a good tutorial. Believe it or not, there are countless man months of many great engineers' professional lives have been ruthlessly swallowed by Microsoft.
Ok, I had my rants. Here is the trick, and I have not found any help or information in dealing with such problem via Google, so my two pennies.
You could use Visual Studio 2003/2005/2008 etc to work on your own unmanaged code if you are indeed a solid C++ engineer and have belief in the pure logic world. Problem comes when you try to use COM component within your unmanaged code (not via Interop which is to serve marshalling between managed and unmanaged boundary).
First of all, you use magic #import "xxx.tlb" to import type library into your unmanaged world, and choose your fancy namespace, such as:
#import "MyCOM15.tlb"
using namespace MyCOM15;
Then you initialise your code to be "COM aware" by calling
CoInitialize(NULL);
in the beginning of your code snippets. In the meantime, put CoInitialize(NULL); before you are done. (Apology if this reads like teaching grandma sucking eggs.) After this, you retrieve the famous Dispatch interface and create instance of choice based on uuid:
CLARITYCOMLib::IMyField10Ptr fieldPtr = NULL;
fieldPtr.CreateInstance(__uuidof(MyField10));
Now you think you can relax... so did I.
You can not compile!!! You will see error like this
error C3861: 'TEXT': identifier not found C:\Program Files\Microsoft Visual Studio 8\VC\include\comdef.h 255
"TEXT" is a macro for different character set setting. You might think to make sure all your project have Unicode character setting would make this go away. Well, apparently we are wrong again. Long story short, the way to fix this is to change "TEXT" into "(_TCHAR*)&". For example, the original line in comdef.h:
_COM_PRINTF_S_1(m_pszMsg, 32, TEXT("IDispatch error #%d"), wCode);
will become
_COM_PRINTF_S_1(m_pszMsg, 32, (_TCHAR*)&("IDispatch error #%d"), wCode);
Also, include "tchar.h" in your header files.
At last, make sure your unmanaged code project does not have /clr:pure or /clr:safe turned on. These two types of common runtime support are not compatible with what we are trying to achieve here.
No comments:
Post a Comment