How to import COM object in VC6?


 I am writing a VC console program that uses a COM object
(sdk.dll). Can anybody give me an working example and how it calls a sample function? The SDK comes with the dll and also the lib file but not the header files. Do I need the .h files?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If it comes with a type library, you can use #import. See the documentation for more information.
HahoAuthor Commented:
Sorry, but it doesn't work (assuming it doesn't have a
type lib (*.lib file) and only has a DLL file.
Import is only if u are using and IDL to "import" other
IDL files.
However, I tried adding to it my project library and I can
see it now BUT I cannot compile it.
What is wrong?
Typelib is not *.lib it is *.tlb
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

You can do it with DLL only. You will import the DLL. The only condition is that your type lib should be included your DLL.
In the header file of your console app,

Example .....

#import "ConObject.dll" raw_interfaces_only, raw_native_types, no_namespace, named_guids

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Here is example of the usage in a console application. This code is just a sample from a prototype. If you need the complete example.. let me know

#import "..\Bin\Debug\SystemInfo.dll" raw_interfaces_only, raw_native_types, no_namespace, named_guids

int main(int argc, char* argv[])
      char lpMultiByteStr[128];

      cout << "Initializing COM" << endl;

      if (FAILED (CoInitialize (NULL))) {
            cout << "Failed to intialize COM" << endl;
            return -1;

// Get the container object.

      ISystemInformation *pSystemInfo = NULL;

      hr = CoCreateInstance (CLSID_SystemInformation, NULL, CLSCTX_INPROC, IID_ISystemInformation,
            (void **) &pSystemInfo);
      if (FAILED (hr)) {
            cout.setf (ios::hex, ios::basefield);
            cout << "Failed to create SystemInfo object HR = " << hr << endl;
            return -1;

      BSTR bstrMinorVersion, bstrServicePack;

      hr = pSystemInfo->GetOSInformation (&bstrPlatform, &bstrMinorVersion, &bstrServicePack,

      if (FAILED (hr)) {
            cout.setf (ios::hex, ios::basefield);
            cout << "Failed to get SystemInfo object HR = " << hr << endl;
            return -1;

      WideCharToMultiByte (CP_ACP, 0, bstrPlatform, -1, lpMultiByteStr,
            sizeof (lpMultiByteStr), NULL, NULL);
      cout << "Platform = " << lpMultiByteStr << endl;

      pSystemInfo->Release ();

      ::SysFreeString (bstrPlatform);
cout << "Sutting down COM" << endl;
      CoUninitialize ();

      return 0;
A type library may be incorporated into a DLL.
HahoAuthor Commented:
thanks, and also to chensu...
sorry for not knowing what a type lib (tlb) is....

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.