473,779 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting a ValueError with comtypes

Hello,

I am working with comtypes to interface Microsoft's DirectShow library.

First, I found a Type Library on the internet that was created for
accessing DirectShow from .NET. It seems that DirectShow only comes
with IDL files and no type library.

This got me started.

The following line imports the typelibrary and automatically generates
a wrapper module for DirectShow.

ds = comtypes.client .GetModule('Dir ectShow.tlb')

Next, I can basically start with something like this:

graph = comtypes.CoCrea teInstance(CLSI D_FilterGraph, ds.IGraphBuilde r,
comtypes.CLSCTX _INPROC_SERVER)

I had to create the CLSID_FilterGra ph parameter myself by doing the
following:

CLSID_FilterGra ph =
comtypes.GUID(' {e436ebb3-524f-11ce-9f53-0020af0ba770}')

One of the first issues I ran into was that the type library I found on
the web didn't expose the IMediaControl interface. So, using the
generated wrapper as a template, I created my own wrapper:

class IMediaControl(c omtypes.IUnknow n):
_case_insensiti ve_ = True
_iid_ = comtypes.GUID(' {56A868B1-0AD4-11CE-B03A-0020AF0BA770}')
_idlflags_ = []
IMediaControl._ methods_ = [
COMMETHOD([], HRESULT, 'Run'),
COMMETHOD([], HRESULT, 'Pause'),
COMMETHOD([], HRESULT, 'Stop'),
COMMETHOD([], HRESULT, 'StopWhenReady' ),
COMMETHOD([], HRESULT, 'GetState',
(['in'], c_long, 'msTimeout'),
(['out'], POINTER(c_int), 'pfs' ))
]

This got me further. Once I defined the interface, I get access to the
interface by calling:

control = graph.QueryInte rface(IMediaCon trol)

This seemed to work. Once I got everything setup, I tried to use this
interface. For example:

control.Run()

This generates an exception:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "ds.py", line 462, in play
control.Run()
ValueError: Procedure probably called with not enough arguments (4
bytes missing)

You can get errors like this when using ctypes if you use the wrong
calling convention (CDLL versus WINDLL).

The method "Run" on the IMediaControl interface doesn't take any
arguments.

I did notice that some wrapper code uses STDMETHOD versus COMMETHOD,
but changing this didn't make any difference.

Can anyone explain what might be happening here, or offer some
suggestions?

Thanks in advance,

Jeff

Oct 5 '06 #1
3 3316
wi*********@yah oo.com schrieb:
Hello,

I am working with comtypes to interface Microsoft's DirectShow library.
Cool, another one using comtypes!
First, I found a Type Library on the internet that was created for
accessing DirectShow from .NET. It seems that DirectShow only comes
with IDL files and no type library.
What I sometimes do is to compile the IDL files into a typelib just
for creating the comtypes interface wrappers. This may be somewhat
dangerous because these typelibs should *not* be registered by accident,
so that they do not conflict with other typelibs.

Then, I found out with oleview that on my system the IMediaControl interface
is described in the 'ActiveMovie control type library (Ver 1.0)',
in c:\windows\syst em32\quartz.dll . So I was able to create the wrapper
by calling comtypes.client .GetModule("qua rtz.dll") - it seems that windows
searches the $PATH to find type libraries.
This got me started.

The following line imports the typelibrary and automatically generates
a wrapper module for DirectShow.

ds = comtypes.client .GetModule('Dir ectShow.tlb')

Next, I can basically start with something like this:

graph = comtypes.CoCrea teInstance(CLSI D_FilterGraph, ds.IGraphBuilde r,
comtypes.CLSCTX _INPROC_SERVER)

I had to create the CLSID_FilterGra ph parameter myself by doing the
following:

CLSID_FilterGra ph =
comtypes.GUID(' {e436ebb3-524f-11ce-9f53-0020af0ba770}')

