简体   繁体   English

在C#中检查空指针

[英]Checking for a null pointer in C#

I am calling a C function. 我正在调用C函数。 The function will return a handle. 该函数将返回一个句柄。 The handle is actually a pointer but the C# program only uses it to call other C functions. 该句柄实际上是一个指针,但是C#程序仅使用它来调用其他C函数。 The C program will return a C NULL if there was an error. 如果发生错误,C程序将返回C NULL。 I want the C# program to check if a C NULL was returned. 我希望C#程序检查是否返回了C NULL。

The "prototype" is as follows: “原型”如下:

[DllImport("W:\\Dropbox\\DSI (His)\\Windows Apps\\Debug\\DsiLibrary_CSharp.dll", CharSet = CharSet.Ansi)]
public static extern UIntPtr XBaseGetDefineFile(short fileNum);

The basic idea of the 'if' would be this: “如果”的基本思想是:

if (XBaseFunctions.XBaseGetDefineFile(900) == null)

I have looked at lots of samples of null pointers but none discuss this topic. 我看过很多空指针的示例,但是没有一个讨论这个主题。 So I tried several ideas but to no success. 所以我尝试了几种想法,但没有成功。

What I need to know is how to properly construct the 'if' statement. 我需要知道的是如何正确构造“ if”语句。

A null pointer is simply a pointer to the memory address '0'. 空指针只是指向内存地址“ 0”的指针。 Therefor: 为此:

if (XBaseFunctions.XBaseGetDefineFile(900) == UIntPtr.Zero)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM