开发者

How to import function from c++ .dll into c sharp

In C++ i have this function and used it like below. How to i need to code in c sharp?

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

in the manual it state that "pwComport" points to an array of words, each of which is the number of the comport attached to one of the birds(e.g., COM1 = 1, COM2 = 2, etc.)

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

This is how i do it in c sharp .

[DllIm开发者_JAVA百科port(@"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";

        }

And finalyy i got this error message "Error 2 Argument '4': cannot convert from 'ref ushort[]' to 'ref ushort'"

Someone have any clue about it?


The immediate issue is that you're passing an array of type ushort when only a single ushort value is expected by your current managed extern definition:

That is:

ushort pwComport

Should be:

ushort[] pwComport
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