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

Weird marshalling problem

Hello,

This is the code:

~
Option Strict On
Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Class Test
Inherits Form

Friend Declare Auto Function GetWindowText Lib "user32.dll" ( _
ByVal hWnd as IntPtr, _
<MarshalAs(UnmanagedType.LPTStr), [In](), Out()> ByVal lpString As
String, _
ByVal nMaxCount as Integer _
) As Integer

Protected Overrides Sub OnLoad(e as EventArgs)
MyBase.OnLoad(e)
Text() = "All right!"
Dim tempStr As New String(chr(0),256)
Dim Result as Integer = GetWindowText(Me.Handle, tempStr,
tempStr.Length)
MessageBox.Show(Result.ToString & " " tempStr)
End Sub

<STAThread()> Shared Sub Main()
Application.Run(New Test)
End Sub
End Class
~

I expected it to msgbox "10 All right!" and it did it. In Windows XP.
But... testing it in Windows 98 gave strange result: "10 ". For some
curious reason string wasn't marshalled back, even though I included
OutAttribute.
However when I removed all attributes, it worked fine it both OS'es.
What's the difference? Is it documented or bug?

TIA
Roman
Nov 21 '05 #1
7 1045
use ByRef. ie.:
Friend Declare Auto Function GetWindowText Lib "user32.dll" ( _
ByVal hWnd as IntPtr, _
<MarshalAs(UnmanagedType.LPTStr)> ByRef lpString As String, _
ByVal nMaxCount as Integer ) As Integer
"Dragon" <no@spam.please> wrote in message
news:um****************@TK2MSFTNGP12.phx.gbl...
Hello,

This is the code:

~
Option Strict On
Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Class Test
Inherits Form

Friend Declare Auto Function GetWindowText Lib "user32.dll" ( _
ByVal hWnd as IntPtr, _
<MarshalAs(UnmanagedType.LPTStr), [In](), Out()> ByVal lpString As
String, _
ByVal nMaxCount as Integer _
) As Integer

Protected Overrides Sub OnLoad(e as EventArgs)
MyBase.OnLoad(e)
Text() = "All right!"
Dim tempStr As New String(chr(0),256)
Dim Result as Integer = GetWindowText(Me.Handle, tempStr,
tempStr.Length)
MessageBox.Show(Result.ToString & " " tempStr)
End Sub

<STAThread()> Shared Sub Main()
Application.Run(New Test)
End Sub
End Class
~

I expected it to msgbox "10 All right!" and it did it. In Windows XP.
But... testing it in Windows 98 gave strange result: "10 ". For some
curious reason string wasn't marshalled back, even though I included
OutAttribute.
However when I removed all attributes, it worked fine it both OS'es.
What's the difference? Is it documented or bug?

TIA
Roman

Nov 21 '05 #2
?

If I use ByRef, I get an ExecutionEngineException, which can't even be
handled!
Nov 21 '05 #3
Well, that sucks.

One moment. I'll sort a little test harness............

"Dragon" <no@spam.please> wrote in message
news:Oa**************@tk2msftngp13.phx.gbl...
?

If I use ByRef, I get an ExecutionEngineException, which can't even be
handled!

Nov 21 '05 #4
Hi,

"Dragon" <no@spam.please> wrote in message
news:um****************@TK2MSFTNGP12.phx.gbl...
Hello,

This is the code:

~
Option Strict On
Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Class Test
Inherits Form

Friend Declare Auto Function GetWindowText Lib "user32.dll" ( _
ByVal hWnd as IntPtr, _
<MarshalAs(UnmanagedType.LPTStr), [In](), Out()> ByVal lpString As
String, _
ByVal nMaxCount as Integer _
) As Integer

Protected Overrides Sub OnLoad(e as EventArgs)
MyBase.OnLoad(e)
Text() = "All right!"
Dim tempStr As New String(chr(0),256)
Dim Result as Integer = GetWindowText(Me.Handle, tempStr,
tempStr.Length)
MessageBox.Show(Result.ToString & " " tempStr)
End Sub

<STAThread()> Shared Sub Main()
Application.Run(New Test)
End Sub
End Class

You should be using a StringBuilder if native code changes the string. See

http://msdn.microsoft.com/library/de...forstrings.asp

scroll to the bottom : "Fixed-Length String Buffers"
HTH,
Greetings

~

I expected it to msgbox "10 All right!" and it did it. In Windows XP.
But... testing it in Windows 98 gave strange result: "10 ". For some
curious reason string wasn't marshalled back, even though I included
OutAttribute.
However when I removed all attributes, it worked fine it both OS'es.
What's the difference? Is it documented or bug?

TIA
Roman

Nov 21 '05 #5
Ah yes. GetWindowText just plonks the text into an already existing buffer,
however, your marshalling attributes were probably causing it some confusion
as you were technically trying to marshal it as .... actually I have no
idea. It works. Stop whinging ;)
"Dragon" <no@spam.please> wrote in message
news:Oa**************@tk2msftngp13.phx.gbl...
?

If I use ByRef, I get an ExecutionEngineException, which can't even be
handled!

Nov 21 '05 #6
In article <um**************@TK2MSFTNGP12.phx.gbl>, Dragon wrote:
Hello,

This is the code:

~
Option Strict On
Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Class Test
Inherits Form

Friend Declare Auto Function GetWindowText Lib "user32.dll" ( _
ByVal hWnd as IntPtr, _
<MarshalAs(UnmanagedType.LPTStr), [In](), Out()> ByVal lpString As
String, _
ByVal nMaxCount as Integer _
) As Integer


Private Declare Auto Function GetWindowText Lib "user32.dll" _
(ByVal hWnd As IntPtr, _
ByVal lpString As System.Text.StringBuilder, _
ByVal nMaxCount As Integer) As Integer
Public Function GetWindowText (ByVal hWnd As IntPtr) As String
Dim buffer As New StringBuilder (256)

If GetWindowText (hWnd, buffer, buffer.Length) > 0 Then
Return buffer.ToString ()

' if you want an error check, maybe somthing like...
' Else
' Throw New Win32Exception (Marshal.GetLastWin32Error ())
End If

End Function

--
Tom Shelton [MVP]
Nov 21 '05 #7
Okay, I'll move on to StringBuilder.

Thank you all for your replies.
Nov 21 '05 #8

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

Similar topics

2
by: RJ Lohan | last post by:
Howdy, I have a legacy DLL for which I have a problem marshalling a parameter type of char**. The function header (C++) is as so; extern "C" __declspec(dllexport) int __stdcall...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
2
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
1
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.