开发者

in .Net 4: PInvokeStackImbalance Exception

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?


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


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


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.


Just for fun :

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