Hello All
Could some one describe or direct me to an article which describes how to implement interfacing with a hardware device that come with a DLL. I have a USB Device that came with a DLL. I have the list of the functions. What I need is to implement the function pointers and HINSTANCE into my C# WPF Project and call the functions through my GUI. A step by step instruction is appreciated.
The DLL is not a .NET assembly. I am using DllImport for function pointers.
At this time below is all I got. However I still need to add a C# equivlent of HINSTANCE and loadlibrary
(in C++ I do this: HINSTANCE SomeName = LoadLibrary("myDll.dll");)
to bound the control to my dll. I don't know how to achieve that in C#. The other thing I need to do is to get the Handle that the control is bound to and send it through InitTimerDll() function.
After my function pointers are set and handle (HINSTANCE) is also added I need to call these functions. Any thoughts and Ideas are welcome.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interop;
using System.Runtime.InteropServices;
namespace WpfApplication1
{
class myDLL_DLL_Interface
{
struct dll_init_params { ushort simp; ushort simt;}
[DllImport("myDll.dll")]
public static extern void InitTimerDll("Handle", ref dll_init_params InitParams);
[DllImport("myDll.dll")]
public static extern void TerminateTimerDll();
[DllImport("myDll.dll")]
public static extern void ProberOnLine(ushort i);
[DllImport("myDll.dll")]
public static extern bool StageZmove(ushort die, ushort dir);
[DllImport("myDll.dll")]
public static extern int StageZPosition();
[DllImport("myDll.dll")]
public static extern bool StageToLoad(ushort die);
[DllImport("myDll.dll")]
public static extern bool StageGoTo(ushort die, long x, long y);
[DllImport("myDll.dll")]
public static extern void SetCounterX(ushort x);
[DllImport("myDll.dll")]
public static extern void SetCounterY(ushort y);
[DllImport("myDll.dll")]
public static extern bool Contact();
[DllImport("myDll.dll")]
public static extern void StartDelayInking();
}
}
update 12/16
Here is a updated code with some modification as per SA suggestions:
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Interop;
namespace ProbeCTRL
{
static class SupertimDllInterface
{
public struct dllInitParams
{
private short simp;
private short simt;
private short UnloadDelay;
private short StartTestDelay;
private short ZStageDelay;
private short IndexDelay;
private short InterIndexDelay;
private short InkerPulse;
private short InkerDelay;
}
[DllImport("myDLL.dll")]
public static extern void InitTimerDll();
[DllImport("myDLL.dll")]
public static extern void TerminateTimerDll();
[DllImport("myDLL.dll")]
public static extern void ProberOnLine(UInt16 i);
[DllImport("myDLL.dll")]
public static extern bool StageZmove(UInt16 die, UInt16 dir);
[DllImport("myDLL.dll")]
public static extern int StageZPosition();
[DllImport("myDLL.dll")]
public static extern bool StageToLoad(UInt16 die);
[DllImport("myDLL.dll")]
public static extern bool StageGoTo(UInt16 die, long x, long y);
[DllImport("myDLL.dll")]
public static extern void SetCounterX(UInt16 x);
[DllImport("myDLL.dll")]
public static extern void SetCounterY(UInt16 y);
[DllImport("myDLL.dll")]
public static extern bool Contact();
[DllImport("myDLL.dll")]
public static extern void StartDelayInking();
}
}
Still have problem with InitTimerDll. Previously I added the c++ and VB equivalent for just this function. Here is how everything is in VB and the way function is called. Please help with the C# P/Invoke and how to call these functions properly without this error:
An unhandled exception of type 'System.AccessViolationException' occurred in ProbeCTRL.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Here is the VB version of the function pointers:
Declare Sub InitTimerDll Lib "myDLL.dll" (ByVal hWnd As Integer, ByRef InitParams As dll_init_params)
Declare Sub TerminateTimerDll Lib "myDLL.dll" ()
Declare Sub ProberOnLine Lib "myDLL.dll" (ByVal TheWord As Short)
Declare Function StageZmove Lib "myDLL.dll" (ByVal die As Short, ByVal dir_Renamed As Short) As Boolean
Declare Function StageZPosition Lib "myDLL.dll" () As Integer
Declare Function StageToLoad Lib "myDLL.dll" (ByVal die As Short) As Boolean
Declare Function StageGoTo Lib "myDLL.dll" (ByVal TheWord As Short, ByVal XPos As Integer, ByVal YPos As Integer) As Boolean
Declare Sub StageGetPosition Lib "myDLL.dll" ()
Declare Sub SetCounterX Lib "myDLL.dll" (ByRef XCount As Short)
Declare Sub SetCounterY Lib "myDLL.dll" (ByRef YCount As Short)
Declare Function Contact Lib "myDLL.dll" () As Boolean
Declare Sub StartDelayInking Lib "myDLL.dll" ()
and here is how one of the functions (InitTimerDll) is called in VB:
InitTimerDll(Me.Handle.ToInt32, InitParams)
How do I write these in C#?
I do understand that my questions are beginner and elementary but as I mentioned I am new at C# and find it to be quite amazing. So, I appreciate your patience and your help.