PDA

Click to See Complete Forum and Search --> : How to call this legacy function in C#?


batgurl
Aug 6th, 2006, 08:45 AM
Some kind person show me how to call this in C#?

In VB6 I use:


Declare Function GX_GetDTMFKey Lib "GxVoice.dll" (ByVal ChannelNo As Integer, ByVal DTMFCount As Integer, ByRef DTMFString As Byte) As Integer

...
Dim DtmfBuffer(10) As Byte
Dim itemp As Integer
Dim ChannelNo As Integer

ChannelNo = 5

itemp = GX_GetDTMFKey(ChannelNo, 1, DtmfBuffer(0))


Works great.

In C# I try


[DllImport("GxVoice.dll", CharSet = CharSet.Auto)]
public extern static ushort GX_GetDTMFKey(ushort ChannelNo, ushort DTMFCount, char[] DTMFKey);

...

char[] DTMFKey = new char[1];
ushort i = GxVoiceAPI.GX_GetDTMFKey(5, 1, DTMFKey);



And i comes back with an error code.

Something to do with passing DTMFKey by reference?

The DLL is unmanaged legacy code.

jmcilhinney
Aug 6th, 2006, 08:57 AM
You're passing a Byte by reference in the first code snippet and it works so why do anything differently in the second?[DllImport("GxVoice.dll", CharSet = CharSet.Auto)]
public extern static short GX_GetDTMFKey(short ChannelNo, short DTMFCount, ref Byte DTMFBuffer);byte[10] dtmfBuffer;
short iTemp;
short channelNo = 5;

iTemp = GX_GetDTMFKey(channelNo, 1, ref dtmfBuffer[0]);

batgurl
Aug 6th, 2006, 09:10 AM
byte[10] dtmfBuffer;


Will not compile.

I think you mean:


byte[] dtmfBuffer = new byte[10];


Thanks but unfortunately did not work. Returns an error code (-1) shoudl return zero.

I'm pretty sure


[DllImport("GxVoice.dll", CharSet = CharSet.Auto)]
public extern static ushort GX_GetDTMFKey(ushort ChannelNo, ushort DTMFCount, char[] DTMFKey);


... is okay as it is part of another project that was working fine - but the code that called it is no longer available.

jmcilhinney
Aug 6th, 2006, 09:29 AM
Yeah, sorry about the byte array. Still in VB mode there. From the VB6 declaration I would guess that the third argument is actually declared as a pointer. Given that C# supports pointers you could declare an unsafe code block and actually use a pointer. Apart from that I wouldn't like to suggest anything else without being able to test it, given that I would have thought my previous suggestion would have worked.

JPicasso
Aug 8th, 2006, 08:19 AM
How come you are using ushort data types in place of integers?