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);
}