One of the first issues I ran into was that the type library I found on
the web didn't expose the IMediaControl interface. So, using the
generated wrapper as a template, I created my own wrapper:

class IMediaControl(c omtypes.IUnknow n):
_case_insensiti ve_ = True
_iid_ = comtypes.GUID(' {56A868B1-0AD4-11CE-B03A-0020AF0BA770}')
_idlflags_ = []
IMediaControl._ methods_ = [
COMMETHOD([], HRESULT, 'Run'),
COMMETHOD([], HRESULT, 'Pause'),
COMMETHOD([], HRESULT, 'Stop'),
COMMETHOD([], HRESULT, 'StopWhenReady' ),
COMMETHOD([], HRESULT, 'GetState',
(['in'], c_long, 'msTimeout'),
(['out'], POINTER(c_int), 'pfs' ))
]

This got me further. Once I defined the interface, I get access to the
interface by calling:

control = graph.QueryInte rface(IMediaCon trol)

This seemed to work. Once I got everything setup, I tried to use this
interface. For example:

control.Run()

This generates an exception:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "ds.py", line 462, in play
control.Run()
ValueError: Procedure probably called with not enough arguments (4
bytes missing)

You can get errors like this when using ctypes if you use the wrong
calling convention (CDLL versus WINDLL).

The method "Run" on the IMediaControl interface doesn't take any
arguments.

I did notice that some wrapper code uses STDMETHOD versus COMMETHOD,
but changing this didn't make any difference.

Can anyone explain what might be happening here, or offer some
suggestions?
Your mistake here is that IMediaControl derives from IDispatch, not IUnknown.
So, control.Run() really calls the first IDispatch method, which is
GetTypeInfoCoun t(), which takes one argument ;-).

I'm not 100% sure the following code will work for you because I'm using
a comtypes version that's not yet committed to the repository, but it works
for me:

from comtypes.client import GetModule, CreateObject

# create and import the interface wrapper
ds = GetModule("quar tz.dll")

# FilgraphManager is what the wrapper shows as coclass,
# you can pass that to CreateObject instead of a CLSID or ProgID.
# CreateObject calls QueryInterface for the 'most useful' interface,
# so usually there's no need to specify the interface you want:
fg = CreateObject(ds .FilgraphManage r)

# this prints '0'
print fg.Run()

Thomas

Oct 5 '06 #2
Thomas,

Thanks a ton for the quick response.

I called GetModule('quar tz.dll'), and now I can at least call
IMediaControl:: Run. I get another error, but that's my problem (I
don't have the graph set correctly yet).

You mentioned that you sometimes create a type library for creating the
interface wrappers.

Would you mind sharing with me how you do this?

I seem to recall a thread on ctypes-users where you were looking for a
method to do this. I can't seem to locate what you conclusion was.

By the way, I've been using ctypes and comtypes quite a bit lately, and
I just wanted to say that they are both excellent.

Now if only I had an equivalent way to access C++ dlls, I'd no longer
have to write typemaps in SWIG.

While I'm at it, I thought I would ask you a separate completely
unrelated question.

I have a C++ library wrapped with SWIG. One member function takes a
void* to a buffer.

I want to allocate this buffer using ctypes and pass it to my SWIG
wrapper, but I've been struggling to get it to work.

I tried passing in the addressof, and then casting it back in my SWIG
wrapper, but this doesn't work. Do you have any suggestions on this?

I've also thought about using a numpy array, since this is supported in
both SWIG and ctypes.

Thanks again for your help.,

Jeff

Thomas Heller wrote:
wi*********@yah oo.com schrieb:
Hello,

I am working with comtypes to interface Microsoft's DirectShow library.

Cool, another one using comtypes!
First, I found a Type Library on the internet that was created for
accessing DirectShow from .NET. It seems that DirectShow only comes
with IDL files and no type library.

What I sometimes do is to compile the IDL files into a typelib just
for creating the comtypes interface wrappers. This may be somewhat
dangerous because these typelibs should *not* be registered by accident,
so that they do not conflict with other typelibs.

