views:

167

answers:

4

If I try a marshal a string that is really a NULL pointer, what will happen?

A: 

Well what do you think ? LPstr is A pointer to a null-terminated array of ANSI characters. NULL is obviously not a pointer to a null terminated array. It will probably try to read from NULL, or something. In any case its undefined behavior, it will crash your app or leave it in unknown state. Why do you want that ? why not just check before mashalling it ?

daniel
How can I check an attribute? then I will need to marshal an IntPtr.
DevDevDev
A: 

I imagine it will pass NULL to the underlying API, which may or may not be acceptable input, depending on what you're calling. Most decently designed C(++) code checks for that sort of thing, but it's best to double check.

Promit
+2  A: 

From native to managed, you get a null string object. From managed to native, you get a null pointer. It's fairly intuitive. :-)

Ben M
Cool that's what I was hoping for. You get the null string not the empty string and definitely not an error?
DevDevDev
Yeah, that's right.
Ben M
+1  A: 

A NULL value when typed as a string will be translated to the respective language representation of NULL when marshalled in either direction (NULL for C and null for C#)

JaredPar