P/Invoke syntax

G

Guest

I'm an absolute newb to P/Invoke, and got stuck on this little thing, what would be the correct P/Invoke syntax to import the following dll function

NTSTATUS TdiRegisterProvider
IN PUNICODE_STRING ProviderName
OUT HANDLE *ProviderHandle )

definition of the data types used are
typedef LONG NTSTATUS, *PNTSTATUS
typedef struct _UNICODE_STRING
USHORT Length
USHORT MaximumLength
PWSTR Buffer
} UNICODE_STRING, *PUNICODE_STRING

this is my code for the dllimport, which is not workin

[StructLayout( LayoutKind.Sequential, CharSet = CharSet.Unicode )
public class UnicodeStrin

public ushort Length
public ushort MaximumLength
public string Buffer


[DllImport( "drivers\\tdi.sys", EntryPoint="TdiRegisterProvider" )
public static extern int TdiRegisterProvider(UnicodeString name, ref IntPtr handle)

also trie

[DllImport( "drivers\\tdi.sys", EntryPoint="TdiRegisterProvider" )
public static extern int TdiRegisterProvider(UnicodeString name, [Out] IntPtr handle)

don't laugh if it looks completely ridiculous, I'd appretiate it if someone will point out to me what I'm doing wrong

also, on a side note, can someone tell me when to define an unmanaged struct as managed class and when to define it as managed struct. what are the implications of the two methods? thanks
 
M

Mattias Sjögren

Daniel,
[DllImport( "drivers\\tdi.sys", EntryPoint="TdiRegisterProvider" )]
public static extern int TdiRegisterProvider(UnicodeString name, ref IntPtr handle);

This looks like it should work, as long as UnicodeString is a class.
What NTSTATUS code do you get back when calling the function? How do
you populate the UnicodeString?

also, on a side note, can someone tell me when to define an unmanaged struct as managed class and when to define it as managed struct. what are the implications of the two methods? thanks

Well, it depends. I prefer to use structs most of the time (primarily
because they work correctly when they are used as members of other
types). But sometimes making it a class has some benefits, such as
making parameters easily nullable. You may want to watch this MSDN TV
episode for more details

http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20030424NETFXSK/manifest.xml



Mattias
 
G

Guest

inlin

----- Mattias Sjögren wrote: ----

Daniel
[DllImport( "drivers\\tdi.sys", EntryPoint="TdiRegisterProvider" )
public static extern int TdiRegisterProvider(UnicodeString name, ref IntPtr handle)
This looks like it should work, as long as UnicodeString is a class
What NTSTATUS code do you get back when calling the function? How d
you populate the UnicodeString

I get a null reference exception, which leads me to believe that my declaration was incorrect. but I really don't know what went wrong, nor how to debug this thing. one interesting note is that the output parameter is a HANDLE pointer, not a HANDLE. what should I use for a HANDLE pointer

thanks for the video link, I will check it out

also, on a side note, can someone tell me when to define an unmanaged struct as managed class and when to define it as managed struct. what are the implications of the two methods? thank

Well, it depends. I prefer to use structs most of the time (primaril
because they work correctly when they are used as members of othe
types). But sometimes making it a class has some benefits, such a
making parameters easily nullable. You may want to watch this MSDN T
episode for more detail

http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20030424NETFXSK/manifest.xm



Mattia
 
M

Mattias Sjögren

but I really don't know what went wrong,

Neither do I. Do you have working code in some other language that
calls this API?

one interesting note is that the output parameter is a HANDLE pointer, not a HANDLE. what should I use for a HANDLE pointer.

ref IntPtr



Mattias
 

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