python 使用dll_在PYTHON中使用具有COM功能的DLL

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

解决方案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).

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值