473,568 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HELP! API upgrade problems

Dear Gurus...

(Sorry to cross post... but...) I have a dire problem - been working on this
for days now. It's the undocumented API's for NTDLL.dll - namely
NtOpenSection, NtMapViewOfSect ion, NtUnmapViewOfSe ction, CloseHandle and
CopyMemory. Code listing 1 shows the existing VB6 code, which works
perfectly. However, on upgrading the code to .NET, I am getting no value
returned to vPhysicalMemory - indicating that the NtOpenSection call isn't
working. In the .NET code (listing number 2) the vReturn value is an error
code - translated through Marshal.GetLast Win32Error give 126 - anyone know
where I can find that specific error codes description?!!?
I've also found a site which suggests that NtOpenSection can be replaced by
an equivalent in kernel32.dll called OpenFileMapping (or OpenFileMapping Ex)
but I've found no useful description of what these functions actually DO.
The original author of the code is an old friend who seems to have vanished
off the face of the planet, so any light anyone can shed on these functions
and how the code actually works will be very gratefully received :D

-----------------------------------------------
Code listing 1 - working VB6 code
(vPort As AILocalPort is just a class that holds details of the
serial/parallel/other I/O ports in use by the software)

Option Explicit
Private Const mcCaseInsensiti ve As Long = &H40
Private Const mcSectionMapRea d = &H4
Private Const mcPageReadOnly = 2
Private Const mcViewShare = 1
Private Type mtUnicodeString
Length As Integer
MaxLength As Integer
Buffer As String
End Type
Private Type mtObjectAttribu tes
Length As Long
RootDirectory As Long
ObjectName As Long
Attributes As Long
SecurityDescrip tor As Long
SecurityQOS As Long
End Type
Private Type mtPhysicalAddre ss
LowPart As Long
HighPart As Long
End Type
Private Declare Function OpenSection Lib "ntdll.dll" Alias "NtOpenSect ion"
(ByRef pSection As Long, ByVal pAccess As Long, ByRef pObjAttribs As
mtObjectAttribu tes) As Long
Private Declare Function MapSectionView Lib "ntdll.dll" Alias
"NtMapViewOfSec tion" (ByVal pSection As Long, ByVal pProcess As Long,
pBaseAddress As Long, ByVal pZeroBits As Long, ByVal pCommitSize As Long,
pSectionOffset As mtPhysicalAddre ss, pViewSize As Long, ByVal
pInheritDisposi tion As Long, ByVal pAllocationType As Long, ByVal pProtect
As Long) As Long
Private Declare Function UnmapSectionVie w Lib "ntdll.dll" Alias
"NtUnmapViewOfS ection" (ByVal pProcess As Long, ByVal pBaseAddress As Long)
As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal pObject As Long)
As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMem ory"
(pDestination As Any, pSource As Any, ByVal pLength As Long)
Private Sub GetSerialParall el()
Dim vMemory(0 To &HF) As Byte
Dim vIX As Integer
Dim vPhysicalMemory As Long, vVirtualAddress As Long, vMemLength As Long
Dim vBaseAddress As String, vName As String
Dim vSectionAttribu tes As mtObjectAttribu tes
Dim vDeviceName As mtUnicodeString
Dim vViewBase As mtPhysicalAddre ss
Dim vPort As New AILocalPort
With vDeviceName
.Buffer = "\device\physic almemory" & Chr(0)
.MaxLength = Len(.Buffer) * 2
.Length = .MaxLength - 2
End With
With vSectionAttribu tes
.Length = Len(vSectionAtt ributes)
.ObjectName = VarPtr(vDeviceN ame)
.Attributes = mcCaseInsensiti ve
.SecurityDescri ptor = 0
.RootDirectory = 0
.SecurityQOS = 0
End With
Dim vReturn As Long
vReturn = OpenSection(vPh ysicalMemory, mcSectionMapRea d,
vSectionAttribu tes)
vVirtualAddress = 0
vViewBase.HighP art = 0
vViewBase.LowPa rt = &H400
vMemLength = &H10
vReturn = MapSectionView( vPhysicalMemory , -1&, vVirtualAddress , 0&,
vMemLength, vViewBase, vMemLength, mcViewShare, 0&, mcPageReadOnly)
CopyMemory vMemory(0), ByVal vVirtualAddress - vViewBase.LowPa rt + &H400,
&H10
Rem *** Serial ports ***
For vIX = 0 To &H7 Step 2
vBaseAddress = "&H" & Hex(vMemory(vIX + 1) * 256& + vMemory(vIX))
If Val(vBaseAddres s) > 0 Then
Set vPort = New AILocalPort
vName = "COM" & vIX / 2 + 1
vPort.Setup SerialCOM, vIX / 2 + 1, vName, vBaseAddress
mvPorts.Add vPort, vName
End If
Next
Rem *** Parallel ports ***
For vIX = 8 To &HD Step 2
vBaseAddress = "&H" & Hex(vMemory(vIX + 1) * 256& + vMemory(vIX))
If Val(vBaseAddres s) > 0 Then
Set vPort = New AILocalPort
vName = "LPT" & vIX / 2 - 3
vPort.Setup ParallelLPT, vIX / 2 - 3, vName, vBaseAddress
mvPorts.Add vPort, vName
End If
Next
Rem *** Clean up ***
UnmapSectionVie w -1&, vVirtualAddress
CloseHandle vPhysicalMemory
Set vPort = Nothing
End Sub
-----------------------------------------------
Code listing 2 - non-working VB.NET code
(Yes, I know it's a mess - I've been experimenting with it for so long!!!)

