How do I use Visual Basic 2005 class libraries in C\C++ environments?

Hi All,
I currently attempting to get a Visual Basic 2005 Class library to import and function in a C++\C environment. Unfortunately my experience with COM and DLLs is very limited. I was able to Create a VB Class Library that compiled and ran against my visual Basic debugger. However when it came time to write a C++ debugger I had a very difficult time finding resources and in general I am pretty stuck. I had registered the Class library as a COM object so I created a .tlb file and tried using the #import method, this failed miserably. I see the namespace, and the class and the interface for the COM but I can't seem to get a valid instance of the object when I run my C++ app.

Here is A basic Picture of my VB Class library DLL
( There is a listener thread in one of the Classes)

'Imports
Imports System.Net
Imports System.text
Imports System.Threading

'My interface
Public Interface IAutoLink
Sub init(ByVal strSettings As String)
Sub Add(ByVal strData As String)
Function RetrieveOldest() As String
Function ArrayContents() As String
Sub StartListening()
Sub StopListening()
End Interface

'The Class itself
Public Class AutoLinkClass
Implements IAutoLink
Private Shared m_Packet As Packet
Private Shared m_Queue As CircleQ
Private UDPclient As UDPcommunications
Public Sub init(ByVal strSettings As String) Implements IAutoLink.init
Public Sub Add(ByVal strData As String) Implements IAutoLink.Add
Public Function RetrieveOldest() As String Implements IAutoLink.RetrieveOldest
Public Function ArrayContents() As String Implements IAutoLink.ArrayContents
Public Sub StartListening() Implements IAutoLink.StartListening
Public Sub StopListening() Implements IAutoLink.StopListening
Public Sub Terminate()
' A bunch of internal Classes
#Region "Internal Classes"
End Class
Heres the C++ code I have that doesn't work

#include <iostream>
#include <string>
#import <mscorlib.tlb> raw_interfaces_only
#import "AutoLink.tlb" no_namespace named_guids
using namespace std;

int main(int argc, char* argv[])
{
string hold;
try{

IAutoLink *test = NULL;
CoInitialize(NULL);
LPVOID *Test2 = NULL;
_bstr_t Value;
string addValue;
string::size_type length = 0;
bool bRunning = true;
HRESULT hr;
hr = CoCreateInstance(CLSID_AutoLinkClass,NULL,CLSCTX_INPROC_SERVER,
IID_IAutoLink, Test2);
if (FAILED(hr))
{//This basically always fires
MessageBox(NULL,"Couldn't create the instance!","VB Library",MB_OK);
}

test = (IAutoLink*) Test2;
test->init("8080,192,168,10,104");

while (bRunning)
{
cout << "Options:\nA:(string)=> to add a String to the queue\nP-to pop a string from the Queue\nq- To Quit\n?";
cin >> hold;
if ((hold.find("A",0) != string::npos) || (hold.find("a",0) != string::npos ))
{
//Do an add
length = hold.length();
length = length - 2;
addValue = hold.substr(2,length);
Value = "VALUE";
}
else if (hold == "q" || hold == "Q")
{
bRunning = false;
CoUninitialize();
test->Release();
}
}//While Loop
}
catch (_com_error &ComError)
{
cout <<"COM ERROR!!!!!\n any key to continue";
cin >> hold;
}
catch (...)
{
cout<< "Other Error\n any key to continue";
cin >> hold;
}
return 1;
}

I would greatly appreciate any advice or resources on this issue .
Thanks
Christopher J

[3812 byte] By [ChristopherJ] at [2007-12-31]
# 1
What is the value of hr (in hexadecimal, if you can) after the call to CoCreateInstance?
TimothyNgMSFT at 2007-10-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
When I ran it I got 0xFFFFFFFF80070057 in Hex or -2147024809 in decimal.
ChristopherJ at 2007-10-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
The error code that you got is E_INVALIDARG. CoCreateInstance typically doesn't return E_INVALIDARG, so take a look here: http://msdn2.microsoft.com/en-us/library/ms686615.aspx and use the "inlined" CoCreateInstance code:

