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
#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
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();
}
}
public static class test
{
[DllImport("LaserLib.dll")]
public static extern bool Laser_ScanInit(string com2);
public static extern bool Laser_StartScan();
}
}
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.