Passing NULL to a DLLImport function.

B

Bucky Pollard

I am calling a PVCS DLL function from C#. According to the documentation, I
can pass NULL to any output parameter that I do not want to receive data
back for. How do I do this? Every thing I've tried results in a compiler
error.

TIA:
Bucky Pollard

My declaration is below. I am only looking for one piece of information, so
ultimately I'd like to call the function something like this.

PvcsGetArchiveInfo( ARCHVIE_NOT_OPEN, "c:\\foo.c,v", null, null, null, null,
null, null, null, null, ref MyInt, null, null, null, null, null, null,
MyFlags)

[DllImport("vmwfdtk")]
public static extern int
PvcsGetArchiveInfoVB1( long /*ARCHIVEHANDLE*/ hArchive, /* Input */
string fileName, /* Input */
ref int revcnt, /* Output */
ref int lockers, /* Output */
StringBuilder archive, /* Output */
StringBuilder workfile, /* Output */
StringBuilder owner, /* Output */
StringBuilder access, /* Output */
StringBuilder create_time, /* Output */
ref int attribute_chk_lock, /* Output */
ref int attribute_wrt_prot, /* Output */
ref int attribute_excl_lock, /* Output */
ref int attribute_exp_keys, /* Output */
ref int attribute_translate, /* Output */
ref int attribute_cmprs_delta, /* Output */
ref int attribute_cmprs_text, /* Output */
long /*PVCS_FLAGS*/ flags);

If this helps, here is C Prototype:

PvcsGetArchiveInfoVB1(ARCHIVEHANDLE hArchive, /* Input */
unsigned char * fileName, /* Input */
unsigned short * revcnt, /* Output */
unsigned short * lockers, /* Output */
unsigned char * archive, /* Output */
unsigned char * workfile, /* Output */
unsigned char * owner, /* Output */
unsigned char * access, /* Output */
unsigned char * create_time, /* Output */
unsigned short * attribute_chk_lock, /* Output */
unsigned short * attribute_wrt_prot, /* Output */
unsigned short * attribute_excl_lock, /* Output */
unsigned short * attribute_exp_keys, /* Output */
unsigned short * attribute_translate, /* Output */
unsigned short * attribute_cmprs_delta, /* Output */
unsigned short * attribute_cmprs_text, /* Output */
PVCS_FLAGS flags) /* Input */
 
N

Nicholas Paldino [.NET/C# MVP]

Bucky,

First, you should make sure your declaration is correct. You are using
a 64-bit value for the first parameter, which I don't think you really want.

As for passing in nulls, it would be easiest to do this using unsafe
code. You could declare the parameters as pointer types, and then pass 0 or
maybe null for the parameter you don't want to be set on output.

Finally, you should explicitly state that the CharSet is Ansi, since you
are using 8-bit character values.

Hope this helps.
 
W

Willy Denoyette [MVP]

It's just impossible to tell from the C function declaration how your
DllImport signature should look like, but you should keep in mind that
shorts in C are shorts in C# too, longs in C are int's in C#, handles are
IntPtr, so this is something you should correct first. Leaves us with
PVCS_FLAGS, which is IMO NOT a long in C#.

Willy.
 
B

Bucky Pollard

Thank you to all the replies. I did have to change the ARCHIVEHANDLE
variable to a UIntPtr. IntPtr was getting mathematic errors, which I assumed
were overflows, but didn't research further. All other variables are working
as it, but it seems it would be good form to change the ints to shorts and
the longs to ints.

So the function works, but the issue is I have to create and pass 8 shorts
and 5 StringBuilders for information I really do not care about. I'm not
sure about the overhead on the C# side, but am wondering if the "pointer" is
NULL on the receiving function, if the function does't extract that
information from archive (which is I/O which is overhead).

Also, instead of declaring

ref short lockers

should I code (will it work and is it better form)

out short lockers?

Thanks again,

Bucky.
(Long time C++ coder, short time C# coder and missing pointers)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top