C#进行平台互调用总是很麻烦,不像C++调用的使用,人家厂商把.h文件都写好了,C#中的函数声明得自己来写,差一点都不行。不少人走了不少弯路,甚至对用.net来做工控程序界面失去了信心。为了节省广大开发人员的时间,将我写的一组好使好使的DllImport共享给大家。
下面给出C#调用PAMC运动控制卡中的pcomm32.dll时的用到的常用函数声明的数据类型转换后的形式
public class PMAC
{
/// <summary>
/// This function opens a channel for the program to use the PMAC driver
/// BOOL OpenPmacDevice(DWORD dwDevice);
/// </summary>
/// <param name="dwDevice">Device number to open</param>
/// <returns>True if successful</returns>
[DllImport("pcomm32.dll")]
public static extern bool OpenPmacDevice(uint dwDevice);
/// <summary>
/// This function closes the channel from your program to the PMAC driver
/// BOOL ClosePmacDevice(DWORD dwDevice);
/// </summary>
/// <param name="dwDevice">Device number to close</param>
/// <returns>True if successful</returns>
[DllImport("pcomm32.dll")]
public static extern bool ClosePmacDevice(uint dwDevice);
/// <summary>
/// Provides a way to select and configure currently installed PMAC Devices
/// long PmacSelect( HWND hwnd );
/// </summary>
/// <param name="hWnd">Handle to parent window for device configuration dialog</param>
/// <returns>
/// >= 0 and <= 7 : Device selected
/// -1 or FFFFFFFF : User aborted with Cancel button.
/// </returns>
[DllImport("pcomm32.dll")]
public static extern int PmacSelect(uint hWnd);
/// <summary>
/// Sends a string buffer to PMAC and flushes out any response from PMAC
/// void PmacSendCommandA(DWORD dwDevice,PCHAR command)
/// </summary>
/// <param name="dwDevice">Device number</param>
/// <param name="command">Pointer to NULL terminated string sent to PMAC</param>
[DllImport("pcomm32.dll")]
public static extern void PmacSendCommandA(uint dwDevice, string command);
/// <summary>
/// Most if not all of the communication with the PMAC can be handled
/// long PmacGetResponseA(DWORD dwDevice,PCHAR response,UINT maxchar,PCHAR command);
/// </summary>
/// <param name="dwDevice">Device number</param>
/// <param name="reponse">Pointer to string buffer to copy the PMAC’s response into</param>
/// <param name="maxchar">Maximum characters to copy</param>
/// <param name="command">Pointer to NULL terminated string to be sent to the PMAC as a question/command</param>
/// <returns>
/// The upper byte contains the status of the call, whereas all lower bytes contain the number of characters
/// received from PMAC. If no characters were received from PMAC, check the upper bytes status code for
/// a potential error code. See the Error Handling - ASCII Communication section for a detailed explanation.
///
/// If successful, this function returns the number of characters received, including handshake characters.
/// Otherwise FALSE (0) which implies of course that an error occurred, or no characters were received
/// since PMAC was not required to respond.
/// </returns>
[DllImport("pcomm32.dll")]
public static extern int PmacGetResponseA(uint dwDevice, byte[] reponse, uint maxchar, string command);
[DllImport("pcomm32.dll")]
public static extern int PmacGetBufferA(uint dwDevice, byte[] reponse, uint maxchar);
[DllImport("pcomm32.dll")]
public static extern int PmacSendLineA(uint dwDevice, string command);
}