473,322 Members | 1,408 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,322 software developers and data experts.

P/Invoke syntax

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
3 3060
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: boxim | last post by:
hi all, I'm having a few problems whereby my application is hanging when using the Invoke method of a form's control. Basically, when a user clicks a button on the form, it calls a remote...
11
by: JPRoot | last post by:
Hi I wish to set/get a property using System.ComponentModel.ISynchronizeInvoke.Invoke but I cannot find the exact syntax.. Any clue how to do this (right now I am making GetEnabled/SetEnabled...
1
by: r | last post by:
Hi, Could someone please direct me in the right direction? I would like to p/invoke few functions from user32: DrawAnimatedRects FindWindow, FindWindowEx , GetWindowRect struct RECT how...
3
by: m.posseth | last post by:
Hello does someone know how i can invoke a method in the underlying thread without the usage of a window handle ?? This works perfect in a form Me.Invoke(New MethodInvoker(AddressOf...
1
by: swartzbill2000 | last post by:
Hello, I wish to use Control.Invoke to make an inter-thread call to a sub that has ByRef args. How can I pass the ByRef args? Syntax like New Object() {byRefArg1, byRefArg2} in fact passes by...
1
by: PengYu.UT | last post by:
I try to invoke the operator of the base class. ** line doesn't work. But *** line works. However, *** lines becomes problematic with multiple inheritance is used for "derived". **** works, but...
7
by: George | last post by:
Hi, Does anyone know how Control.Invoke can invoke a method which has ref parameters? For example, class A { public delegate MyFuncHandler(ref int i, ref int j);
23
by: Thomas Due | last post by:
Hi, I have a class which monitors a TCP socket. This will on occasion raise an event which can be handled by a GUI. Now, I am aware of the if(InvokeRequire) { EventHandler d = new...
6
by: Dom | last post by:
I'm teaching myself about delegates and the Invoke method, and I have a few newbie questions for the gurus out there: Here are some CSharp statements: 1. public delegate void MyDelegate (int k,...
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
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.