none
Conversion of python script to C# RRS feed

  • Question

  • Hi all

    I am pretty new to both python and C#, but I tried to to research before I create this topic, but lets start at the beginning

    I want to transform a python script which is given to me by a vendor (no support for C# sadly) and want to transform it to C#.

    I found this very helpful page
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/c28cb528-9f9f-4c2e-a12c-1ccc1611dcd7/c-com?forum=csharpgeneral

    But still have some troubles.

    First, have my python script

    tkwinx = win32com.client.Dispatch("TK_WinX.TK_WinXCOMInterface")
    tkwinx.Tool_HL_Configure('C:\\SW27Build300.prj', 1)
    SendMessage = (u"23 03 EA 2A FB") #this message is required to get answer from ECU
    iRun = tkwinx.AutoCommStart() #Make the comunication with ECU
    iRun = tkwinx.DIAG_LL_FrameGetForPayloadHex_SD(SendMessage)
    print(iRun[1])

    So here's my attempt

    using TK_WinX;
    string[] iRun;
    string SendMessage = "23 03 EA 2A FB";
    Type TK = Type.GetTypeFromProgID("TK_WinX.TK_WinXCOMInterface");
    object TK_Win = Activator.CreateInstance(TK);
    TK.InvokeMember("Tool_HL_Configure", System.Reflection.BindingFlags.InvokeMethod, null, TK_Win, new object[] { "C:\\TK_WinX\\projects\\SW27Build300\\System_1\\SW27Build300\\SW27Build300.prj" , 1 });
    iRun = TK.InvokeMember("AutoCommStart", System.Reflection.BindingFlags.InvokeMethod, null, TK_Win, new object []);
    iRun = TK.InvokeMember("DIAG_LL_FrameGetForPayloadHex_SD", System.Reflection.BindingFlags.InvokeMethod, null, TK_Win, new object[] { SendMessage });

    First I import the dll, then initialise the return array and the command string.

    I get the type of the function, and create the object with Activator.CreateInstance.

    Then I try to call the functions, first one (Tool_HL_Configure) looks good so far, no errors, although I would like to know if I passed the arguments correctly.

    I call the next function (AutoCommStart) but this function has no argument, so i tried to leave empty, so i get an error for the square brackets of new object, error "array creation must have array size or array initializer"

    And the last function (DIAG_LL_FrameGetForPayloadHex_SD) displays an error over the whole line, error message "Cannot implicitly convert type 'object' to 'string[]' an explicit conversion exists(are you missing a cast?)"

    So I have 3 questions
    1. are the arguments in the first function passed correctly?
    2. how can i call the second function with no argument?
    3. how can I pass my command string correctly?

    Any help is highly appreciated and will return in a free beer if you come around :)


    Wednesday, June 5, 2019 12:49 PM

Answers

  • You're dealing with COM here so you should import the COM reference into your project. 

    1. Go to the project's References node in Solution Explorer.

    2. Click Add Reference (or equivalent).

    3. Go to the COM tab.

    4. Find the COM class in the list.

    5. Click Add.

    The IDE will generate a proxy type for you that you can use. To use it then just new it up.

    //Add a using statement for the COM namespace that was generated - guessing it is TK_WinX here
    using TK_WinX;
    
    //To actually use it later in your code just new up an instance, note that class name will follow whatever the COM import generated so you'll need to adjust this name
    //If you get an error about COM and an interface then remove -Class and ensure the generated reference is embedded COM (right click reference in Solution Explorer, select Properties and find the Embedded COM Type property)
    var proxy = new WinXCOMInterfaceClass();
    
    //Everything else is just like a regular .NET type
    proxy.AutoCommStart();
    
    //This looks like a byte array in your original code, perhaps  Unicode string - clarify what type it is or when you call the next method look and see what type was generated, we'll assume a Unicode string here
    var message = "23 03 EA 2A FB";
    
    //Note also that message is probably not the correct value as this would be sending what looks like a byte stream as a string and that isn't how it works - if you need to send a byte stream as a string then you need to convert it first
    var data = new byte[] { 0x23, 0x03, 0xEA, 0x2A, 0xFB };
    message = Encoding.ASCII.GetString(data);
    
    //If this fails to compile then you have a type mismatch - message needs to have the same type as the parameter - Intellisense will tell you the expected type of the parameter
    proxy.DIAG_LL_FrameGetForPayloadHex_SD(message);


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 5, 2019 1:59 PM
    Moderator

All replies

  • You're dealing with COM here so you should import the COM reference into your project. 

    1. Go to the project's References node in Solution Explorer.

    2. Click Add Reference (or equivalent).

    3. Go to the COM tab.

    4. Find the COM class in the list.

    5. Click Add.

    The IDE will generate a proxy type for you that you can use. To use it then just new it up.

    //Add a using statement for the COM namespace that was generated - guessing it is TK_WinX here
    using TK_WinX;
    
    //To actually use it later in your code just new up an instance, note that class name will follow whatever the COM import generated so you'll need to adjust this name
    //If you get an error about COM and an interface then remove -Class and ensure the generated reference is embedded COM (right click reference in Solution Explorer, select Properties and find the Embedded COM Type property)
    var proxy = new WinXCOMInterfaceClass();
    
    //Everything else is just like a regular .NET type
    proxy.AutoCommStart();
    
    //This looks like a byte array in your original code, perhaps  Unicode string - clarify what type it is or when you call the next method look and see what type was generated, we'll assume a Unicode string here
    var message = "23 03 EA 2A FB";
    
    //Note also that message is probably not the correct value as this would be sending what looks like a byte stream as a string and that isn't how it works - if you need to send a byte stream as a string then you need to convert it first
    var data = new byte[] { 0x23, 0x03, 0xEA, 0x2A, 0xFB };
    message = Encoding.ASCII.GetString(data);
    
    //If this fails to compile then you have a type mismatch - message needs to have the same type as the parameter - Intellisense will tell you the expected type of the parameter
    proxy.DIAG_LL_FrameGetForPayloadHex_SD(message);


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, June 5, 2019 1:59 PM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 10, 2019 8:28 AM
    Moderator
  • Yes it works now, thanks a lot
    free beer incoming :D
    Tuesday, June 11, 2019 10:09 AM