Click here to Skip to main content
15,891,896 members

Using a dll having COM functions in PYTHON

VinayChoudhary99 asked:

Open original thread
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++
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

JavaScript
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
Tags: C++, C, Python, COM

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900