Then, I found out with oleview that on my system the IMediaControl interface
is described in the 'ActiveMovie control type library (Ver 1.0)',
in c:\windows\syst em32\quartz.dll . So I was able to create the wrapper
by calling comtypes.client .GetModule("qua rtz.dll") - it seems that windows
searches the $PATH to find type libraries.
This got me started.

The following line imports the typelibrary and automatically generates
a wrapper module for DirectShow.

ds = comtypes.client .GetModule('Dir ectShow.tlb')

Next, I can basically start with something like this:

graph = comtypes.CoCrea teInstance(CLSI D_FilterGraph, ds.IGraphBuilde r,
comtypes.CLSCTX _INPROC_SERVER)

I had to create the CLSID_FilterGra ph parameter myself by doing the
following:

CLSID_FilterGra ph =
comtypes.GUID(' {e436ebb3-524f-11ce-9f53-0020af0ba770}')

One of the first issues I ran into was that the type library I found on
the web didn't expose the IMediaControl interface. So, using the
generated wrapper as a template, I created my own wrapper:

class IMediaControl(c omtypes.IUnknow n):
_case_insensiti ve_ = True
_iid_ = comtypes.GUID(' {56A868B1-0AD4-11CE-B03A-0020AF0BA770}')
_idlflags_ = []
IMediaControl._ methods_ = [
COMMETHOD([], HRESULT, 'Run'),
COMMETHOD([], HRESULT, 'Pause'),
COMMETHOD([], HRESULT, 'Stop'),
COMMETHOD([], HRESULT, 'StopWhenReady' ),
COMMETHOD([], HRESULT, 'GetState',
(['in'], c_long, 'msTimeout'),
(['out'], POINTER(c_int), 'pfs' ))
]

This got me further. Once I defined the interface, I get access to the
interface by calling:

control = graph.QueryInte rface(IMediaCon trol)

This seemed to work. Once I got everything setup, I tried to use this
interface. For example:

control.Run()

This generates an exception:

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "ds.py", line 462, in play
control.Run()
ValueError: Procedure probably called with not enough arguments (4
bytes missing)

You can get errors like this when using ctypes if you use the wrong
calling convention (CDLL versus WINDLL).

The method "Run" on the IMediaControl interface doesn't take any
arguments.

I did notice that some wrapper code uses STDMETHOD versus COMMETHOD,
but changing this didn't make any difference.

Can anyone explain what might be happening here, or offer some
suggestions?

Your mistake here is that IMediaControl derives from IDispatch, not IUnknown.
So, control.Run() really calls the first IDispatch method, which is
GetTypeInfoCoun t(), which takes one argument ;-).

I'm not 100% sure the following code will work for you because I'm using
a comtypes version that's not yet committed to the repository, but it works
for me:

from comtypes.client import GetModule, CreateObject

# create and import the interface wrapper
ds = GetModule("quar tz.dll")

# FilgraphManager is what the wrapper shows as coclass,
# you can pass that to CreateObject instead of a CLSID or ProgID.
# CreateObject calls QueryInterface for the 'most useful' interface,
# so usually there's no need to specify the interface you want:
fg = CreateObject(ds .FilgraphManage r)

# this prints '0'
print fg.Run()

Thomas
Oct 5 '06 #3
wi*********@yah oo.com schrieb:
Thomas,

Thanks a ton for the quick response.

I called GetModule('quar tz.dll'), and now I can at least call
IMediaControl:: Run. I get another error, but that's my problem (I
don't have the graph set correctly yet).

You mentioned that you sometimes create a type library for creating the
interface wrappers.

Would you mind sharing with me how you do this?
Nothing fancy: I compile them with midl.
I seem to recall a thread on ctypes-users where you were looking for a
method to do this. I can't seem to locate what you conclusion was.