Option Strict On
Option Explicit On
Imports System.Runtime. InteropServices
Friend Class AILocalPorts
Inherits CollectionBase
Private Structure mtUnicodeString
Public Length As Short
Public MaxLength As Short
Public Buffer As String
End Structure
<StructLayout(L ayoutKind.Seque ntial)> Public Structure mtObjectAttribu tes
Public Length As Integer
Public RootDirectory As Integer
Public ObjectName As IntPtr
Public Attributes As Integer
Public SecurityDescrip tor As Integer
Public SecurityQOS As Integer
End Structure
<StructLayout(L ayoutKind.Seque ntial)> Private Structure mtPhysicalAddre ss
Public Sub New(ByVal pLowPart As Integer, ByVal pHighPart As Integer)
LowPart = pLowPart
HighPart = pHighPart
End Sub
Public LowPart As Integer
Public HighPart As Integer
End Structure
' <DllImport("ntd ll.dll", EntryPoint:="Nt OpenSection",
CharSet:=CharSe t.Ansi, ExactSpelling:= True)> _
' Private Function OpenSection(ByR ef pSection As Integer, ByVal pAccess As
Integer, ByRef pObjAttribs As mtObjectAttribu tes) As Integer
' End Function
'<DllImport("nt dll.dll", EntryPoint:="Nt OpenSection", SetLastError:=T rue,
CharSet:=CharSe t.Ansi, _
'ExactSpelling: =True, CallingConventi on:=CallingConv ention.Winapi)> _
'Public Shared Function OpenSection(ByR ef pSection As Integer, _
' ByVal pAccess As Integer, ByRef pAttributes As mtObjectAttribu tes) As
Integer
'End Function
'<DllImport("ke rnel32.dll")> Public Shared Function OpenFileMapping (ByVal
dwDesiredAccess As UInt32, _
'ByVal bInheritHandle As Boolean, ByVal lpName As String) As IntPtr
'End Function
Private Declare Ansi Function OpenSection Lib "ntdll.dll" Alias
"NtOpenSect ion" (ByRef pSection As Integer, ByVal pAccess As Integer,
ByRef
pObjAttribs As mtObjectAttribu tes) As Integer
'<DllImport("ke rnel32.dll")> Public Shared Function MapViewOfFile(B yVal
hFileMappingObj ect As IntPtr, _
'ByVal dwDesiredAccess As UInt32, ByVal dwFileOffsetHig h As UInt32, ByVal
dwFileOffsetLow As UInt32, _
'ByVal dwNumberOfBytes ToMap As UIntPtr) As IntPtr
'End Function
Private Declare Ansi Function MapSectionView Lib "ntdll.dll" Alias
"NtMapViewOfSec tion" (ByRef pSection As Integer, ByVal pProcess As
Integer,
ByRef pBaseAddress As Integer, ByVal pZeroBits As Integer, ByVal
pCommitSize As Integer, ByRef pSectionOffset As mtPhysicalAddre ss, ByRef
pViewSize As Integer, ByVal pInheritDisposi tion As Integer, ByVal
pAllocationType As Integer, ByVal pProtect As Integer) As Integer
Private Declare Ansi Function UnmapSectionVie w Lib "ntdll.dll" Alias
"NtUnmapViewOfS ection" (ByVal pProcess As Integer, ByVal pBaseAddress As
Integer) As Integer
Private Declare Ansi Function CloseHandle Lib "kernel32" (ByVal pObject As
Integer) As Integer
Private Declare Ansi Sub CopyMemory Lib "kernel32" Alias "RtlMoveMem ory"
(ByRef pDestination As Byte, ByRef pSource As Integer, ByVal pLength As
Integer)
Public Sub New()
MyBase.New()
Try
List.Clear()
GetSerialParall el()
Catch vException As Exception
If ASEC.QueryDebug (vException, "") Then Stop
End Try
End Sub
Private Sub GetSerialParall el()
Const cSectionMapRead As Integer = 4
Const cPageReadOnly As Integer = 2
Const cViewShare As Integer = 1
Dim vDeviceName As mtUnicodeString
With vDeviceName
.Buffer = "\device\physic almemory" & Convert.ToChar( 0)
.MaxLength = CType(.Buffer.L ength * 2, Short)
.Length = CType(.MaxLengt h - 2, Short)
End With
Dim vPointer As IntPtr
vPointer = Marshal.AllocHG lobal(Marshal.S izeOf(vDeviceNa me)) ' Allocate
some unmanaged memory
Marshal.Structu reToPtr(vDevice Name, vPointer, True) ' Copy the device
name structure to the pointer
Dim vSectionAttribu tes As mtObjectAttribu tes
With vSectionAttribu tes
.Length = Marshal.SizeOf( vSectionAttribu tes)
.ObjectName = vPointer
.Attributes = 64 ' Case insensitive
.SecurityDescri ptor = 0
.RootDirectory = 0
.SecurityQOS = 0
End With
Dim vReturn As Integer
Dim vPhysicalMemory As Integer ' vPhysicalMemory gets set to > 0 (eg 1144)
vReturn = OpenSection(vPh ysicalMemory, cSectionMapRead ,
vSectionAttribu tes) ' equiv = openfilemapping ?
If vReturn <> 0 Then MessageBox.Show (Marshal.GetLas tWin32Error.ToS tring)
Dim vVirtualAddress As Integer = 0
Dim vViewBase As New mtPhysicalAddre ss(1024, 0)
Dim vMemLength As Integer = 16
' equiv = mapviewoffile ?
vReturn = MapSectionView( vPhysicalMemory , -1, vVirtualAddress , 0,
vMemLength, vViewBase, vMemLength, cViewShare, 0, cPageReadOnly)
If vReturn <> 0 Then MessageBox.Show (Marshal.GetLas tWin32Error.ToS tring)
Dim vMemory(15) As Byte
CopyMemory(vMem ory(0), vVirtualAddress - vViewBase.LowPa rt + 1024, 16)
' Variables for port interation
Dim vIX As Integer
Dim vBaseAddress As String, vName As String
Dim vPort As New AILocalPort
' Serial ports
For vIX = 0 To 7 Step 2
vBaseAddress = "&H" & (vMemory(vIX + 1) * 256 +
vMemory(vIX)).T oString("X")
If CType(vBaseAddr ess, Integer) > 0 Then
vPort = New AILocalPort
vName = "COM" & vIX / 2 + 1
vPort.Setup(ASE C.PortTypes.Ser COM, CType(vIX / 2, Integer) + 1, vName,
vBaseAddress)
List.Add(vPort)
End If
Next
' Parallel ports
For vIX = 8 To 13 Step 2
vBaseAddress = "&H" & (vMemory(vIX + 1) * 256 +
vMemory(vIX)).T oString("X")
If CType(vBaseAddr ess, Integer) > 0 Then
vPort = New AILocalPort
vName = "LPT" & vIX / 2 - 3
vPort.Setup(ASE C.PortTypes.Par LPT, CType(vIX / 2, Integer) - 3, vName,
vBaseAddress)
List.Add(vPort)
End If
Next
' Clean up
UnmapSectionVie w(-1, vVirtualAddress )
CloseHandle(vPh ysicalMemory)
Marshal.FreeHGl obal(vPointer)
End Sub

