Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C++
// 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


I'm trying to call the LaserLib.dll in c# project
C#
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);
    }
}


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.
Posted
Updated 9-Apr-14 21:22pm
v3

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:

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

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
 
Share this answer
 
Comments
playerdaw 10-Apr-14 1:57am    
Hi Sergey thanks for the response, I've already put the MarshlAsAttribute in the string parameter, but still no luck.
Sergey Alexandrovich Kryukov 10-Apr-14 8:43am    
Hm... Could it be a problem in "LaserLib.DLL" itself, or its use? Could you make a test application in C++ doing exactly the same?
—SA
playerdaw 10-Apr-14 8:47am    
Hi Sergey, the supplier of the data collector provided the "LaserLib.dll" source together with a c++ application using the dll and it works well, if only I could attach the source here... which part of the usage do you want to see so I can reply it to you. Many thanks!
Sergey Alexandrovich Kryukov 10-Apr-14 10:54am    
I don't, just checking... could you reproduce exact same calls as you did for .NET?
—SA
Sergey Alexandrovich Kryukov 10-Apr-14 10:57am    
As to Solution 2: do target instruction-set architectures of the DLL and .NET assembly match? Do the match actual computer architecture? The problem is: in case of mismatch, no problem will be detected by the compiler (because it does not "know" what is the LaserLib.DLL), but the call will crash. Please check up. Even "AnyCPU" can be a problem (in case of the use of WoW64). So, what are those 3 architectures?
—SA
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.
 
Share this answer
 
Comments
playerdaw 10-Apr-14 3:24am    
Hi bernhard,

There is no error in loading the SmartDeviceProject1 program.

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