views:

272

answers:

4

I was using the strlen function from msvcrt.dll in a .Net 3.5 project. More specifically:

private unsafe static extern int strlen( byte *pByte );

After migrating to .NET 4.0, if I use this function it throws a PInvokeStackImbalance exception.

How can I import the .NET 3.5 msvcrt.dll or fix this exception?

+4  A: 

I suspect that the problem is with the calling convention, you should be using Cdecl.

[DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
private unsafe static extern int strlen(byte* pByte);
Chris Taylor
Thank you Chris. it works like a charm again ; ) +1
SDReyes
Great, I'm glad it helped.
Chris Taylor
+1 I had this same problem at work today and your solution was exactly what I needed. Thank you.
Robert Greiner
+1  A: 

This is not really a direct answer, but it seems that for something like this functionality, it might be better to write your own. For example, the following C# code might work (although there are probably one liners using existing functions that would work too):

  static int mystrlen( byte[] pbyte )
     {
     int i = 0;
     while ( pbyte[i] != 0 )
        i++;
     return i;
     }
Mark Wilkins
+1  A: 

There should not be any changes in this from .NET 3.5 to 4. (and, btw, msvcrt.dll is not part of the framework - it is the Microsft C++ Runtime Library). Are you sure nothing else has changed in your project.

I just tried this code, which works and prints "4" as expected:

class Test
{
    public unsafe static void Main(string[] args)
    {
        byte[] bytes = new byte[] {70, 40, 30, 51, 0};
        fixed(byte* ptr = bytes)
        {
            int len = strlen(ptr);
            Console.WriteLine(len);
        }
    }
    [DllImport("msvcrt.dll")]
    private unsafe static extern int strlen(byte* pByte);       
}

It is unclear to me why you would ever want to call strlen from managed code, but of course you might have your reasons. If you need an alternative managed implementation, here is a one liner you can use:

private static int managed_strlen(byte[] bytes)
{
    return bytes.TakeWhile(b => b != 0).Count();
}

Of course that does not deal with multi-byte (unicode) characters, but I don't think strlen does either.

driis
Hi Driis, thanks for your response. I'm using it inside unsafe code, I forgot to mention it. I'd forgot the CallingConvention attribute that @ChrisTaylor : P
SDReyes
+1  A: 

Just for fun :

public static unsafe int strlen(void* buffer)
{
    byte* end = (byte*)buffer;
    while (*end++ != 0);
    return(int)end - (int)buffer - 1;
}
VirtualBlackFox