相关疑难解决方法(0)

C#,IntPtr的默认参数值

我想在一个带有参数IntPtr.Zero的函数中使用默认参数值IntPtr.这是不可能的,因为IntPtr.Zero它不是编译时常量.

有什么方法可以做我想做的事吗?

c# intptr default-parameters

15
推荐指数
2
解决办法
6027
查看次数

如何在C#中处理null或者可选的dll struct参数

如何struct使用pinvoke 处理从C#调用的dll方法中的可选参数?例如,lpSecurityAttributes此处参数应该null在不存在时传递.

正确的传递方式struct似乎正在使用ref,但它不能有可选参数,或者null总的来说.

有什么方法可以达到这个目的?

c# null pinvoke interop optional-parameters

13
推荐指数
1
解决办法
1172
查看次数

如何将IntPtr获取到结构?

我有一个带签名的方法

public int Copy(Texture texture, Rect? srcrect, Rect? dstrect)
Run Code Online (Sandbox Code Playgroud)

Rect是一个结构,但我需要允许调用者也传递null(或IntPtr.Zero)到该方法.

我想将其传递给带有签名的DLL

[DllImport("SDL2.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_RenderCopy")]
internal static extern int RenderCopy(IntPtr renderer, IntPtr texture, IntPtr srcrect, IntPtr dstrect);
Run Code Online (Sandbox Code Playgroud)

我希望我可以做以下事情:

return SDL.RenderCopy(_ptr, texture._ptr, srcrect.HasValue ? (IntPtr)srcrect.Value : IntPtr.Zero, dstrect.HasValue ? (IntPtr)dstrect.Value : IntPtr.Zero);
Run Code Online (Sandbox Code Playgroud)

但我不能像那样强制转换结构.还有其他方法可以让我IntPtr摆脱它吗?


另一种方法是创建4个重载:

  • ref Rect, ref Rect
  • IntPtr, IntPtr
  • ref Rect, IntPtr
  • IntPtr, ref Rect

如果我需要传递超过2个结构指针,这可能会变得更加混乱.


我提出了一个解决方案,但我有一些问题:

public int Copy(Texture texture, Rect? srcrect=null, Rect? dstrect=null)
{
    return SDL.RenderCopy(_ptr, texture._ptr, srcrect.HasValue …
Run Code Online (Sandbox Code Playgroud)

c# interop

9
推荐指数
1
解决办法
2万
查看次数

P/invoke函数将指针指向struct

诸如CreateProcess之类的函数具有指向结构的指针.在CI中,它只是NULL作为可选参数的指针传递,而不是在堆栈上创建虚拟结构对象并将指针传递给虚拟对象.

在C#中,我已将其声明为(p/invoke)

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern bool CreateProcess(
            string lpApplicationName,
            string lpCommandLine,
            ref SECURITY_ATTRIBUTES lpProcessAttributes,
            ref SECURITY_ATTRIBUTES lpThreadAttributes,
            bool bInheritHandles,
            CreateProcessFlags dwProcessCreationFlags,
            IntPtr lpEnvironment,
            string lpCurrentDirectory,
            ref STARTUPINFO lpStartupInfo,
            ref PROCESS_INFORMATION lpProcessInformation);
Run Code Online (Sandbox Code Playgroud)

但是,如果我试图通过nulllpProcessAttributes论点或lpThreadAttributes论据,我得到一个编译错误:

错误2参数3:无法从"<null>"转换为"ref Debugging.Wrappers.SECURITY_ATTRIBUTES"

如何修改上面的函数签名,以便我可以传递nullSECURITY_ATTRIBUTES参数,而不会出现此编译器错误?(如果我愿意,还能传递一个真正的结构?)

c# pinvoke winapi interop

6
推荐指数
2
解决办法
3835
查看次数

如何为pinvoke调用的结构分配空值?

我想打电话给LsaOpenPolicy,它需要一个LSA_OBJECT_ATTRIBUTES结构。我正在使用来自pinvoke.net的结构定义。该结构具有领域public LSA_UNICODE_STRING ObjectName;

LSA_OBJECT_ATTRIBUTESMSDN文章说:

当您调用时LsaOpenPolicy,将此函数的成员初始化为NULL或零,因为该函数不使用该信息。

特别是:

对象名

    应该为NULL

虽然我可以将LSA_OBJECT_ATTRIBUTESstruct 的其他字段分配给IntPtr.Zero(或仅将其分配给0值类型),但看不到的方法ObjectName。特别,

无法将类型'System.IntPtr'隐式转换为'LSA_UNICODE_STRING'

在这种情况下我该怎么办?我应该初始化LSA_UNICODE_STRING长度为零(长度0,最大长度0,空/空缓冲区)的a吗?我应该更改LSA_OBJECT_ATTRIBUTES定义以便该字段为IntPtr吗?我应该使它为可空值并分配null给该字段吗?

我对内存管理的经验很少,因此对于可能导致内存泄漏的任何事情我都非常警惕。

c# pinvoke struct marshalling

5
推荐指数
1
解决办法
1382
查看次数