Re: Help calling function from DLL and processing returned value (Can not marshal return value)

v-jetan_at_online.microsoft.com
Date: 03/04/04


Date: Thu, 04 Mar 2004 07:44:20 GMT


Hi Angel,

Thanks for posting in this community!!

I am monitoring this group. After reviewing your discussion in this post, I
think you meet the problem of P/invoke marshal.

I think you should follow Nicholas's suggestion to marshal the return value
as IntPtr. But, I think you may can not use Marshal.PtrToStringAnsi to
convert the value. Because the return value of z4date may be a character
array without a '\0' at the end of the array. So maybe you should read the
byte one by one(Use Marshal.ReadByte method). Like this:

//Test C function, which return value without a '\0' at the end of the
character array:
extern "C" __declspec(dllexport) char* RetAChar()
{
        pChar = new char[8];
        for(int i=0; i<=7; i++)
        {
                *pChar = 'd';
                pChar++;
        }
        pChar -= 8;
        return pChar;
}

[DllImport("DllDemo.dll", CharSet=CharSet.Ansi)]
private static extern IntPtr RetAChar();
private void button3_Click(object sender, System.EventArgs e)
{
        IntPtr str=RetAChar();

        byte b;
        for(int i=0;i<8;i++)
        {
                b=Marshal.ReadByte(str);
                Console.WriteLine(Convert.ToChar(b));
        }
}

But as you stated, your problem should not related to this, can you show me
more information of your problem?

====================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.