Python Wrapper for C# Com Object

B

bg_ie

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

bg_ie

(e-mail address removed) 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\\Debug\\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
 
B

bg_ie

(e-mail address removed) skrev:
(e-mail address removed) 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\\Debug\\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.
 
T

TheSeeker

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


(e-mail address removed) skrev:
(e-mail address removed) 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\\Debug\\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.
 
T

Thomas Heller

(e-mail address removed) 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\\Debug\\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
 
B

bg_ie

Thomas Heller skrev:
(e-mail address removed) 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\\Debug\\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,userName,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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top