When I finally get these calls working, I will be updating the lacking code
on pinvoke.net!!
Thanks to all who read this far ;)
_______________ ______
The Grim Reaper
Nov 20 '05 #1
9 1700
Hi Grim,

Did you know there is a special newsgroup for this kind of API upgrade
questions.

microsoft.publi c.dotnet.langua ges.vb.upgrade

(I write this because Herfried says that we answer to much questions here
which can as well be done in the special newsgroups).

An answer that you would get here first will be that seeing your code I
would first change all the longs for integer, mostly that is the biggest
problem.

(A long in VB6 is an integer in VB.net)

I hope this helps?

:-)

Cor
Nov 20 '05 #2
* "Cor Ligthert" <no**********@p lanet.nl> scripsit:
Did you know there is a special newsgroup for this kind of API upgrade
questions.

microsoft.publi c.dotnet.langua ges.vb.upgrade
.... or mpdf.interop.
(I write this because Herfried says that we answer to much questions here
which can as well be done in the special newsgroups).


Mhm...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
>
(I write this because Herfried says that we answer to much questions here which can as well be done in the special newsgroups).


Mhm...

Now I do exactly what you always say, what is wrong?

:-)

Cor

ps I have answered the AdoNet question in that newsgroup
Nov 20 '05 #4
* "Cor Ligthert" <no**********@p lanet.nl> scripsit:
(I write this because Herfried says that we answer to much questions
here
which can as well be done in the special newsgroups).


