|
-
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.
-
June 3rd, 2004, 09:06 AM
#2
You are using something like this
Code:
::_variant_t vtLib;
.........
vtLib = new _variant_t("TRMS Lib");
........
//delete vtLib; I can't delete for some reason
Note 'vtLib' is not a pointer so,, either use
Code:
::_variant_t vtLib("TRMS Lib");
or
Code:
::_variant_t *vtLib; // as a pointer
.........
vtLib = new _variant_t("TRMS Lib");
........
delete vtLib; //can delete now
-
June 3rd, 2004, 09:10 AM
#3
Thanks for the pointer help. Now about the rest of it... any ideas guys/gals?
-
June 3rd, 2004, 10:14 AM
#4
I am confused about this part, what are you doing here
Code:
libLib = new Library;
..........
delete libLib;
libLib = reinterpret_cast<Library*>(app->GetCurrentLibrary());
............
cout << libLib->GetName() << endl; //Fails here
...............
delete libLib;
well, if return type of 'GetCurrentLibrary()' is a pointer to 'Library' you must not delete it after getting data from your app variable.
Code:
cout << libLib->GetName() << endl; //Fails here
...............
// delete libLib; /* should not be called*/
also try to use a diffrent variable ptr than 'libLib' and check out if problem still exists
Amit
-
June 3rd, 2004, 10:46 AM
#5
I just added that part to show that I can create the object using the constructor and then use getname() but I can't after getting the return value of the GetCurrentLibrary(). It doesn't work even if I take the first assignment out. Thats how I had it before, I just added for purposes of demonstrating.
I may be mistaken but I thought I could reuse the same variable to assign a new object, as long as I delete the old reference so that its memory gets freed. Its not good programming practice maybe, but it was just a quick and dirty example.
Edit
---
Either way the problem occurs when I try to use .GetName after assigning it from the GetCurrentLibrary method.
maybe I'm misunderstanding you...
Last edited by shawns; June 3rd, 2004 at 10:53 AM.
-
June 4th, 2004, 02:26 AM
#6
Well, i was just asking about the order of deletion of pointers.
As regard using same pointer after deleting it, this will not any create problems.
Amit
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
|