【问题标题】:Method with return value as boolean返回值为布尔值的方法
【发布时间】:2015-09-10 05:39:37
【问题描述】:

我有以下代码,其中 ShellExecuteEx 在执行时返回布尔值 true 或 false。我通过将其转换为字符串将其分配给类级别变量。

strShellCallStatus = ShellExecuteEx(ref info).ToString();

[DllImport("shell32.dll", CharSet = CharSet.Auto)]
static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);

public static void exev()
{
    SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
    info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
    info.lpVerb = "open";
    info.lpFile = "c:\\windows\\notepad.exe";
    info.nShow = 5;
    info.fMask = 0x440;
    info.hwnd = IntPtr.Zero;
    strShellCallStatus = ShellExecuteEx(ref info).ToString();
}

我应该担心 ShellExecuteEx 返回空值吗?如果是这样,我想使用以下语句:

strShellCallStatus = Convert.ToString(ShellExecuteEx(ref info));

【问题讨论】:

  • bool 怎么可能是null
  • 为什么需要返回一个NULL值,要么执行,要么不执行。

标签: c# interop user32


【解决方案1】:

只要ShellExecuteEx 签名不是bool? ShellExecuteEx(),你就不必害怕它会返回null,因为bool 是一个默认false 的值类型。

简单地说 - 带有签名 bool ShellExecuteEx() 的方法无法返回 null,因为它甚至无法编译。

【讨论】:

    猜你喜欢
    • 2012-11-03
    • 1970-01-01
    • 2012-11-04
    • 2015-06-18
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多