Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C COM Python
Hi,
I have written a DLL in C/C++ which uses COM internally.
 
While trying to call function in DLL, I have found out that COM is not getting initialised. I am initialising COM in C.
 
consider following example
 
IN C/C++
 
#define DLLEXPORT extern "C" __declspec(dllexport)
DLLEXPORT int Init_COM()
{
	int result = 1;
 
	//printf("\n*** INITIALIZING ***\n\n");
	HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
	if (FAILED(hr))
	{
		result = -8;
		//printf("\nERROR: COM INITIALIZATION FAILED"); 
	}
        else
        {
                //do work here
        }
        
        return result;
}
 
DLLEXPORT int some_function()
{
        CComPtr<IPortableDeviceManager> pPortableDevice;
        .
        .
        .
        //now I call COM functions first to initialise the pointer and then to do some work
        //But because COM is not initialised this will fail
        return some_val;
}
 
 
The value returned in python in -8, meaning COM initialization Failed. I am very new to python and hence don't know how to use function written in c/c++ in a DLL using COM in python! Please HELP..
 
IN Python I import the DLL
 
from ctypes import *
cdll.LoadLibrary("MY_DLL.dll") as my_dll #or import MY_DLL.dll
 
return_val = my_dll.Init_COM() #returns -8
return_val = my_dll.some_function() #wont work
 
 
I dont exactly remember python syntax so there might be some error above!
Thank You
Posted 13-Jan-12 3:08am
Edited 13-Jan-12 6:46am
v5
Comments
CPallini at 13-Jan-12 8:23am
   
How do you access the COM component from Python? In this sample code
http://www.devshed.com/c/a/Python/Windows-Programming-in-Python/2/
CoInitialize is NOT explicitely called.
VinayChoudhary99 at 13-Jan-12 11:43am
   
I have updated my question showing how I am trying to do. Kindly suggest way to make it work!
CPallini at 13-Jan-12 12:15pm
   
It looks you're correctly calling your DLL.
I guess CoInitializeEx is returning RPC_E_CHANGED_MODE (i.e. you are trying to change threading model of an already initialized COM), however the best way to know is exporting the of value of hr (or debugging to see it).
VinayChoudhary99 at 18-Jan-12 1:17am
   
It was indeed error in CoInitializeEx, which was being called as MULTITHREADED in C. I changed it to CoInitialize and it is working properly now. Many Thanks!
CPallini at 18-Jan-12 3:12am
   
You are welcome.
KarstenK at 16-Jan-12 4:17am
   
You are writing bad code in NOT returning the real result.
 
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr))
{
//printf("\nERROR: COM INITIALIZATION FAILED");
return hr;
}
 
What is the value? I guess some dlls are missed, so the COM-dll doesnt get loaded...
VinayChoudhary99 at 18-Jan-12 1:18am
   
I changed CoInitializeEx(NULL, COINIT_MULTITHREADED) to CoInitialize(NULL), and it is now working. problem seemed to be in Threading as CPallini said.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

provided by CPallini:
 
It looks you're correctly calling your DLL.
I guess CoInitializeEx is returning RPC_E_CHANGED_MODE (i.e. you are trying to change threading model of an already initialized COM), however the best way to know is exporting the of value of hr (or debugging to see it).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

It looks to me like your COM has not been initialized properly and like Vinay mentioned try exporting the value of hr or any other variable just as a test first. There are two things I feel might have gone wrong here.
 
1) Make sure all your COM port parameters are set properly, I can't see where you are initializing them. (For eg: Baud rate, data bits, parity, stop bits, Flow control etc)
 
2) Check all your connections obviously (which I'm sure you have done): which COM port, serial connection etc.
 
I'm not sure if this would help or not, but I usually use:
 
AFX_MANAGE_STATE(AfxGetStaticModuleState());
 
at the beginning of each function. (standard DLL accessing procedure for me).
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 14 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100