Use function from DLL file

Hi,

I'm a beginning C# programmer and I'm writing an earlier written program(c++) in C#.

I have a DLL file called K8062D.dll which has following functions:

StartDevice Opens the communication link to the K8062 device

StopDevice Closes the link to the K8062 device

SetChannelCount(Count) Sets the maximum DMX channel in use

SetData(Channel, Data)

How can I call these functions from C#

Best regards,

Yannick



Answer this question

Use function from DLL file

  • limon26

    Great thanks!
  • Spanglishone

    Michael_Grumbach wrote:

    Do I just make sure the dll file is in the same directory, or do I put a reference to it in the code somewhere

    Putting Dll in the same directory as of your application is enough and you dont need to do anything.

    Best Regards,

    Rizwan aka RizwanSharp



  • Daxez

    You are welcome :)

    Best Regards,

    Rizwan aka RizwanSharp



  • IGiberson

    I found that "USBm.dll" makes the compiler much happier.

    Thanks,

    Mike


  • mattdawg

    Copy K8062D.dll in release or bin folder where your program's output is generated then write a class like:

    using System.Runtime.InteropServices;

    namespace SomeNameSpace

    {

    public static class K8062DWrapper

    {

    [DllImport(K8062D.dll)]

    public static extern void StartDevice();

    [DllImport(K8062D.dll)]

    public static extern void StopDevice();

    [DllImport(K8062D.dll)]

    public static extern void SetChannelCount();

    [DllImport(K8062D.dll)]

    public static extern void SetData();

    }

    }

    Now you can call function of above cretaed class like:

    K8062DWrapper.StartDevice();

    etc.

    Make sure to change paramters these functions require and values they return.

    I hope this will help.

    Best Regards,

    Rizwan aka RizwanSharp



  • bluebx32

    Would you please post your code.

    Best Regards,

    Rizwan aka RizwanSharp



  • AndyL

    Hi again,

    I typed in exactly what you said to, changing the .dll name to USBm, my dll's name, ofcourse.

    And I get the message : The name 'USBm' does not exist in the current context.

    What does this mean. I put a .dll copy in my projects bin folder, and the place where the code is output to.

    Thanks,

    Mike


  • Amit Online

    I had the same question. So thanks.

    But I how do I connect the dll to my application

    Maybe I'm using the wrong words.

    Do I just make sure the dll file is in the same directory, or do I put a reference to it in the code somewhere

    Thanks,

    Mike


  • Harshal Bhakta

    Thank you very much, the information was very helpfull

     


  • werg

    Hi, use this

    [DllImport("k8062D.dll")]

    private static void StartDevice (void);



  • Ilia

    Hi again,

    New problem.

    I wrote this (for an output function):

    [DllImport("USBm.dll", EntryPoint = "USBm_WriteA")]

    public static extern void USBm_WriteA(byte deviceindex, byte data);

    and this (for an input function):

    [DllImport("USBm.dll", EntryPoint = "USBm_ReadB")]

    public static extern void USBm_ReadB(byte deviceindex, byte data);

    And this works :

    USBmWrapper.USBm_WriteA(0, Write_Data);

    But this doesn't:

    USBmWrapper.USBm_ReadB(0, Read_Data);

    Error : "AccessViolationException was unhandled" "Attempt to read or write protected memory. This is often an indication that other memory is corrupt."

    Is this just a coding error, or is my computer broken

    Thanks,

    Mike


  • Rattlerr

    Yes It must do,You have to specify the complete name/path.

    Best Regards,

    Rizwan aka RizwanSharp



  • Use function from DLL file