Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

COM conflicts with WinMain

Posted on 2004-08-17
11
Medium Priority
?
720 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

877 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