Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

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

}
}

C#



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:

VB
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.
Posted
Updated 9-Dec-11 19:32pm
v3
Comments
Abhinav S 9-Dec-11 22:51pm    
Code blocks added.

You can consider doing a mixed-mode C++ DLL if you are unable or it hard to do what you want using only C#.

You will then be able to use existing DLL more easily and since you are compiling that new DLL as a mixed-mode library, you can relatively easily make a .NET friendly interface for your C# WPF application.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Dec-11 23:32pm    
It would be all correct, but you don't know the history of the problem. This is actually a re-post by OP who probably have some trouble understanding MSDN, Wikipedia and other articles and CodeProject answers; the problems are on a very elementary level. I already advised this way along with P/Invoke; it did not help yet. As for now, why mixed-mode and another language it OP has already mostly completed P/Invoke (with mistakes)?

The problems are very different. I'm trying to help, please see my solution.
--SA
No! Why did not you read references I gave you in my answer to your previous question?

This is no HINSTANCE or anything like that. You don't need it. You don't need to load DLL at all. All you need is to have the DLL during run-time only in the output directory or in the system %PATH%. It will be loaded with your application thanks to DllImport; you don't need a code for loading.

You already have some code. It has two mistakes. The method InitTimerDll does not look like a method declaration, because you try to write first parameter as an immediate string constant, but it should be a type. Let's say it's string (by default, string is marshaled as char*). Fix it, otherwise the code won't even compile.

Also, the class myDLL_DLL_Interface should be static; and give it a semantic name, capitalized, without underscores, to meed Microsoft naming conventions. Also, declare "myDll.dll" a private constant, remove all immediate constants. Also, why methods are static? Do you plan to use them from the other assembly? If not, they should be internal. The fixed are advise in this paragraph are not critical though, it's just for better supportability and culture.

That's it! Include this file in your project and use call those method where you need them.

Next time, please don't re-post your question. You should have used the page of your previous question and add your code and follow-up questions using "Improve question".

Good luck,
—SA
 
Share this answer
 
v2
Comments
Member 8456258 10-Dec-11 0:39am    
Thanks SA
as a mater of fact InitTimerDll is a major problem for me. Here are the VB and C++ Version:
C++:
Collapse | Copy Code

typedef void (WINAPI* LPInitTimerDll)(HWND, struct dll_init_params*);
LPInitTimerDll InitTimerDll;
HINSTANCE hDll = LoadLibrary("supertim.dll");
InitTimerDll = (LPInitTimerDll)GetProcAddress(hDll, "InitTimerDll");

VB:
Collapse | Copy Code

Declare Sub InitTimerDll Lib "supertim.dll" (ByVal hWnd As Integer, ByRef InitParams As dll_init_params)


Looking at these two codes can you tell me what variables and type I have to pass on.
Thanks,
B
Member 8456258 10-Dec-11 1:34am    
Sa, I modified my question with your input, please read the updates and comment. Thanks for your help and Patience.
Member 8456258 10-Dec-11 1:34am    
SA, I modified my question with your input, please read the updates and comment. Thanks for your help and Patience.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900