|
-
June 3rd, 2004, 08:44 AM
#1
Problems Using Type Lib with MFC Class Wizard
Before I explain my problem, let me just say that I'm pretty new to C++, all my previous experience came from a 1credit course at University that taught only the basics of writting console apps and some subtle nuances of strings, STL etc. SO Please bare with me 
some background:
I've been given the task of re-writing an NT Service (originally in VB) in VC++. The service uses a proprietary type library for a product called DocsOpen. So far I've been prototyping up the basic components that I'll need to use. Some ADO stuff, the service code, Threading etc.
Now I was hoping that I could implement the DocsOpen Typelib similarly to how the ADO stuff is implemented (using code samples from the MSDN site), unfortunately this is C++.
problem:
When I try to simply #import the .tlb file and use the various
ClassNamePtr classes and then use .CreateInstance passing the GUID etc, it doesn't quite work the way it should. Some functions might work but others wont. Now I'm not too surprised but I was hoping the samples for ADO might work as guide.
Code:
#import "pcdtype.tlb" no_namespace
#include <iostream.h>
#include <stdio.h>
#include <ole2.h>
#include <stdio.h>
#include "conio.h"
void DocsOpenCrap();
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void main()
{
HRESULT hr = S_OK;
if(FAILED(::CoInitialize(NULL)))
return;
if (SUCCEEDED(hr))
{
DocsOpenCrap();
//Wait here for the user to see the output
printf("Press any key to continue...");
getch();
::CoUninitialize();
}
}
void DocsOpenCrap()
{
::ApplicationPtr pApp = NULL;
::DocProfilePtr pProfile = NULL;
HRESULT hr = S_OK;
try
{
TESTHR(pApp.CreateInstance(__uuidof(Application)));//Fails here
pProfile = pApp->CreateNewDocProfile();
}
catch(_com_error &e)
{
cout << "Error" << endl;
}
}
I guess I decided to give up on this method for a while.
I did some looking around and I found that I can use MFC Class Wizard to make a wrapper class for a type lib. I figured this should maybe take care of some of my trouble. When I use this method, I can seemingly create some objects and even use some the functions. But when I use a function that returns an object, then try to cast it to its real type, and then use one of its functions -- it fails. I'm sure I'm doing something obviously stupid. But maybe someone can point me in the right direction.
Here is some code using the class wizard method.
Code:
// DocsTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "DocsTest.h"
#include "pcdtype.h"
#include <comdef.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
::Application* app = new Application;
::_variant_t vtLib;
::Library* libLib;
BOOL b;
vtLib = new _variant_t("TRMS Lib");
cout << "Setting Lib" << endl;
b = app->SetCurrentLibrary(vtLib);
cout << b << endl;
libLib = new Library;
cout << libLib->GetName() << endl;
delete libLib;
libLib = reinterpret_cast<Library*>(app->GetCurrentLibrary());
cout << app->GetFullName() << endl;
cout << libLib->GetName() << endl; //Fails here
delete app;
//delete vtLib; I can't delete for some reason
delete libLib;
}
return nRetCode;
}
output
Setting Lib
-858993460
5F4CCB14
5F4CCB14
Press any key to continue
I forgot to mention before, I have no API reference or toolkit for this DocsOpen product (Too expensive I guess ). Apparently the company doesn't have any code samples in C++, so I guess it doesn't matter anyway!
So, is there any hope here? Is there a general way to use a type library and know how to handle the returns etc. ? I do have some VB code to look at, so I have an idea which objects/methods I need to use. ..
Sorry for the long post. I appreciate any feedback you may have.
Thanks.
Last edited by shawns; June 3rd, 2004 at 08:50 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|