Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2004

Tip: Looking for answers? Try searching our database.

Addressing reference to struct in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lau Lei Cheong - 24 Nov 2004 10:18 GMT
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

Signature

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.

Mattias Sj?gren - 25 Nov 2004 12:57 GMT
>Is there any equivalent to the "null in C++ " in C#.

That would be IntPtr.Zero for IntPtr parameters and null for pointer
parameters in unsaafe code.

Just like in C++, you can't pass null to a ref parameter.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.