|
Luc Pattyn wrote: here is my theory (still theory since I have not run any of this on Win64):
- for native pointers you really should use IntPtr, and it is not hard to do so.
- Win32 uses 4B pointers, Win64 needs 8B pointers
- you must build for either 32-bit or 64-bit
- on Win64 everything must be consistent (all modules 64-bit is fine, and
all modules 32-bit runs in some compatibility mode, not sure tho)
- IntPtr is a value type that holds either 4B or 8B pointer value
- IntPtr must be initialized, normally you give its value at construction
- a lot of P/Invoke examples are wrong, and use int/uint where it should have
been IntPtr. Most notably SendMessage: lParam, wParam and return value should
be regarded as IntPtr, and cast to int or whatever if and when appropriate.
- correct prototypes would have IntPtr for anything that is or could be a handle
(such as lParam).
Thanks for this explanation.
I've got a lot of code revision to do. I have older "toolkits" whose P/Invoke methods are apparently wrong, or will at least break on a 64-bit system.
Luc Pattyn wrote: Your problem seems to be you did not initialize handle.
Actually, I don't think I'm suppose to initialize the handle, rather I should use the out modifier on the IntPtr parameters rather than ref , as I had initially tried.
|
|
|
|
|
Leslie Sanford wrote: Actually, I don't think I'm suppose to initialize the handle, rather I should use the out modifier on the IntPtr parameters rather than ref, as I had initially tried
That depends entirely on the function at hand; you typically need to provide
an IntPtr or be ready to receive one (with "out" keyword).
I don't recall any Win32 function that provides a handle that way, they typically
use the return value (and return zero for error, then you would call GetLastError
for details).
Greetings
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Luc Pattyn wrote: That depends entirely on the function at hand; you typically need to provide
an IntPtr or be ready to receive one (with "out" keyword).
I don't recall any Win32 function that provides a handle that way, they typically
use the return value (and return zero for error, then you would call GetLastError
for details).
Oh, I wasn't clear. The example I gave in my earlier post was with waveOutOpen ; it opens a waveform output device. The native version takes an pointer to a WAVEOUT handle and returns a MMRESULT value indicating the status of the operation. If the operation was successful, the handle is filled with a valid handle to the device. This handle is later used with other waveOutXXX functions to interact with the device.
So in this specific case, I needed an unitialized IntPtr to pass as the handle modified by the out keyword.
After the wave device has been opened, other waveOutXXX functions that are called expect the handle to be initialized.
Does this make sense?
|
|
|
|
|
Leslie Sanford wrote: Does this make sense?
Of course, it does.
I was not familiar with waveOutOpen, but I looked it up now, and you are right,
it does output a handle, hence "out someIntPtr" is the right way to go.
The only comment I could make is you don't need the "=new IntPtr()" part of
your handle declaration, a simple "IntPtr handle;" should suffice.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Thanks for your responses.
I have to say that this has been a humbling day. Code that I had written a couple of years ago that I thought was fine has turned out not to be correct. I've had to go back through my code and correct this problem.
|
|
|
|
|
First of all, you're welcome.
Second, that is a common situation when the playing field changes.
Things are working fine, they look good, compile without hassles, run great,
so you think they might be correct; and then something changes, it may still
work fine, but we got new information or new insights, and we feel we have
to adapt the code, so we learn new ways to be even "more correct" about it.
I have lots of experience in writing C code for different embedded systems,
and I can tell you, if you use five different compilers, it is just impossible
to write one page of code that all compilers will digest without giving a single
warning.
Things have improved, newer languages are more strict; having a single tool vendor
(Microsoft) also helps. But then documentation, whatever they try, is not good
enough. It is great at describing API functions, and less so for general
principles, do's and don'ts, etc.
And to make matters worse, you need more than is officially available (examples,
suggestions, bug work-arounds, ...) but lots of things you find on Internet
(including on CP) seem OK but are plain wrong, often the authors are satisfied
they got something that works, but did not bother (enough) about correctness and
general fitness.
I did investigate the IntPtr stuff when I developed TrayIconBuster (see one of my
CP articles); I was hoping it would run on XP, Vista and Win64 and had read some
messages signaling pitfalls in Win64. Having only XP-32 available, I had to research
a lot before I came up with "my theory", the one I presented earlier. And I must
admit, I did not get most of it from Microsoft documentation, and I don't really
have proof, having only XP to test on.
In earlier code I did not even use IntPtr, I used uint instead (the IntPtr stuff
did not seem to have any added value, just some coding overhead), but I too learned
it the hard way, so a couple weeks ago I revisited all my P/Invoke stuff and fixed
the pointers. Having my own classes (LP_user32, LP_kernel32, ...) holding the
prototypes helped a lot in finding them all; so it was a matter of adapting the
prototypes, then let the compiler discover the remaining problem spots.
One thing I did not understand from day one I started with .NET is why the heck
doesn't Microsoft do what www.pinvoke.net is trying to do, i.e. provide complete and
accurate prototypes for ALL Win32 functionality; they did provide all the header
files in older Visual C versions, and they must have the files internally.
Having them from MS would have improved their quality and completeness.
Unfortunately I have two more comments:
- there is a concept of safe handles since .NET 2.0; I have not studied it yet;
- I just discovered a "HandleRef" structure, which has existed since 1.0
(although its ToIntPtr method is new since 2.0)
Taken together, I would doubt turning handles simply into IntPtrs is really OK.
So I am afraid the theory is still evolving...
Is this a humbling experience ? I don't know; I do realize the more you learn,
the more you realize you don't know much. But that's fine by me, it won't stop
my eagerness to learn.
BTW I am a chess player, and it is my daily experience: some people know more
about the game, some less. You can try to work your way up, you may succeed in
improving your average, but the net result is some people know more, some less.
However hard you try. Well, unless you are at Kasparov's level that is.
Cheers.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I am having a Select Query which is suppose to select multipple records from a table (MS-Access). I am using OLEDB from my connection and using OLEDB Adapter and datasets to desplay data. But I want to perform an operation on Each records after selecting from the DB- Which I want to Diaplay to the Data Grid also. But By using the OLEDB i am unable to collect the information from the records before binding. Is any way there to take the records and operate on it before binding, and using OLEDB ?
Dim con As OleDbConnection<br />
Dim cmd As OleDbCommand = New OleDbCommand<br />
Dim dataSet As dataSet = Nothing<br />
Sub BindGrid()<br />
Dim oleDbDataAdapter As OleDbDataAdapter = Nothing<br />
Dim ConnectionString = System.Configuration. _<br />
ConfigurationSettings.AppSettings(ConConstant.MS_ACCESS_CONNECTION_STRING)<br />
' Ge the sql query<br />
Dim SelectQueryText As String = String.Empty<br />
SelectQueryText = "Select Leave_Code as LeaveCode,Leave_Name as LeaveName,NoOfDays as Days from Leave_Mstr"<br />
oleDbDataAdapter = New OleDbDataAdapter(SelectQueryText, _<br />
New OleDbConnection(ConnectionString))<br />
' Create the data set<br />
dataSet = New DataSet<br />
' Fill the data set with the data<br />
oleDbDataAdapter.Fill(dataSet)<br />
If (Not oleDbDataAdapter Is Nothing) Then<br />
oleDbDataAdapter = Nothing<br />
End If<br />
With dgLeavCrtnLeavDtls<br />
.DataSource = dataSet<br />
.DataMember = dataSet.Tables(0).TableName<br />
.DataBind()<br />
End With<br />
End Sub<br />
Sub tablebind()<br />
Dim oleDbDataAdapter As OleDbDataAdapter = Nothing<br />
Dim ConnectionString = System.Configuration. _<br />
ConfigurationSettings.AppSettings(ConConstant.MS_ACCESS_CONNECTION_STRING)<br />
' Ge the sql query<br />
Dim SelectQueryText As String = String.Empty<br />
SelectQueryText = "Select Leave_Code as LeaveCode,Leave_Name as LeaveName,NoOfDays as Days from Leave_Mstr"<br />
oleDbDataAdapter = New OleDbDataAdapter(SelectQueryText, _<br />
New OleDbConnection(ConnectionString))<br />
' Create the data set<br />
dataSet = New DataSet<br />
' Fill the data set with the data<br />
oleDbDataAdapter.Fill(dataSet)<br />
Dim dh As TableHeaderCell<br />
Dim dc As DataColumn<br />
Dim dr As DataRow<br />
For Each dr In dataSet.Tables(0).Rows<br />
Dim trow As New TableRow<br />
For Each dc In dataSet.Tables(0).Columns<br />
Dim tcell As New TableCell<br />
tcell.Controls.Add(New LiteralControl(dr(dc.ColumnName).ToString))<br />
trow.Cells.Add(tcell)<br />
Next<br />
Next<br />
End Sub
This is the function I am using to desplay values to tables.Here i want to take the All the available leave codes to an array or some variable and want to process on it.
Praveen.K
System Engineer
Graviton Technologies Pvt.Ltd
|
|
|
|
|
Instead of using the <code> tag, you should use the <pre> tag.
Dim con As OleDbConnection
Dim cmd As OleDbCommand = New OleDbCommand
Dim dataSet As dataSet = Nothing
Sub BindGrid()
Dim oleDbDataAdapter As OleDbDataAdapter = Nothing
Dim ConnectionString = _
System.Configuration.ConfigurationSettings.AppSettings(ConConstant.MS_ACCESS_CONNECTION_STRING)
Dim SelectQueryText As String = String.Empty
SelectQueryText = "Select Leave_Code as LeaveCode,Leave_Name as LeaveName,NoOfDays as Days from Leave_Mstr"
oleDbDataAdapter = New OleDbDataAdapter(SelectQueryText, New OleDbConnection(ConnectionString))
dataSet = New DataSet
oleDbDataAdapter.Fill(dataSet)
If (Not oleDbDataAdapter Is Nothing) Then
oleDbDataAdapter = Nothing
End If
With dgLeavCrtnLeavDtls
.DataSource = dataSet
.DataMember = dataSet.Tables(0).TableName
.DataBind()
End With
End Sub
Sub tablebind()
Dim oleDbDataAdapter As OleDbDataAdapter = Nothing
Dim ConnectionString = _
System.Configuration.ConfigurationSettings.AppSettings(ConConstant.MS_ACCESS_CONNECTION_STRING)
Dim SelectQueryText As String = String.Empty
SelectQueryText = "Select Leave_Code as LeaveCode,Leave_Name as LeaveName,NoOfDays as Days from Leave_Mstr"
oleDbDataAdapter = New OleDbDataAdapter(SelectQueryText, New OleDbConnection(ConnectionString))
dataSet = New DataSet
oleDbDataAdapter.Fill(dataSet)
Dim dh As TableHeaderCell
Dim dc As DataColumn
Dim dr As DataRow
For Each dr In dataSet.Tables(0).Rows
Dim trow As New TableRow
For Each dc In dataSet.Tables(0).Columns
Dim tcell As New TableCell
tcell.Controls.Add(New LiteralControl(dr(dc.ColumnName).ToString))
trow.Cells.Add(tcell)
Next
Next
End Sub Why do you have both of these functions? They do almost the same thing, except one tries to bind the data grid to the table and the other one looks like you are trying to copy the row to a new row. It's not entirely clear what you are trying to do or what the problem is that you are encountering.
|
|
|
|
|
"inside a flash web page" i need to link a button to another web page.html
i tried the linkage option in the action script but it doesn't work
somebody help me please
best regard
bachirium
|
|
|
|
|
What has this got to do with .NET?
|
|
|
|
|
Hi everybody!
I want source chinese chess by C# or VB.net for my studying.
Can you get me!
Thank!
|
|
|
|
|
Dear Frnds,
I have got Desktop Application setup to deploy my application
i want to install the Crystal Reports msi through this setup but i am not able to install the same where as rest of the things(application assemblies) get installed properly.
Mayuresh
|
|
|
|
|
Hai,
I am using dotnetFramework1.1 with C# coding for my project.
I need to display some Records.So i use DataList Control For that...But my Problem is that when I click a particular row ,i want to redirect to some other page showing the full description of that particular record.How is this possible????....Please hepl me to solve my proble....
thanks in advance
Bijesh
|
|
|
|
|
|
Can any one tell me why value type instance is created in Stack?
M.Sendilkumar
|
|
|
|
|
Because allocation on stack is much faster than on the heap (just move the stack pointer).
-^-^-^-^-^-
no risk no funk ................... please vote ------>
|
|
|
|
|
I know this may seem easy for some of you and I hope that you can help me, so here goes.
In VB6 there was an OpenDatabase function, well in VB.net there is no such function (even with the DAO reference). My other code is functioning off of VB6 that I converted to VB.net and the DAO reference and I do not want to re-work my whole project, anybody know a work-around or something???
|
|
|
|
|
You really don't have a choice. You're going to have to rework the database code. DAO died a merciful death, what, 8 years ago...?
ADO.NET is what you're going to have to use.
|
|
|
|
|
Hi,
I need to use an open source reporting framework, and naturally I'd prefer to use it in .NET.
Couldn't find anything serious - the only thing I found was DataVision, but it's for Java and requires JDBC. MSSQL will not work with it.
Can any one recommend or give a link to a .NET open source reporting framework?
(DataVision might end up being used with Oracle, but only as a default).
Thanks,
Yuval
"The true sign of intelligence is not knowledge but imagination." - Albert Einstein
|
|
|
|
|
There's always this[^] one.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Thanks Pete!
Just what I was looking for!
Yuval
"The true sign of intelligence is not knowledge but imagination." - Albert Einstein
|
|
|
|
|
Thanks for the link, Pete. That one looks really good
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Hi, I read some paper about how clr works on managed modules, managed heap and jit the managed assembly. That works on heap (reference type). It seems me that only the reference to those oblects are on the stack but the real objects are on the heap. Value type instead are allocate on the stack. I'm wondering what changes if also objects were allocate on the stack; what'll happen? If also objects will be on the stack where do the jitting go? Will I have need of assembly; What changes are necessary, please?
thank you.
|
|
|
|
|
MarKus0 wrote: I'm wondering what changes if also objects were allocate on the stack; what'll happen?
Nothing happens because object instances are NEVER allocated on the stack.
|
|
|
|
|
yes, I know that.
But the question was different: what changes should I do inside CLR if I want allocate object on the stack, what changes on the CLR Should I do? (I want change CLR for this, it's a theoretical question, my curiousity)
thanks
|
|
|
|
|