By the way, I've been using ctypes and comtypes quite a bit lately, and
I just wanted to say that they are both excellent.
Thanks!
Now if only I had an equivalent way to access C++ dlls, I'd no longer
have to write typemaps in SWIG.

While I'm at it, I thought I would ask you a separate completely
unrelated question.

I have a C++ library wrapped with SWIG. One member function takes a
void* to a buffer.

I want to allocate this buffer using ctypes and pass it to my SWIG
wrapper, but I've been struggling to get it to work.

I tried passing in the addressof, and then casting it back in my SWIG
wrapper, but this doesn't work. Do you have any suggestions on this?
Which object does the SWIG wrapper expect? Would a bufferobject suffice?
If this is the case, you could call buffer() on a ctypes object, maybe one
created with 'ctypes.create_ string_buffer(s ize_in_bytes)'. Every ctypes
type instance supports the buffer protocol.
I've also thought about using a numpy array, since this is supported in
both SWIG and ctypes.
That might also work.

Thomas

Oct 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
4273
by: Aki Niimura | last post by:
Hello everyone, I started to use pickle to store the latest user settings for the tool I wrote. It writes out a pickled text file when it terminates and it restores the settings when it starts. It worked very nicely. However, I got a ValueError when I started the tool from Unix when I previously used the tool from Windows.
4
5970
by: Bob Staheli | last post by:
The .Net DataObject class implements the COM/OLE IDataObject interface , so how do I get it. I have tried this, but it does not work : // Declare the COM/OLE IDataObject interface public interface IOleDataObject {
0
1102
by: solid.snake.84 | last post by:
In my ATL Class, I have a function with the body STDMETHODIMP CSimpleObj::Show() { AFX_MANAGE_STATE(AfxGetStaticModuleState()) // TODO: Add your implementation code here CSimpleDialog myDialog; int result = myDialog.DoModal(); MessageBox(NULL,TEXT("Live From Python"),
0
2317
by: Chris Fonnesbeck | last post by:
I have built the following unit test, observing the examples laid out in the python docs: class testMCMC(unittest.TestCase): def setUp(self): # Create an instance of the sampler self.sampler = DisasterSampler()
1
2542
by: Jeff | last post by:
How does this work, who is the person that translated ComTypes.FORMATETC.cfFormat from a long to a short? Did I miss something somewhere that said that a long in a C++ structure translates into a short? This is really bad since RegisterClipboardFormat() returns a long so you can't even use the result in the object.......
1
17260
by: alain MONTMORY | last post by:
Hello everybody, I am a newbie to python so I hope I am at the right place to expose my problem..... :-http://www.python.org/doc/2.4.2/ext/pure-embedding.html 5.3 Pure Embedding I download the code example from http://www.python.org/doc/2.4.2/ext/run-func.txt I call the file "TestOfficiel.c" and I compile it with : gcc -g -I/usr/include/python2.3/ TestOfficiel.c -o TestOfficiel -lpython2.3 -ldl all is OK (or seems to be...).
0
1264
by: Thomas Heller | last post by:
comtypes seems to gain some attention (comtypes is a pure Python, lightweight COM client and server framework, based on the ctypes Python FFI package.) I'll try to release a new version over the next days. However, I'm wondering what would be the correct list to discuss this package... - the python-win32 mailing list - the ctypes-users mailing list
2
1283
by: wcc | last post by:
Hello group, Is there a separate mailing list for comtypes? Or this is the appropriate place to post questions related to this package(from Thomas Heller)? Thanks, -- wcc
2
3914
by: nassegris | last post by:
Hello! I need to somehow extract the TypeLib ID from a COM-dll compiled using VB6. Is is possible or do I have to look it up in the registry somehow (ie by iterating over TypeLibIDs in HKEY_CLASSES_ROOT\TypeLib\ and matching the DLL path info? Please excuse any inaccuracies in this post, my understanding of COM is patchy, especially the progid/clsid/typelib part of it.
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9930
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.