Solved

COM conflicts with WinMain

Posted on 2004-08-17
11
668 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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 viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

744 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

13 Experts available now in Live!

Get 1:1 Help Now