Addressing reference to struct in C#

From: Lau Lei Cheong (leu_lc_at_yehoo.com.hk)
Date: 11/24/04


Date: Wed, 24 Nov 2004 18:18:19 +0800

Hello,

        It happens that one days I had to write program that uses Windows
API, but seems C# do not have anything as convenient as "windows.h" in C++,
so I had to declare the functions myself.

        While most of the codes I found on the net involving C++, I have to
do some ports in order to use them in my code. (and actually I'm building
wrapper classes for the APIs, based on functionality) And I have some
problem on the declarations.

        I'll use ReadFile() to illustrate the problem, here's the prototype
defined in MSDN:

BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped);

And on the web, I see variants of declarations:

[DllImport("kernel32.dll", SetLastError=true)]
static extern int ReadFile (int hFile, ref object lpBuffer, int
nNumberOfBytesToRead, ref int lpNumberOfBytesRead, ref OVERLAPPED
lpOverlapped);

[DllImport("kernel32.dll", SetLastError=true)]
static extern int ReadFile (int hFile, ref object lpBuffer, int
nNumberOfBytesToRead, out int lpNumberOfBytesRead, out OVERLAPPED
lpOverlapped);

  [DllImport("kernel32.dll", SetLastError=true)]
  public static extern bool ReadFile(System.IntPtr hFile, byte[] lpBuffer,
uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, out uint
lpOverlapped);

  [DllImport("kernel32.dll", SetLastError=true)]
  public static extern bool ReadFile(System.IntPtr hFile, byte[] lpBuffer,
uint nNumberOfBytesToRead, out int lpNumberOfBytesRead, System.IntPtr
lpOverlapped);

Okay, it seems that the first and last one is closer to defination, so I
included both.

It becomes strange that when places that C++ code uses null and C# yields
compile time error when I use that. I have to use IntPtr.Zero in this case.
Is there any equivalent to the "null in C++ " in C#. In this way I won't
have to duplicate function declarations just to work around cases I have to
pass nulls to them.

I don't what to use unsafe keyword because of some issues. (One of them is I
have poor mind over pointers. :P)

I feel that there should be neater way addressing this issue. Please help if
anyone knows. Thank you.

Regards,
Lau Lei Cheong

-- 
If you want to mail to me, please replace all instance of "e" to "a" in my
email address. Thank you.
Sorry for any inconvience caused, but there's many spam-mailers.


Relevant Pages

  • Re: Why is this not an error in Visual C?
    ... says that when an old-style function definition is processed, ... That is, I think, `int fint x; ... Concerning the compatibility of function ... All declarations in the same scope that refer to the same object or function ...
    (comp.lang.c)
  • Re: Why is this not an error in Visual C?
    ... int foo ... What we're looking for to describe MSVC as broken is a constraint ... involve declarations of incompatible types. ... compatibility and of a composite type, ...
    (comp.lang.c)
  • Re: Why is this not an error in Visual C?
    ... says that when an old-style function definition is processed, ... That is, I think, `int fint x; ... Concerning the compatibility of function ... All declarations in the same scope that refer to the same object or function ...
    (comp.lang.c)
  • Re: range vs precisionC
    ... volatile int a; ... in this case that is volatile. ... and we have declarations like: ... What is needed now, I think, is to realize that subtypes (and all those ad ...
    (comp.lang.c)
  • Re: cooking the spaghetti
    ... I find your code layout to be jarring. ...     int main ... latter is probably a throwback to K&R C, where parameter declarations ... int main{ ...
    (comp.lang.c)