在c#.net中使用非托管dll [英] Using an unmanaged dll in c#.net

查看:75
本文介绍了在c#.net中使用非托管dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我刚刚使用非托管dll,现在我遇到了一个用c ++程序创建的dll,我需要在c#中引用。



dll有一个LaserLib.h文件,里面是以下代码

Hi everyone, I am just new in using unmanaged dll and now I'm stuck with a dll created in c++ program that I need to reference in c#.

The dll has a LaserLib.h file, inside it is the following code

// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the LASERLIB_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see 
// LASERLIB_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef LASERLIB_EXPORTS
#define LASERLIB_API __declspec(dllexport)
#else
#define LASERLIB_API __declspec(dllimport)
#endif

extern "C"
{
LASERLIB_API BOOL Laser_ScanInit(LPCWSTR lpDevName);
LASERLIB_API BOOL Laser_StartScan();
LASERLIB_API BOOL Laser_StopScan();
LASERLIB_API unsigned char Laser_ReadScanCode(char code[], DWORD timeout);
LASERLIB_API void Laser_ScanDeinit();
}
code





我正试图在c#项目中调用LaserLib.dll



I'm trying to call the LaserLib.dll in c# project

using System.Runtime.InteropServices;
namespace SmartDeviceProject1
{
    public partial class Form1 : Form
    {
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = test.Laser_ScanInit("COM2:").ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            test.Laser_StartScan();
            //textBox1.Text = test.Laser_ReadScanCode("256", Convert.ToUInt32(0));
        }

    }

    public static class test
    {
        [DllImport("LaserLib.dll")]

        public static extern bool Laser_ScanInit(string com2);
        public static extern bool Laser_StartScan();
        //public static extern string Laser_ReadScanCode(string code, uint timeOut);
    }
}





加载SmartDeviceProject1程序时没有错误,但是当我点击button1时会出现异常提示InvalidProgramException。如果我走在正确的轨道上,我希望有人可以指出我。在此先感谢。



There is no error in loading the SmartDeviceProject1 program, however when I click the button1, an exception will prompt "InvalidProgramException". I hope someone can point me if I'm in the right track. Thanks in advance.

推荐答案

您只调用两个P / Invoked函数,因此我只能使用Laser_ScanInit方法发现一个问题。尝试将 MarshlAsAttribute 添加到字符串参数:



You call only two P/Invoked functions, so I can spot only one problem, with the method Laser_ScanInit. Try to add the MarshlAsAttribute to the string parameter:

public static extern bool Laser_ScanInit(
    [MarshalAs(UnmanagedType.LPWStr)]
    string com2);



请参阅:

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype%28v=vs.90%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.runtime.interopserv ices.marshalasattribute%28v = vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/aa288468%28v=vs.71%29.aspx [ ^ ]。



此外,你总是可以在非托管方面遇到一些问题,所以如果代码在C ++中使用DLL,你可能想要创建一个测试原型。 />


-SA


Please see:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype%28v=vs.90%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/aa288468%28v=vs.71%29.aspx[^].

Besides, you can always have some problems on unmanaged side, so you may want to create a testing prototype if the code using the DLL in C++.

—SA


如果 Form_Load 程序,它将被64位Windows吞噬 - 你不会得到任何出错的信息。在这里添加一个try-catch块,并确保向用户显示错误。

您尝试从该dll导入多个函数,但我只看到一个DllImport属性 - 将该属性添加到每个要导入的功能。此外,您可以使用DllImport的 CallingConvention 参数。
If an exception happens in the Form_Load procedure, it will be swallowed by a 64bit Windows - you won't get any information that something went wrong. Add a try-catch block here, and make sure that you show errors to the user.
You try to import several functions from that dll, but I see only one DllImport attribute - add that attribute to every function to be imported. Additionally, you could play with the CallingConvention parameter of the DllImport.


这篇关于在c#.net中使用非托管dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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