本文介绍了从C#调用Rand或SRand(在Windows 7 x64中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法正确P调用C Rand和SRand函数.
我在x64和x86模式下都尝试过,但是堆栈不平衡或只有两位数字(38 41 45 ...).
我相信我们应该期望更大的数字0lt; = n< 2 ^ 32.

任何人都可以发现问题:

X64

I can''t manage to PInvoke properly the C Rand and SRand functions.
I tried in both x64 and x86 modes but I get either unbalanced stacks or just two digits numbers (38 41 45...).
I believe we should be expecting much bigger numbers 0<=n<2^32.

Can anyone spot the problem:

X64

//[DllImport(@"C:\Windows\SysWOW64\msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
[DllImport(@"C:\Windows\System32\msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Int64 srand(UInt64 seed);

//[DllImport(@"C:\Windows\SysWOW64\msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
[DllImport(@"C:\Windows\System32\msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Int64 rand();


public static void Main()
{
    for (uint i = 0; i < 10; i++)
    {
        srand(i);
        Console.WriteLine(rand());
    }

}




X86




X86

[DllImport(@"C:\Windows\SysWOW64\msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
//[DllImport(@"C:\Windows\System32\msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Int64 srand(UInt32 seed);

[DllImport(@"C:\Windows\SysWOW64\msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
//[DllImport(@"C:\Windows\System32\msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 rand();


public static void Main()
{
    for (uint i = 0; i < 10; i++)
    {
        srand(i);
        Console.WriteLine(rand());
    }

}

推荐答案


这篇关于从C#调用Rand或SRand(在Windows 7 x64中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 20:26