I am trying to use a DLL that was writen in C++ but my application is in C#
The DLL is from another company but they have supplied an SDK for their software.
They give an example of how to load their DLL in C++ but I need to adapt it to C#.
Below is their instructions of how to do it in C++
MarkEzd.dll file is Dynamic Link Library.
MarkEzdDll.h is header file of the exports function in MarkEzd.dll
The calling way of MarkEzd.dll is explicitly link. Developer needs to load and free MarkEzd.dll by calling Windows API function.
The steps are as follows.
Call Windows’ API function LoadLibrary() to load DLL dynamically;
Call Windows’ API function GetProcAddrress() to get the pointer of the functions in the DLL and use the function pointer to finish the work;
Call Windows’ API function FreeLibrary() to release library when you do not use DLL or the program ends.
Below is the example they have provided.
Step 2. Program software for calling markezd.dll. a) First step : Dynamic Load MarkEzd.dll
HINSTANCE hEzdDLL = LoadLibrary(_T("MarkEzd.dll"));
b) Second step: get the pointer of the function to be called
lmc1_Initial=(LMC1_INITIAL)GetProcAddress(hEzdDLL, _T("lmc1_Initial"));
lmc1_Close=(LMC1_CLOSE)GetProcAddress(hEzdDLL, _T("lmc1_Close"));
lmc1_LoadEzdFile=(LMC1_LOADEZDFILE)GetProcAddress(hEzdDLL,_T("lmc1_LoadEzdFile"));
lmc1_Mark=(LMC1_MARK)GetProcAddress(hEzdDLL,_T("lmc1_Mark"));
c) Third step: Call the function
1) Initialization lmc1 board: lmc1_Initial()
.
2) Open test.ezd: lmc1_LoadEzdFile(_T(“test.ezd”))
.
3) Call lmc1_Mark() for machining: lmc1_Mark()
.
4) Close lmc1 board: lmc1_Close()
.
d) Fourth step, Release markezd.dll: FreeLibrary(hEzdDLL)
Bellow is the descriptions of the commands.
lmc1_Initial
INTENTION: initialize lmc1 control board
DEFINITION: int lmc1_Initial(TCHAR* strEzCadPath, BOOL bTestMode, HWND hOwenWnd)
strEzCadPath: the full path where ezcad2.exe exists
bTestMode Whether in test mode or not
hOwenWnd: The window that has the focus. It is used to check the user’s stop messages.
DESCRIPTION: you must first call lmc1¬_Initial before other function in program.
RETURN VALUE: common error code
lmc1_Close
INTENTION: Close lmc1 board
DEFINITION: int lmc1_Close();
DESCRIPTION: you must call lmc1_Close to close the lmc1 board when exit program.
RETURN VALUE: common error code
lmc1_LoadEzdFile
INTENTION: open the appointed ezd file, and clear all the object in database.
DEFINITION: int lmc1_LoadEzdFile(TCHAR* strFileName);
DESCRIPTION: this function can open an ezd file that was build by user as a template. User need not set process parameters, because they will be loaded in from the template file.
RETURN VALUE: common error code
lmc1_Mark
INTENTION: mark all the data in database
DEFINITION: int lmc1_Mark(BOOL bFlyMark);
bFlyMark= TRUE // mark on fly
DISCRIPTION: Begin to mark by calling this function after loading ezd file using lmc1_LoadEzdFile. The function will not return back until marking complete.
RETURN VALUE: common error code
They also explain how to set up VS6.0
most of the functions return an integer code of 0 for success.
Would this be correct?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Start_Mark
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
[DllImport("MarkEzd.dll")]
[return: MarshalAs(UnmanagedType.I2)]
public static extern int lmc1_Initial(string strEzCadPath, bool bTestMode, IntPtr hOwenWnd);
[DllImport("MarkEzd.dll")]
[return: MarshalAs(UnmanagedType.I2)]
public static extern int lmc1_Close();
[DllImport("MarkEzd.dll")]
[return: MarshalAs(UnmanagedType.I2)]
public static extern int lmc1_LoadEzdFile(string strFileName);
[DllImport("MarkEzd.dll")]
[return: MarshalAs(UnmanagedType.I2)]
public static extern int lmc1_Mark(bool bFlyMark);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr hEzdDLL = LoadLibrary("MarkEzd.dll");
IntPtr iplmc1_Initial = GetProcAddress(hEzdDLL, "lmc1_Initial");
IntPtr iplmc1_Close = GetProcAddress(hEzdDLL, "lmc1_Close");
IntPtr iplmc1_LoadEzdFile = GetProcAddress(hEzdDLL, "lmc1_LoadEzdFile");
IntPtr iplmc1_Mark = GetProcAddress(hEzdDLL, "lmc1_Mark");
int intlmc1_Initial=lmc1_Initial("c:\temp", false, hEzdDLL);
if (intlmc1_Initial > 0)
{
return;
}
int intlmc1_LoadEzdFile = lmc1_LoadEzdFile("c:\temp\test.ezd");
if (intlmc1_LoadEzdFile > 0)
{
return;
}
int intlmc1_Mark = lmc1_Mark(true);
if (intlmc1_Mark > 0)
{
return;
}
int intlmc1_Close = lmc1_Close();
if (intlmc1_Close > 0)
{
return;
}
FreeLibrary(hEzdDLL);
}
}
}
The correct syntax is as follows.
using System;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Company.Group
{
public class FuncList
{
[DllImport("MarkEzd.dll", EntryPoint = "lmc1_Initial2", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern int Initialize(string PathName, bool TestMode);
}
}
Use P-Invoke to call native DLL. You might have to marshall some datatype in order to make it work.
http://msdn.microsoft.com/en-us/library/aa288468.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With