Mhm...


Now I do exactly what you always say, what is wrong?

:-)

ps I have answered the AdoNet question in that newsgroup


I don't say anything against posting replies/answers to OT questions
there. If I know an answer, I post the answer + hint for the more
appropriate group, if I don't know an answer, I post a hint for the more
appropriate group too ;-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5
> > Now I do exactly what you always say, what is wrong?

:-)

ps I have answered the AdoNet question in that newsgroup


I don't say anything against posting replies/answers to OT questions
there. If I know an answer, I post the answer + hint for the more
appropriate group, if I don't know an answer, I post a hint for the more
appropriate group too ;-).

You know what I mean with this message I think, although it is not written
as often with me?

Cor
Nov 20 '05 #6
Thank you Cor - I did indeed miss those 2 shorts at the top of the code.
However, they made no difference.

I posted to the vb.upgrade group about 12 hours beforehand. Got a bit
frustrated with it last night and posted here too. I'll try posting to
mpdf.interop as Herfried suggested.
_______________ _______________ ___
The Grim

"Cor Ligthert" <no**********@p lanet.nl> wrote in message
news:eq******** ******@tk2msftn gp13.phx.gbl...
Hi Grim,

Did you know there is a special newsgroup for this kind of API upgrade
questions.

microsoft.publi c.dotnet.langua ges.vb.upgrade

(I write this because Herfried says that we answer to much questions here
which can as well be done in the special newsgroups).

An answer that you would get here first will be that seeing your code I
would first change all the longs for integer, mostly that is the biggest
problem.

(A long in VB6 is an integer in VB.net)

I hope this helps?

:-)

Cor

Nov 20 '05 #7
Hi Herfried,

I can't find mpdf.interop ... got the full name or a clue where I might find
it?? :D
Thanks.

I tried posting to a couple of winapi groups.... and basically got told to
get lost... so I'm still stuck :(
_______________ _____________
The Grim Reaper

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:eB******** ******@tk2msftn gp13.phx.gbl...
* "Cor Ligthert" <no**********@p lanet.nl> scripsit:
Did you know there is a special newsgroup for this kind of API upgrade
questions.

microsoft.publi c.dotnet.langua ges.vb.upgrade


... or mpdf.interop.
(I write this because Herfried says that we answer to much questions here which can as well be done in the special newsgroups).


Mhm...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #8
* "The Grim Reaper" <gr*********@bt openworld.com> scripsit:
I can't find mpdf.interop ... got the full name or a clue where I might find
it?? :D


microsoft.publi c.dotnet.framew ork.interop. That's a group on the public
ms news server (likme this one).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #9
Sorry Herfried - I was too shortsighted to spot the abbreviation!

For anyone still interested in the subject of this thread - getting the
hardware base addresses of the serial and parallel ports - I've eventually
solved it using 3 complex WMI queries. So all that API/Marshaling is gone,
it's all in managed code now!!
If anyone else needs to know port addresses for use in VB.NET, reply to this
or drop me an email.
Thanks to all for your suggestions.
_______________ _______________
The Grim Reaper

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:eb******** ******@tk2msftn gp13.phx.gbl...
* "The Grim Reaper" <gr*********@bt openworld.com> scripsit:
I can't find mpdf.interop ... got the full name or a clue where I might find it?? :D


microsoft.publi c.dotnet.framew ork.interop. That's a group on the public
ms news server (likme this one).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
6505
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help Workshop program: hcw.exe that's included with Visual Basic. This exact same file compiled perfectly with no notes, warnings or errors prior to...
9
4390
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with the microsoft HTML workshop utility, lets call it c:\path\help.chm. My question is how do you launch it from the GUI? What logic do I put behind...
4
3335
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to cmd.Cmd but I would also like to get the man-page-like help for classes and functions. Does anyone know how to do that? Thanks. Sarir
2
6422
by: Sudheer Kareem | last post by:
Dear All Please tell me how to assosiate help files with my Vb.net Project. Regards Sudheer
6
4316
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing result in any way. Who can help me, I thank you very very much. list.cpp(main program)...
3
3335
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With numarray, help gives unhelpful responses:
7
5351
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available from clicking on many of the available topics (mostly methods but some properties are also unavailable). This same problem occurs with many, if not...
5
3253
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time using "F1" help within the VB IDE. Is this expectation achievable In trying to test my help file in the IDE, I have a solution with 2 projects:...
10
3343
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably the worst I ever seen. I almost cannot find anything I need, including things I
0
2862
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application calls MS Excel, so the scenario is that I am supposed to see the Excel Menu bar, FILE EDIT VIEW INSERT ... HELP. I am able to see the menu bar, but in...
0
7604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7916
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.