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.

marsh
  • 2,592
  • 5
  • 29
  • 53
vald
  • 31
  • 1
  • 2
  • 5
  • You should include the C# function declaration you are trying to call – Sayse Aug 14 '14 at 07:04
  • Possible duplicate of [How to use a C# dll in IronPython](http://stackoverflow.com/q/1200182/608639), [How to load a C# dll in python?](http://stackoverflow.com/questions/2077870/how-to-load-a-c-sharp-dll-in-python), [Use .Net (C#) dll in Python script](http://stackoverflow.com/q/5783387/608639), [How to use dll file in python](http://stackoverflow.com/q/27597047/608639), etc. – jww Dec 22 '14 at 06:45

3 Answers3

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
nevets
  • 4,631
  • 24
  • 40
  • 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 '14 at 08:32
  • the error in line: send = mydll.z (I've edited the code) – vald Aug 14 '14 at 08: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]http://stackoverflow.com/questions/1200182/how-to-use-a-c-sharp-dll-in-ironpython – nevets Aug 14 '14 at 08:51
  • what's more, after importing the class, you can use the function directly. Check https://ironpython.codeplex.com/wikipage?title=FAQ for details. – nevets Aug 14 '14 at 08: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#.

Daniel
  • 42,087
  • 4
  • 55
  • 81
0

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