Accessing Hidden VB Interfaces in VC++

G

Guest

Hi,

I have a DLL written in VB 6.0. The DLL has some properties which can be set
and a collection of Names. Here is the code in VB which i am using to access
this DLL.

Public Sub Form_Load()

Dim objAddBook as New Disp.AddressBook

objAddBook.Names.Add("John","Doe")
objAddBook.Names.Add("Emily","Jones")

Msgbox "Name : " & objAddBook(1).FirstName & " " & objAddBook(1).LastName

End Sub


This piece of code works great. But, when I want to add items to this
collection in VC++, I am getting stuck. Can anyone please help me out on how
I could use this DLL in VC++. Any pointers to help me would be appreciated.

Thanks.
 
B

Brian Muth

Tom said:
Hi,

I have a DLL written in VB 6.0. The DLL has some properties which can be
set
and a collection of Names. Here is the code in VB which i am using to
access
this DLL.

Public Sub Form_Load()

Dim objAddBook as New Disp.AddressBook

objAddBook.Names.Add("John","Doe")
objAddBook.Names.Add("Emily","Jones")

Msgbox "Name : " & objAddBook(1).FirstName & " " & objAddBook(1).LastName

End Sub


This piece of code works great. But, when I want to add items to this
collection in VC++, I am getting stuck. Can anyone please help me out on
how
I could use this DLL in VC++. Any pointers to help me would be
appreciated.

Stuck where? What have you coded so far? Have you imported the DLL and used
the xxx.tlh file?

Brian
 
G

Guest

Here is what i have done so far:

I have already imported the IDL for the DLL into an IDL file and compiled it
to .H file using MIDL utlity.

ON THE CLICK EVENT OF A DIALOG I AM CALLING THE DLL AS FOLLOWS:

HRESULT hresult;
CLSID clsid1;

CoInitialize(NULL);

hresult = CLSIDFromProgID(OLESTR("Disp.AddressBook"),&clsid1);

_AddressBook *addr

hresult =
CoCreateInstance(clsid1,NULL,CLSCTX_INPROC_SERVER,__uuidof(_AddressBook),(LPVOID *) &addr);

if(FAILED(hresult))
{
AfxMessageBox("Creation Failed");
return;
}


addr->Id="100"
addr->Location="Atlanta"

/*
After this I want to enter the First & Last Name into the Names Collection.
I thought I could access the collection as follows:
addr->Names->Add("John","Doe");
But this doesnt seem to work. This is where I needed help.
*/

If you need any more info on what I am doing, Let me know and I can provide
you.
Thanks.
 
G

Guest

I am not sure, if what I am doing is correct. Possibly, you could give me
some guidelines to do it correctly in that case. Thanks for the help Brian.

Tom
 
B

Brian Muth

Check out the #import directive. Specify the DLL produced by your VB
project. This will create a xxx.tlh file in your Debug/Release folder.
You'll probably want to specify no_namespace just so you don't need to
specify the namespace all over the place in your source code.

What is very nice about this file is that it creates smart pointers for you
(they can be suppressed, although I personally prefer using them).

Your coding will then look something like:

HRESULT hr;

hr = CoInitialize(NULL);
// don't forget to check the hr value here. This doesn't always succeed.

_AddressBookPtr p; // note that VB uses "_" to prefix an interface. p is
a smart pointer

hr = p.CoCreateInstance (__uuidof (AddressBook)); // no "_" prefix here.
This is a classid

if(FAILED(hr))
{
AfxMessageBox("Creation Failed");
return;
}

HTH

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top