By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

Python Wrapper for C# Com Object

P: n/a
Hi,

I wish to write a Python wrapper for my C# COM object but am unsure
where to start. I have a dll and a tlb file, and I can use this object
in C via the following code -

// ConsolApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#import "C:\Documents and Settings\X\Mina dokument\Visual Studio
2005\Projects\X_COMObject\X_COMObject\bin\Debug\X_ COMObject.tlb"
using namespace X_COMObject;

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);

X_COMObject::XCOM_InterfacePtr p(__uuidof(X_COMObject::XCOM_Class));
XCOM_Interface *X_com_ptr ;
X_com_ptr = p ;
X_com_ptr->SetID(10);
int x = X_com_ptr->GetID();
printf("%d",x);
getchar();

return 0;
}

Can anyone offer me some tips as to how to do this in Python?

Thanks very much for your help,

Barry.

Dec 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

bg***@yahoo.com skrev:
Hi,

I wish to write a Python wrapper for my C# COM object but am unsure
where to start. I have a dll and a tlb file, and I can use this object
in C via the following code -

// ConsolApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#import "C:\Documents and Settings\X\Mina dokument\Visual Studio
2005\Projects\X_COMObject\X_COMObject\bin\Debug\X_ COMObject.tlb"
using namespace X_COMObject;

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);

X_COMObject::XCOM_InterfacePtr p(__uuidof(X_COMObject::XCOM_Class));
XCOM_Interface *X_com_ptr ;
X_com_ptr = p ;
X_com_ptr->SetID(10);
int x = X_com_ptr->GetID();
printf("%d",x);
getchar();

return 0;
}

Can anyone offer me some tips as to how to do this in Python?

Thanks very much for your help,

Barry.
This is what I've done so far, but I know I'm not doing this correctly.
Can anyone help me out?

#import pythoncom
#pythoncom.CoInitialize()

from comtypes.client import GetModule, CreateObject

module = GetModule("C:\\Documents and Settings\\X\\Mina
dokument\\Visual Studio
2005\\Projects\\X_COMObject\\X_COMObject\\bin\\Deb ug\\X_COMObject.tlb")

dir(module)

interface = module.XCOM_Interface()

dir(interface)

interface.SetID()

#pythoncom.CoUnitialize()
Traceback (most recent call last):
File "C:/Python25/test.py", line 14, in <module>
interface.SetID()
TypeError: Expected a COM this pointer as first argument

Dec 28 '06 #2

P: n/a

bg***@yahoo.com skrev:
bg***@yahoo.com skrev:
Hi,

I wish to write a Python wrapper for my C# COM object but am unsure
where to start. I have a dll and a tlb file, and I can use this object
in C via the following code -

// ConsolApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#import "C:\Documents and Settings\X\Mina dokument\Visual Studio
2005\Projects\X_COMObject\X_COMObject\bin\Debug\X_ COMObject.tlb"
using namespace X_COMObject;

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);

X_COMObject::XCOM_InterfacePtr p(__uuidof(X_COMObject::XCOM_Class));
XCOM_Interface *X_com_ptr ;
X_com_ptr = p ;
X_com_ptr->SetID(10);
int x = X_com_ptr->GetID();
printf("%d",x);
getchar();

return 0;
}

Can anyone offer me some tips as to how to do this in Python?

Thanks very much for your help,

Barry.

This is what I've done so far, but I know I'm not doing this correctly.
Can anyone help me out?

#import pythoncom
#pythoncom.CoInitialize()

from comtypes.client import GetModule, CreateObject

module = GetModule("C:\\Documents and Settings\\X\\Mina
dokument\\Visual Studio
2005\\Projects\\X_COMObject\\X_COMObject\\bin\\Deb ug\\X_COMObject.tlb")

dir(module)

interface = module.XCOM_Interface()

dir(interface)

interface.SetID()

#pythoncom.CoUnitialize()
Traceback (most recent call last):
File "C:/Python25/test.py", line 14, in <module>
interface.SetID()
TypeError: Expected a COM this pointer as first argument
Can anyone help me with this?

Thanks,

Barry.

Dec 29 '06 #3

P: n/a
Hi,

A question. Why in your C-version are your doing:

X_com_ptr->SetID(10);

and in the Python version:

interface.SetID() ?

I don't know anything of your COM object, but does SetID require a
parameter?

Duane
bg***@yahoo.com wrote:
bg***@yahoo.com skrev:
bg***@yahoo.com skrev:
Hi,
>
I wish to write a Python wrapper for my C# COM object but am unsure
where to start. I have a dll and a tlb file, and I can use this object
in C via the following code -
>
// ConsolApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#import "C:\Documents and Settings\X\Mina dokument\Visual Studio
2005\Projects\X_COMObject\X_COMObject\bin\Debug\X_ COMObject.tlb"
using namespace X_COMObject;
>
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
>
X_COMObject::XCOM_InterfacePtr p(__uuidof(X_COMObject::XCOM_Class));
XCOM_Interface *X_com_ptr ;
X_com_ptr = p ;
X_com_ptr->SetID(10);
int x = X_com_ptr->GetID();
printf("%d",x);
getchar();
>
return 0;
}
>
Can anyone offer me some tips as to how to do this in Python?
>
Thanks very much for your help,
>
Barry.
This is what I've done so far, but I know I'm not doing this correctly.
Can anyone help me out?

#import pythoncom
#pythoncom.CoInitialize()

from comtypes.client import GetModule, CreateObject

