相关疑难解决方法(0)

P/Invoke [In,Out]属性是否可选用于编组数组?

假设存在一个带有纯C接口的本机函数,如下所示,从本机DLL导出:

// NativeDll.cpp

extern "C" void __stdcall FillArray(
    int fillValue, 
    int count, 
    int* data)
{
    // Assume parameters are OK...

    // Fill the array
    for (int i = 0; i < count; i++)
    {
        data[i] = fillValue;
    }
}
Run Code Online (Sandbox Code Playgroud)

以下P/Invoke工作正常(使用VS2010 SP1测试):

[DllImport("NativeDll.dll", CallingConvention=CallingConvention.StdCall)]
public static extern void FillArray(
    int fillValue,
    int count,
    [In, Out] int[] data
);
Run Code Online (Sandbox Code Playgroud)

以及这个P /调用,与上面相同,但没有[In, Out]属性:

[DllImport("NativeDll.dll", CallingConvention=CallingConvention.StdCall)]
public static extern void FillArray(
    int fillValue,
    int count,
    int[] data
);
Run Code Online (Sandbox Code Playgroud)

那么,这些 …

c# c++ arrays pinvoke attributes

11
推荐指数
1
解决办法
4263
查看次数

如何在.NET c#中使用Win32 GetMonitorInfo()?

我必须实现一个保存窗口最后位置的功能.当应用程序启动时,需要获取并恢复该位置.

现在可能会拆除第二台显示器.如果最后一个位置在现在不可见的监视器上(换句话说,保存的坐标在可见坐标之外),则应捕获此情况并将坐标设置为默认位置而不是最后位置.

为了检索有关监视器的信息,我需要使用Win32.我不容易做这项工作.

我创建了一个Helper CLass:

public static class DisplayHelper
    {
        private const int MONITOR_DEFAULTTONEAREST = 2;

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern int GetSystemMetrics(int nIndex);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern UInt32 MonitorFromPoint(Point pt, UInt32 dwFlags);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern bool GetMonitorInfo(UInt32 monitorHandle, ref MonitorInfo mInfo);


        public static void GetMonitorInfoNow(MonitorInfo mi, Point pt)
        {
            UInt32 mh = MonitorFromPoint(pt, 0);
            mi.cbSize = (UInt32)System.Runtime.InteropServices.Marshal.SizeOf(typeof(MonitorInfo));
            mi.dwFlags = 0;
            bool …
Run Code Online (Sandbox Code Playgroud)

.net c# winapi multiple-monitors user32

6
推荐指数
1
解决办法
9961
查看次数

为什么C#中的"out"参数作为语言构造存在?

为什么C#中的"out"参数作为语言构造存在?

详细阐述这个问题

为什么它首先存在?是否有更好的语言功能可以获得"out"参数可以获得的相同效果?

使值类型的行为类似于引用类型,这不奇怪吗?

是不是有更好的方法从方法返回多个值?

它是一个历史性的东西,意味着C#的第一个版本没有办法实现使用out参数可以实现的东西,但现在有更新的功能,它只是保留在语言中以便向后兼容?

我不是在问什么

  1. 我不是在问它是做什么的
  2. 我不是在问它是如何使用的
  3. 我不是在问"ref"和"out"之间的区别是什么
  4. 我读过应该避免使用它并选择其他结构

在阅读类似的问题时,我没有发现任何重复.

预期答案格式

我很想听到这样的话:"看,这是一个只能通过使用"out"参数语言结构来解决的问题,这里有一个代码示例......".

或者,"看,这曾经是解决以下问题的唯一方法......代码示例......,但是因为C#版本...更好的解决方法就是这样......代码示例......" .

没有意见.

c# out

4
推荐指数
2
解决办法
338
查看次数

标签 统计

c# ×3

.net ×1

arrays ×1

attributes ×1

c++ ×1

multiple-monitors ×1

out ×1

pinvoke ×1

user32 ×1

winapi ×1