Solved

COM conflicts with WinMain

Posted on 2004-08-17
11
683 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS 2010 Convert to SSIS 2013 On Different Box 3 61
eclipse formatting 6 106
Need a complete list of ASP.NET page load events 3 59
I could not set window to top 4 21
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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++.

828 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