module = GetModule("C:\\Documents and Settings\\X\\Mina
dokument\\Visual Studio
2005\\Projects\\X_COMObject\\X_COMObject\\bin\\Deb ug\\X_COMObject.tlb")

dir(module)

interface = module.XCOM_Interface()

dir(interface)

interface.SetID()

#pythoncom.CoUnitialize()
Traceback (most recent call last):
File "C:/Python25/test.py", line 14, in <module>
interface.SetID()
TypeError: Expected a COM this pointer as first argument

Can anyone help me with this?

Thanks,

Barry.
Dec 29 '06 #4

P: n/a
bg***@yahoo.com schrieb:
bg***@yahoo.com skrev:
>Hi,

I wish to write a Python wrapper for my C# COM object but am unsure
where to start. I have a dll and a tlb file, and I can use this object
in C via the following code -

// ConsolApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#import "C:\Documents and Settings\X\Mina dokument\Visual Studio
2005\Projects\X_COMObject\X_COMObject\bin\Debug\X _COMObject.tlb"
using namespace X_COMObject;

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);

X_COMObject::XCOM_InterfacePtr p(__uuidof(X_COMObject::XCOM_Class));
XCOM_Interface *X_com_ptr ;
X_com_ptr = p ;
X_com_ptr->SetID(10);
int x = X_com_ptr->GetID();
printf("%d",x);
getchar();

return 0;
}

Can anyone offer me some tips as to how to do this in Python?

Thanks very much for your help,

Barry.

This is what I've done so far, but I know I'm not doing this correctly.
Can anyone help me out?

#import pythoncom
#pythoncom.CoInitialize()
The above is unneeded if you use comtypes as below (and is unneeded
when using pythoncom, as well).
from comtypes.client import GetModule, CreateObject

module = GetModule("C:\\Documents and Settings\\X\\Mina
dokument\\Visual Studio
2005\\Projects\\X_COMObject\\X_COMObject\\bin\\Deb ug\\X_COMObject.tlb")
You don't intantiate the interface, you have to instantiate the COM object.
Something like

CreateObject("XCOM_Class")

but of course you have to use the correct argument in the call - the progid
of the COM object.
Alternatively you can use the CoClass from the typelibrary, look into the
generated module in the comtypes\gen directory for a class derived from
comtypes.CoClass.

InternetExplorer, for example, can be started in these ways:

# using the progid:
ie = CreateObject("InternetExplorer.Application")

# using the clsid:
ie = CreateObject("{0002DF01-0000-0000-C000-000000000046}")
# using the coclass from the generated module:
mod = GetModule("shdocvw.dll")
ie = CreateObject(mod.InternetExplorer)

Thomas

Jan 2 '07 #5

P: n/a

Thomas Heller skrev:
bg***@yahoo.com schrieb:
bg***@yahoo.com skrev:
Hi,

I wish to write a Python wrapper for my C# COM object but am unsure
where to start. I have a dll and a tlb file, and I can use this object
in C via the following code -

// ConsolApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#import "C:\Documents and Settings\X\Mina dokument\Visual Studio
2005\Projects\X_COMObject\X_COMObject\bin\Debug\X_ COMObject.tlb"
using namespace X_COMObject;

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);

X_COMObject::XCOM_InterfacePtr p(__uuidof(X_COMObject::XCOM_Class));
XCOM_Interface *X_com_ptr ;
X_com_ptr = p ;
X_com_ptr->SetID(10);
int x = X_com_ptr->GetID();
printf("%d",x);
getchar();

return 0;
}

Can anyone offer me some tips as to how to do this in Python?

Thanks very much for your help,

Barry.
This is what I've done so far, but I know I'm not doing this correctly.
Can anyone help me out?

#import pythoncom
#pythoncom.CoInitialize()

The above is unneeded if you use comtypes as below (and is unneeded
when using pythoncom, as well).
from comtypes.client import GetModule, CreateObject

module = GetModule("C:\\Documents and Settings\\X\\Mina
dokument\\Visual Studio
2005\\Projects\\X_COMObject\\X_COMObject\\bin\\Deb ug\\X_COMObject.tlb")
You don't intantiate the interface, you have to instantiate the COM object.
Something like

CreateObject("XCOM_Class")

but of course you have to use the correct argument in the call - the progid
of the COM object.
Alternatively you can use the CoClass from the typelibrary, look into the
generated module in the comtypes\gen directory for a class derived from
comtypes.CoClass.

InternetExplorer, for example, can be started in these ways:

# using the progid:
ie = CreateObject("InternetExplorer.Application")

# using the clsid:
ie = CreateObject("{0002DF01-0000-0000-C000-000000000046}")
# using the coclass from the generated module:
mod = GetModule("shdocvw.dll")
ie = CreateObject(mod.InternetExplorer)

Thomas
Thanks very much for your help.

I tried running the following code -

import win32com.client
scanObj = win32com.client.Dispatch("X_COMObject")

but im getting the following error -

Traceback (most recent call last):
File
"C:\Python22\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" ,
line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Python25\test.py", line 20, in ?
scanObj = win32com.client.Dispatch("Scania_COMObject")
File "C:\Python22\Lib\site-packages\win32com\client\__init__.py",
line 95, in Dispatch
dispatch, userName =
dynamic._GetGoodDispatchAndUserName(dispatch,userN ame,clsctx)
File "C:\Python22\Lib\site-packages\win32com\client\dynamic.py", line
98, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python22\Lib\site-packages\win32com\client\dynamic.py", line
78, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx,
pythoncom.IID_IDispatch)
com_error: (-2147221005, 'Invalid Class String', None, None)

I ran my C++ code again and it works fine. I also checked COM Browser
and my X_COMObject is present and I'm spelling it right.

Any ideas what the problem migth be?

Thanks again,

Barry.

Jan 4 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.