在PYTHON中使用具有COM功能的dll [英] Using a dll having COM functions in PYTHON

查看:323
本文介绍了在PYTHON中使用具有COM功能的dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经在C/C ++中编写了一个DLL,该DLL在内部使用COM.

尝试在DLL中调用函数时,我发现COM尚未初始化.我正在用C初始化COM.

考虑以下示例

在C/C ++中

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



在-8中以python返回的值,表示COM初始化失败.我是python的新手,因此不知道如何使用python中的COM在DLL中使用c/c ++编写的函数!请帮助..

在Python中,我导入了DLL



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



我不完全记得python语法,因此上面可能存在一些错误!
谢谢



I dont exactly remember python syntax so there might be some error above!
Thank You

推荐答案

由CPallini提供:

看来您正确地调用了DLL.
我猜CoInitializeEx返回RPC_E_CHANGED_MODE(即您正在尝试更改已经初始化的COM的线程模型),但是最好的了解方法是导出hr的值(或调试以查看它).
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).


在我看来,您的COM尚未正确初始化,并且像Vinay提到的那样,请首先尝试导出hr或任何其他变量的值作为测试.我觉得这里可能发生了两件事.

1)确保正确设置了所有COM端口参数,但看不到要在何处进行初始化. (例如:波特率,数据位,奇偶校验,停止位,流控制等)

2)显然检查所有连接(我确定已完成):哪个COM端口,串行连接等.

我不确定这是否有帮助,但是我通常使用:

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());



在每个功能的开头. (对我来说是标准的DLL访问程序).



at the beginning of each function. (standard DLL accessing procedure for me).


这篇关于在PYTHON中使用具有COM功能的dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