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
4
  • I've downloaded ironP and now i have errors in code,can u lpease help me to rewright this code from py32 to IPython?
    – vald
    Aug 14, 2014 at 8:32
  • the error in line: send = mydll.z (I've edited the code)
    – vald
    Aug 14, 2014 at 8:38
  • 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/…
    – nevets
    Aug 14, 2014 at 8:51
  • what's more, after importing the class, you can use the function directly. Check ironpython.codeplex.com/wikipage?title=FAQ for details.
    – nevets
    Aug 14, 2014 at 8:57
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#.

0

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.