473,324 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

struct -> null

Hello,

if I have a P/Invoke method: open(STRUCT1 str, STRUCT2 str2);

is there a way to pass <null> for a struct in C#?
How can I solve this?

thx
Nov 15 '05 #1
7 7166
Structs are value types (in C++ and in C#), so NULL would be incompatible
with any struct type. Now, if you want to pass NULL for a reference type,
for example if yourfunction was declared like:

open (struct struct1 *str, struct struct1 *str2) { ... }

you would do so with IntPtr.Zero (that is NULL).

Hope that helps,
-JG
Nov 15 '05 #2
Create two DllImports for the same api. The second one will be overloaded
with something like "object flag" on the STRUCT2 parm. Now you can pass a
STRUCT2 or a null ref. Example:

[DllImport("kernel32.dll")]
public static extern uint GetFileSize(
IntPtr hFile, //[in] Handle to the file for size.
ref uint fileSizeHigh); //[out] Pointer to var where high-order word is
returned.

[DllImport("kernel32.dll")]
public static extern uint GetFileSize(
IntPtr hFile, //[in] Handle to the file for size.
object flag); //[out] Overloaded to allow passing null.

--
William Stacey, MVP

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:u0**************@TK2MSFTNGP11.phx.gbl...
Hello,

if I have a P/Invoke method: open(STRUCT1 str, STRUCT2 str2);

is there a way to pass <null> for a struct in C#?
How can I solve this?

thx

Nov 15 '05 #3
From the help on IntPtr.Zero: "The value of this field is not equivalent to
a null reference..."
Wouldn't you be passing a ptr to the IntPtr struct that contains a zero? Or
does pInvoke change this to a null ref?

--
William Stacey, MVP

"Juan Gabriel Del Cid" <jd*****@atrevido.nospam.net> wrote in message
news:eP**************@TK2MSFTNGP10.phx.gbl...
Structs are value types (in C++ and in C#), so NULL would be incompatible
with any struct type. Now, if you want to pass NULL for a reference type,
for example if yourfunction was declared like:

open (struct struct1 *str, struct struct1 *str2) { ... }

you would do so with IntPtr.Zero (that is NULL).

Hope that helps,
-JG

Nov 15 '05 #4
is there a way to pass <null> for a struct in C#?
How can I solve this?


In addition to what the other suggested, another way is to use unsafe
code and a real pointer type

STRUCT1* str

And yet another option is to change the parameter type to an array

STRUCT1[] str

and then either pass in a single-element array or null.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #5
> From the help on IntPtr.Zero: "The value of this field is not
equivalent to a null reference..."


Of course it's not a null reference, it's a value type. In the past there
was no distinction between a value type and a reference type. You could cast
data to whatever you wanted and back. For example, a pointer is really
nothing more than an integer (4 bytes on a 32 bit processor). In this case,
NULL is just 4 bytes all set to zero (just like IntPtr.Zero).

So, in C/C++ you have:

if ((int)NULL == 0) {
// this is always true
}

and in C# you have:

if ((int)null == 0) {
// this will never compile because you can't convert
// null (or any other reference type)
// to an int (or any other value type)
}

if ((int)IntPtr.Zero == 0) {
// this is always true
}

Hope that clears it up for you,
-JG
Nov 15 '05 #6
I'd bet that the method you're trying to call takes a pointer to a struct,
and you should be using:

open(ref STRUCT1 str, ref STRUCT2 str2);

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Dirk Reske" <_F*******@gmx.net> wrote in message
news:u0**************@TK2MSFTNGP11.phx.gbl...
Hello,

if I have a P/Invoke method: open(STRUCT1 str, STRUCT2 str2);

is there a way to pass <null> for a struct in C#?
How can I solve this?

thx

Nov 15 '05 #7
In terms of the OP question, IntPtr does not help him. Assume the exported
function is something like:
BOOL DoSomething(SOMESTRUCT * struct1);

And the managed struct and/or class is:
// Declares a managed structure for the unmanaged structure.
[ StructLayout( LayoutKind.Sequential )]
public struct MyStruct
{
public int one = 1;
public long two =2;
}
// Declares a managed class for the unmanaged structure.
[ StructLayout( LayoutKind.Sequential )]
public class MyClass
{
public int one = 1;
public long two = 2;
}

Then his exports might be something like (among others):
// Because MyStruct is a value type, you cannot pass null as a
// parameter. Instead, declare an overloaded method.
[ DllImport( "Kernel32.dll" )]
public static extern bool DoSomething(
ref MyStruct myStruct );

[ DllImport( "Kernel32.dll" )]
public static extern bool DoSomething(
int flag ); // Declares an int instead of a structure reference so you
can pass a zero.

// Because MyClass is a class, you can also pass null as parameter.
// No overloading is needed.
[ DllImport( "Kernel32.dll", EntryPoint="DoSomething" )]
public static extern bool DoSomething2(
MyClass myClass );

Then he can call "DoSomething" like:
MyStruct ms;
MyClass mc = new MyClass();
bool b = DoSomething(ref ms); //Call using ref to struct.
bool b = DoSomething(0); //Call passing zero. You could pass
(int)IntPtr.Zero here, but why?
bool b = DoSomething2(mc); //Call using ref to class.
bool b = DoSomething2(null); //Call passing null.

In none of those cases does IntPtr help.

--
William Stacey

"Juan Gabriel Del Cid" <jd*****@atrevido.nospam.net> wrote in message
news:uV**************@TK2MSFTNGP12.phx.gbl...
Hope that clears it up for you,

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
10
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
6
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is ...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
15
by: dutchgoldtony | last post by:
Hi all, I was just wondering if this is possible. I'm trying to implement a viterbi decoder in C and am creating an array of nodes (the struct), and an array of pointers to nodes (the member...
7
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.