Solved

COM conflicts with WinMain

Posted on 2004-08-17
11
673 Views
Last Modified: 2013-12-14
I first much say that I dont have much experience with COM though this has certainly given me alot of exposure to its processes.

I have a dll that I want to interface with, easy right?  If I create a basic Console app in Visual Studio( i.e. setting /SUBSYSTEM:CONSOLE ) I can perform the CoInitializeEx(...) and CoCreateInstance( ... ) just fine.  But if I change my small little app to a windows app( i.e. setting /SUBSYSTEM:WINDOWS and naming my main - WinMain ) then the CoInitializeEx(...) works, but the CoCreateInstance(...) doesn't.

I have no idea what is going on, I havent found much help on the internet since most people either use console apps, or MFC/VB apps but I am limited to only using Win32 directly.

Thanks alot in advance.
0
Comment
Question by:wygar
  • 6
  • 4
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11824863
That should work just fine. Which HRESULT value is returned by 'CoCreateInstance()' when it fails?
0
 

Author Comment

by:wygar
ID: 11824918
E_NOINTERFACE
0
 
LVL 86

Expert Comment

by:jkr
ID: 11824942
That should also happen in a console app then. How are you calling 'CoInitializeEx()'?
0
 

Author Comment

by:wygar
ID: 11824961
     HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
      if (FAILED(hr))
      {
            printf("CoInitialize failed\n");
            return 0;
      }

      // Instantiate Greet object, obtaining interface pointer
      IMaya* pMaya;
      hr = CoCreateInstance(CLSID_MayaCOMBridge, NULL, CLSCTX_SERVER,
                        IID_IMaya, (void **) &pMaya);
      if (FAILED(hr))
      {
            printf("CoCreateInstance failed\n");
            return 0;
      }

      pMaya->Release();

That is the full source that i am using in both instances.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11825040
First of all, get rid of the 'printf()' statements, you won't see them anyway. What are the compiler/linker settings for both cases?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:wygar
ID: 11825120
They were in there for the console, but I actually just verified if it was working or not by using the debugger.

WinMain:
Compiler:
/Od /I "D:\football\rztools\src\lib\MayaComBridge\MayaCOMBridge" /I "D:\football\rztools\src\lib\Win32FrameWork" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /Gm /EHsc /RTC1 /MDd /GS /Fo"Debug/" /Fd"Debug/vc70.pdb" /W3 /nologo /c /Wp64 /Zi /TP

Linker:
/OUT:"Debug/TestCOM.exe" /INCREMENTAL /NOLOGO /LIBPATH:"D:\football\rztools\src\lib\Win32FrameWork" /DEBUG /PDB:"Debug/TestCOM.pdb" /SUBSYSTEM:WINDOWS /MACHINE:X86 Win32FrameWork.lib comctl32.lib  kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib \football\rztools\src\lib\Win32FrameWork\Debug\Win32FrameWork.lib

Console:
Compiler:
/Od /I "D:\football\rztools\src\lib\MayaComBridge\MayaCOMBridge" /I "D:\football\rztools\src\lib\Win32FrameWork" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /Gm /EHsc /RTC1 /MDd /GS /Fo"Debug/" /Fd"Debug/vc70.pdb" /W3 /nologo /c /Wp64 /Zi /TP

Linker:
/OUT:"Debug/TestCOM.exe" /INCREMENTAL /NOLOGO /LIBPATH:"D:\football\rztools\src\lib\Win32FrameWork" /DEBUG /PDB:"Debug/TestCOM.pdb" /SUBSYSTEM:CONSOLE /MACHINE:X86 Win32FrameWork.lib comctl32.lib  kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib \football\rztools\src\lib\Win32FrameWork\Debug\Win32FrameWork.lib

0
 

Author Comment

by:wygar
ID: 11826389
I am bumping up the points since I would really like to have this fixed soon.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11826771
Hmm, since I have no clue so far what it be, I'll just keep on asking: In which context are you calling the above code?
0
 

Author Comment

by:wygar
ID: 11826868
Well, I am going to be using this COM object in a much larger program and attempted to do so until I ran into this problem.

I have since then narrowed the problem down to this very small example which I believe will, when solved, provide me with the answer to making the COM interface work in my program.  The COM interface is for talking with Maya to do some exporting features.
This connection with COM was/is being done in Perl using the Win32::OLE module and it works perfectly fine.  I dont understand where the difference is.

I will post the source code for my small program since it isnt that long:  I alternate between commenting out the WinMain and the regular main while at the same time changing the compiler option as stated above.  This is being run on a Windows 2000 machine with Visual Studio .net 2003.

----- BEGIN SOURCE CODE -------

#define WIN32_LEAN_AND_MEAN
#define _WIN32_DCOM

#include <stdio.h>
#include <objbase.h>
#include "MayaCOMBridge.h"
#include "MayaCOMBridge_i.c"
#include <comdef.h>
#include "commctrl.h"

LRESULT CALLBACK MainEventHandler( HWND window, UINT msg, WPARAM wparam, LPARAM lparam );

//int WINAPI WinMain( HINSTANCE instance, HINSTANCE, LPSTR, int )
//{
//      // Initialize COM
//      HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
//      if (FAILED(hr))
//      {
//            printf("CoInitialize failed\n");
//            return 0;
//      }
//
//      // Instantiate Greet object, obtaining interface pointer
//      IMaya* pMaya;
//      hr = CoCreateInstance(CLSID_MayaCOMBridge, NULL, CLSCTX_SERVER,
//                        IID_IMaya, (void **) &pMaya);
//      if (FAILED(hr))
//      {
//            printf("CoCreateInstance failed\n");
//            return 0;
//      }
//
//      pMaya->Release();
//
//      return 1;
//}
//
//
//LRESULT CALLBACK MainEventHandler( HWND window, UINT msg, WPARAM wparam, LPARAM lparam )
//{
//      return 0;
//}
int main(int argc, char* argv[])
{
      // Initialize COM
      HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
      if (FAILED(hr))
      {
            printf("CoInitialize failed\n");
            return 0;
      }

      // Instantiate Greet object, obtaining interface pointer
      IMaya* pMaya;
      hr = CoCreateInstance(CLSID_MayaCOMBridge, NULL, CLSCTX_SERVER,
                        IID_IMaya, (void **) &pMaya);
      if (FAILED(hr))
      {
            printf("CoCreateInstance failed\n");
            return 0;
      }

      pMaya->Release();
      return 0;
}

-------- END SOURCE CODE -------------
0
 
LVL 4

Accepted Solution

by:
anthony_w earned 475 total points
ID: 11829016
In your actual code above, you specifiy COINIT_MULTITHREADED in WinMain, but COINIT_APARTMENTTHREADED in main. Try using COINIT_APARTMENTTHREADED in WinMain as well.
0
 

Author Comment

by:wygar
ID: 11835147
Haha, great find.  Hows that for an easy 475 points.
Now if anyone knows why the COINIT_MULTITHREADED wont work in the WinMain, but COINIT_APARTMENTTHREADED does I would be really curious.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Should CArray be used for a list of pointers in C++? 19 97
How to convert MFC::CString to UTF8 wchar_t* 10 141
Best book to learn C++ 4 70
Angular JS Route 3 47
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now