By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,899 Members | 1,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,899 IT Pros & Developers. It's quick & easy.

P/Invoke syntax

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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/epi...K/manifest.xml

Mattias

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

P: n/a
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/epi...SK/manifest.xm

Mattia

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

Nov 15 '05 #3

P: n/a
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

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

This discussion thread is closed

Replies have been disabled for this discussion.