在C#WPF中没有.Net Assembly DLL实现 [英] none .Net Assembly DLL implementation in C# WPF

查看:102
本文介绍了在C#WPF中没有.Net Assembly DLL实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
有人可以描述一下,也可以引导我进入一篇文章,该文章描述如何实现与DLL附带的硬件设备的接口.我有一个DLL附带的USB设备.我有功能列表.我需要在C#WPF项目中实现函数指针和HINSTANCE,并通过GUI调用函数.感谢您逐步指导.
DLL不是.NET程序集.我正在使用DllImport作为函数指针.
目前,这是我所能得到的.但是我仍然需要添加HINSTANCE和loadlibrary的C#等价物.
(在C ++中,我这样做:HINSTANCE SomeName = LoadLibrary("myDll.dll");)
将控件绑定到我的dll.我不知道如何在C#中实现这一目标.我需要做的另一件事是获取控件绑定到的Handle并通过InitTimerDll()函数发送它.
在设置了函数指针并添加了句柄(HINSTANCE)之后,我需要调用这些函数.任何想法都可以接受.

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

}
}



更新12/16

以下是根据SA建议进行一些修改的更新代码:



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

}
}





InitTimerDll仍然有问题.以前,我仅为此功能添加了c ++和VB等效项.这就是VB中所有内容以及调用函数的方式.请帮助C#P/Invoke,以及如何正确调用这些函数而不会出现此错误:



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.



这是函数指针的VB版本:



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



这是在VB中如何调用函数之一(InitTimerDll):



and here is how one of the functions (InitTimerDll) is called in VB:

InitTimerDll(Me.Handle.ToInt32, InitParams)



如何用C#编写这些代码?

我确实知道我的问题是初学者和初级的,但是正如我提到的,我是C#的新手,并且发现它很棒.因此,感谢您的耐心配合和帮助.



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.

推荐答案

如果您无法执行或难以使用自己想要的功能,则可以考虑使用混合模式的C ++ DLL.仅C#.

然后,您将能够更轻松地使用现有DLL,并且由于将新DLL编译为混合模式库,因此可以相对轻松地为C#WPF应用程序创建.NET友好界面.
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.


不!为什么您没有阅读我在回答上一个问题时给您的参考文献?

这不是HINSTANCE或类似的东西.您不需要它.您根本不需要加载DLL.您只需要在输出目录或系统%PATH%中将DLL 仅在运行时提供.借助DllImport,它将随您的应用程序一起加载;您不需要加载代码.

您已经有一些代码.它有两个错误.方法InitTimerDll看起来不像方法声明,因为您尝试将第一个参数写为立即字符串常量,但是它应该是类型.假设它是字符串(默认情况下,string被编组为char*).修复它,否则代码甚至无法编译.

同样,类myDLL_DLL_Interface应该是静态的.并为其赋予一个语义名称,该名称要大写(不带下划线),以符合Microsoft的命名约定.另外,声明"myDll.dll"为私有常量,删除所有立即数.另外,为什么方法是静态的?您打算从其他程序集中使用它们吗?如果没有,它们应该是内部的.虽然本段中的固定建议并不重要,但这只是为了获得更好的可支持性和文化.

而已!将此文件包含在您的项目中,并在需要它们的地方使用调用这些方法.

下次,请不要重新发布您的问题.您应该已经使用了上一个问题的页面,并使用改善问题"添加了代码和后续问题.

祝你好运,
—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


这篇关于在C#WPF中没有.Net Assembly DLL实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