hresult = CoGetClassObject(rclsid, dwClsContext, NULL, IID_IClassFactory, &pCF);
hresult = pCF->CreateInstance(pUnkOuter, riid, ppvObj)
pCF->Release();

and see where the error is returned. I have a hunch its returned by CreateInstance. This will help us narrow down what the problem is.

TimothyNgMSFT at 2007-10-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
Well I triedCoGetClassObject(rclsid, dwClsContext, NULL, IID_IClassFactory, &pCF);
and it returned
E_INVALIDARG
'Heres a look at the declarations I call
IAutoLink *test = NULL;
IClassFactory* pCF;
LPVOID *Test2 = NULL;
_bstr_t Value;
string addValue;
string::size_type length = 0;
bool bRunning = true;
HRESULT hr;
CoInitialize(NULL);
hr = CoGetClassObject(CLSID_AutoLinkClass,CLSCTX_INPROC_SERVER, NULL,IID_IAutoLink, Test2);
It then reports back invalid argument, at any rate I typecast the Test2 to a pCF and try CreateInstance I then get an unhandeled exception telling me it encountered a reading violation at 0x00000000(I assume something important is null) . im not really sure what the Problem is, any thoughts.

ChristopherJ at 2007-10-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
Hmm, that's strange. Did you run regasm on your DLL that you built in VB? Also, trying using CLSCTX_ALL and see what happens.
TimothyNgMSFT at 2007-10-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
I did run regasm, and it registered it successfully I tried CLSCTX_ALL, that didn't help either. I do have a couple thoughts. One I do import two .Net libraries in my DLL do I need to do anything special to the DLL to allow the .Net libraries? Two is it possible my GUID for my class or the interface is invalid(I am not explicitly defining either)?
ChristopherJ at 2007-10-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
Hey there, I don't think you need to do anything special to the imports for your DLL.

Can you try the following? Using the C++ debugger, can you identify what the CLSID is that you are asking for, and also what the IID is (the GUID).

When you get the GUID, can you do a search in your registry:

HK_CLASSES_ROOT\CLSID

see if its registered there?

I'm also assuming that you have VC++ installed; there's a tool that comes with C++ called "oleview.exe" which you can use to browse your object. Try to search for your VB object in oleview, and see if you can "view" it (the process of viewing the object using oleview creates it, so if oleview can create it, then everything is good).

Give those a shot, and let me know what you find - i hope we can get to the bottom of this!

TimothyNgMSFT at 2007-10-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8
The Class GUID was registered and the interface ID seem valid, however I couldn't find the Interface ID in the registry. I opened up oleview.exe, it was registered correctly under dot net Components. I assume oleview has been around for a while because its pretty buggy at least on my machine. but I was able to get some interesting(not useful at this point) info.
One- I cant create and instance of my DLL using oleview.exe,fortunately it seems my DLL isn't the only one to cause OLE to crash completely.
Two-
My Registry tab was different then the norm the tree normally started like this
CLSID = {GUID}= description
Mine however had two entries for this the first looked like this
CLSID = {GUID}[<No Name>] = Autolink.autolinkClass
the Second line was this
{GUID}[APPID] = {GUID}
The second line was not present in the other dlls that I looked at comparision. I also didnt see any other References to APPID in any other dlls so I believe that might be something making mine fail under C++. do you have any thoughts?

Thanks
Christopher

ChristopherJ at 2007-10-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9
I have learned that my file requires the APPID and its not the source of my problem. I did some more research and add strong Naming. After I did this I tested the library on VB6 application and got it to work. Strangely However my VC application still refuses to work so I believe the problem lays with my C++ code. However I not really sure what I did wrong or whats required for a COM to run in C++. Are they any obvious pitfalls I may be missing?
Thanks
Christopher J
ChristopherJ at 2007-10-9 > top of Msdn Tech,Visual Basic,Visual Basic General...