0

I am trying to recreate a DLL function call in Python. The code uses a DLL file to set the laser parameters for various material setting. Here is the original C# code:

public bool ApplyLasFile(string strLasFile)
{
    if (!File.Exists(strLasFile))
    {
        //MessageBox.Show("File not found", "Error", MessageBoxButtons.OK);
        return false;
    }
    Char[] arr = strLasFile.ToCharArray();
    ApplyLAS(arr, strLasFile.Length);
    return true;
}

Here is my python code:

def setLaserSettings(input):
    #laserSettings(power (0-1000), speed (0-1000), height (0-4000), ppi (0-1000))
    input=input.upper()
    if(input=="ABS"):
        settings=laserSettings(825, 1000)
    elif(input=="STAINLESS"):
        settings=laserSettings(1000, 84)
    elif(input=="TITANIUM"):
        settings=laserSettings(1000, 84)
    else:
        return False
    charList=[]
    for x in range (0, len(settings)): #convert string into c_char[]
        charList.append(c_char(settings[x]))
    #print charList
    print ULSLib.ApplyLAS(charList, len(settings))

The DLL call ULSLib.ApplyLAS(charList, len(settings)) is returning the error ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1. I started out with just using list(settings) to stand in for ToCharArray(), and when that didn't work I built a c_char array per the Python ctypes man page. However, I am still getting that error. Does anybody see what I am missing? Thanks for any help you can offer.

Edit: I have also tried list(string) and list(charList) and both return the same error.

  • What is the signature of the C function `ApplyLAS`? If the first parameter is a `char *`, have you tried passing a `c_char_p` value? – Luke Woodward Jun 06 '16 at 18:30
  • Is this what you are talking about? `[DllImport("RemoteCtrl.dll", CharSet = CharSet.Unicode)]` `internal static extern bool ApplyLAS(Char[] FileName, int nLength);` This is what was at the top of the C# souce. – seeigecannon Jun 06 '16 at 18:53
  • no, that's the signature of the C# method. I was after the signature of the C function in the DLL. – Luke Woodward Jun 07 '16 at 19:30
  • Googleing `what is a dll signature` only brought up security related things. How would I go about finding the signature of the C function in the DLL? (Note: I was able to get this working, so this is purely for my own knowledge at this point.) – seeigecannon Jun 07 '16 at 21:59
  • http://stackoverflow.com/questions/386133/get-signatures-of-exported-functions-in-a-dll – Luke Woodward Jun 08 '16 at 21:09

2 Answers2

0

So this is weird. The function call requires a Char array, but putting the string strait into the function call within python seems to be getting the job done for some reason. I am new to python, so hopefully someday this will make sense to me.

0

I suspect the reason that your code works unexpectedly is that the C function you are calling in the DLL takes a char * for its first parameter. I'm guessing its declaration looks something like:

bool ApplyLAS(char *filename, int length);

I'm guessing the return type here, and it's also quite possible that the first parameter is actually a wchar_t * or the second is an unsigned int. I'm also assuming your DLL has been compiled from C code. It's possible it may have been compiled from another language such as Fortran instead.

A char * parameter, such as the one I suspect is in your C function, is typically how you pass a string to C. Python's ctypes module will pass a Python string to the C function as a char *, thereby making it easy to pass a string to C.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104