0

I need to use a function in Python(32) from dll written in C#. I use ctypes, but i got the error message:'can't find z function'. The name of the funct i need is 'z' an lib name is "ledscrcons.dll". I've checked it from another app(C#) and this library works good, but python doesn't see it. I have no idea what is the problem?? Here is the code of PScript:

import sys
import string
from time import gmtime, strftime
from array import *
from ctypes import  *
import ctypes
import clr

def SendStr(s, port):
    mydll = clr.AddReference('ledscrcons.dll')
    from ledscrcons import Class1
    send = mydll.z
    mydll.z.argtypes = [ctypes.c_char_p, ctypes.c_int]
    mydll.z.restype  = c_int
    st = s.encode('cp1251')
    i=2
    count = 0
    critcnt = 1
    while i!=0 and count<critcnt:
        i=send(c_char_p(st), c_int(port))
        if i==2 :
            print(str(i) + "dd")
        if i==1 :
            print(str(i) + 'dd')
        if i==0 :
            print('t')
        count = count + 1
        if count==critcnt:
            if i==1:
                print('kk')
            if i==2:
                print('uu')
    return i

Please,any help would be usefull.

2

3 Answers 3

1

I guess the library you used is not Com Visible. You can make it Com Visible by setting attribute:

[ComVisible(true)]

You can also try IronPython, which is a .net implementation of Python. In IronPython, simply do

import clr
clr.AddReference('YourAssemblyName')
from YourNameSpace import YourClassName
Sign up to request clarification or add additional context in comments.

4 Comments

I've downloaded ironP and now i have errors in code,can u lpease help me to rewright this code from py32 to IPython?
the error in line: send = mydll.z (I've edited the code)
I guess it's because the script could not locate the .dll. Try adding the path to that .dll to your sys.path. Check this [link]stackoverflow.com/questions/1200182/…
what's more, after importing the class, you can use the function directly. Check ironpython.codeplex.com/wikipage?title=FAQ for details.
0

C#-DLLs are not native executables but need the .NET-Runtime library. So you cannot use them with native Python executables. You have to switch to IronPython which is a python implementation in C#.

Comments

0

You cannot use ctypes to access a .NET assembly. I would recommend using IronPython or Python .NET

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.