Click here to Skip to main content
12,501,692 members (54,899 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# WPF USB GUI
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.
Posted 8-Dec-11 16:15pm
Comments
SAKryukov 9-Dec-11 0:04am
   
What do you mean by the DLL? Is it a .NET assembly or not? (I'm afraid not.)
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

It looks like this is not a .NET assembly but a native (unmanaged) DLL. There are no "steps" to help you.

You need to have profiles of all functions and learn P/Invoke. Start from here:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].

Read this CodeProject article: Essential P/Invoke[^].

Doing it for your API can be very easy or very difficult. It depends on complications like incompatible types, callbacks, etc. The problems can range from trivial translation into C# to creation of custom marshaller.

There is an alternative approach which is especially good if you have a static link library. You can create a mixed-mode (managed+unmanaged) C++/CLI DLL. You can mix C++ with C++/CLI and wrap all native calls to your native DLL into .NET C++/CLI "ref" classes. The obtained executable can be used like a regular .NET assembly and referenced by your C# assembly.

—SA
  Permalink  
v2
Comments
losmac 11-May-12 12:08pm
   
+5
SAKryukov 11-May-12 13:56pm
   
Thank you.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Thanks for your respond SAKryukov. 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++ it is :HINSTANCE hDll = LoadLibrary("HINSTANCE hDll = LoadLibrary("supertim.dll");.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. I appreciate your help.

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();


}
}
  Permalink  
Comments
losmac 11-May-12 12:07pm
   
I belive that isn't an answer. If you need to update your question, use "Improve question" button. Please, remove this "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 11 May 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100