如何将函数从c ++ .dll导入c sharp

时间:2011-06-01 10:52:07

标签: c# c++ dll

在C ++中我有这个功能并使用它如下。如何在c sharp中编码?

birdRS232WakeUp(int nGroupID, BOOL bStandAlone, int nNumDevices,
WORD *pwComport, DWORD dwBaudRate, DWORD dwReadTimeout,DWORD dwWriteTimeout,int nGroupMode       
= GMS_GROUP_MODE_ALWAYS);

在手册中声明“pwComport”指向一个单词数组,每个单词都是附加到其中一个鸟类的comport的编号(例如,COM1 = 1,COM2 = 2等)

WORD COM_port[5] = {0,15,0,0,0}

if ((!birdRS232WakeUp(GROUP_ID,
    FALSE, // Not stand-alone
    DEVCOUNT, // Number of Devices
    COM_port, // COM Port
    BAUD_RATE, // BAUD
    READ_TIMEOUT,WRITE_TIMEOUT, // Reponses timeouts
    GMS_GROUP_MODE_ALWAYS))) 
{
    printf("Can't Wake Up Flock!\n");
    Sleep(3000);
    exit(-1);
}

这就是我在c sharp中的表现。

[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public  static extern bool birdRS232WakeUp(int nGroupID, Boolean  bStandAlone, int      
nNumDevices,ref ushort pwComport, uint dwBaudRate, uint dwReadTimeout, uint   
dwWriteTimeout);

 ushort[] COM_port = new ushort[5]{0,13,0,0,0};

        if ((!birdRS232WakeUp(GROUP_ID, false, DEVCOUNT,ref  COM_port, BAUD_RATE, READ_TIMEOUT, WRITE_TIMEOUT)))
        {
            LWakeUpStatus.Text = "Failde to wake up FOB";

        }

finalyy我收到此错误消息“Error 2 Argument'4':无法从'ref ushort []'转换为'ref ushort'”

有人对此有任何线索吗?

1 个答案:

答案 0 :(得分:1)

当前管理的外部定义只需要一个ushort值时,您就会传递一个ushort类型的数组:

那是:

ushort pwComport

应该是:

ushort[] pwComport