PC Review


Reply
Thread Tools Rate Thread

Calling WinAPI Functions from .NET

 
 
=?Utf-8?B?RGFu?=
Guest
Posts: n/a
 
      9th Oct 2005
Are there any concise rules about when you need to marshal data to WinAPI
calls, and when you don't? It appears that most times you can call WinAPI
calls directly from .NET without any preparation. For example

Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal
pPrinterName As String, ByVal phPrinter As Integer,
<MarshalAs(UnmanagedType.Struct)> ByRef pDefault As PRINTER_DEFAULTS) As
Integer

Dim tmpHandle As Int32
Dim msDeviceName as String = "MyPrinter"
OpenPrinter(msDeviceName, tmpHandle, 0)

However, some functions appear to require all kinds of fancy manipulation
before you get to call them--particularly ones which require structures or
arrays. (See http://www.codeproject.com/vb/net/printwatchvbnet.asp). Why
isn't .NET smart enough to handle this "under the covers", the way that VB6
is?



 
Reply With Quote
 
 
 
 
Peter Foot [MVP]
Guest
Posts: n/a
 
      9th Oct 2005
There are a couple of articles on MSDN which describe the capabilities /
limitations of Platform Invocation (P/Invoke) from .NETCF:-
http://msdn.microsoft.com/smartclien...ntrointerp.asp
http://msdn.microsoft.com/smartclien...advinterop.asp

Peter

--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://www.peterfoot.net

"Dan" <(E-Mail Removed)> wrote in message
news:FCE63829-AF20-404E-A6FB-(E-Mail Removed)...
> Are there any concise rules about when you need to marshal data to WinAPI
> calls, and when you don't? It appears that most times you can call WinAPI
> calls directly from .NET without any preparation. For example
>
> Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA"
> (ByVal
> pPrinterName As String, ByVal phPrinter As Integer,
> <MarshalAs(UnmanagedType.Struct)> ByRef pDefault As PRINTER_DEFAULTS) As
> Integer
>
> Dim tmpHandle As Int32
> Dim msDeviceName as String = "MyPrinter"
> OpenPrinter(msDeviceName, tmpHandle, 0)
>
> However, some functions appear to require all kinds of fancy manipulation
> before you get to call them--particularly ones which require structures or
> arrays. (See http://www.codeproject.com/vb/net/printwatchvbnet.asp). Why
> isn't .NET smart enough to handle this "under the covers", the way that
> VB6
> is?
>
>
>



 
Reply With Quote
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      9th Oct 2005
Right, but the question was targeting the full framework I think...
I would say, read this:
http://www.amazon.com/exec/obidos/tg...glance&s=books

"Peter Foot [MVP]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> There are a couple of articles on MSDN which describe the capabilities /
> limitations of Platform Invocation (P/Invoke) from .NETCF:-
> http://msdn.microsoft.com/smartclien...ntrointerp.asp
> http://msdn.microsoft.com/smartclien...advinterop.asp
>
> Peter
>
> --
> Peter Foot
> Windows Embedded MVP
> http://www.inthehand.com | http://www.peterfoot.net
>
> "Dan" <(E-Mail Removed)> wrote in message
> news:FCE63829-AF20-404E-A6FB-(E-Mail Removed)...
>> Are there any concise rules about when you need to marshal data to WinAPI
>> calls, and when you don't? It appears that most times you can call
>> WinAPI
>> calls directly from .NET without any preparation. For example
>>
>> Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA"
>> (ByVal
>> pPrinterName As String, ByVal phPrinter As Integer,
>> <MarshalAs(UnmanagedType.Struct)> ByRef pDefault As PRINTER_DEFAULTS) As
>> Integer
>>
>> Dim tmpHandle As Int32
>> Dim msDeviceName as String = "MyPrinter"
>> OpenPrinter(msDeviceName, tmpHandle, 0)
>>
>> However, some functions appear to require all kinds of fancy manipulation
>> before you get to call them--particularly ones which require structures
>> or
>> arrays. (See http://www.codeproject.com/vb/net/printwatchvbnet.asp).
>> Why
>> isn't .NET smart enough to handle this "under the covers", the way that
>> VB6
>> is?
>>
>>
>>

>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in calling winapi that use pointer to struct from c# =?Utf-8?B?YW1pdCBiaGFyYWR3YWo=?= Microsoft C# .NET 1 25th Nov 2004 04:17 PM
problem in calling winapi that use pointer to struct from c# =?Utf-8?B?YW1pdCBiaGFyYWR3YWo=?= Microsoft Dot NET Framework Forms 2 25th Nov 2004 03:48 PM
calling WinApi functions from c# Michael Maercker Microsoft C# .NET 6 31st Oct 2004 10:39 AM
WinAPI Functions solex Microsoft Excel Programming 5 13th Jan 2004 11:55 AM
Calling a WinAPI function nicolasr Microsoft C# .NET 2 10th Aug 2003 01:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:43 PM.