Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

COM conflicts with WinMain

Posted on 2004-08-17
11
Medium Priority
?
708 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

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
 

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 1425 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

660 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