By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,053 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,053 IT Pros & Developers. It's quick & easy.

Need help converting structure to VB.Net

P: n/a
Hi there,

I've implemented a local system hook to suppress certain windows beeing
displayed by the axWebbrowser control. Now I need some more information
before I can decide, whether to suppress a window or not.

My callback function get a long pointer (lParam) to a structure which
contains further information. This structure is described at MSN as follows:

typedef struct tagCREATESTRUCT {
LPVOID lpCreateParams;
HINSTANCE hInstance;
HMENU hMenu;
HWND hwndParent;
int cy;
int cx;
int y;
int x;
LONG style;
LPCTSTR lpszName;
LPCTSTR lpszClass;
DWORD dwExStyle;
} CREATESTRUCT, *LPCREATESTRUCT;

I am trying to reference the structure by using the
Marshal.PtrToStructure(lParam, t.GetType()) method, where t is a local
var to the structure. But I don't seem to manage a correct
implementation of the above structure.

I'd really appreciate any help on this.

regards.
Nov 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

Have you searched http://www.pinvoke.net ?

Basicly the handle and pointer members become IntPtrs, the ints, LONGs
and DWORDs translate to Integers and the LPCTSTRs become Strings.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2

P: n/a
Hi,

thanks for your reply. But translating LPCTSTR to Strings throws an
exception. I tried translating the rest like you said and keeping LPCSTR
as intPTR, but i am still getting strange values within my structure.

I'll check the given URL for more informations. Maybe I can find
something helpful.

Mattias Sjögren wrote:
Have you searched http://www.pinvoke.net ?

Basicly the handle and pointer members become IntPtrs, the ints, LONGs
and DWORDs translate to Integers and the LPCTSTRs become Strings.

Mattias

Nov 21 '05 #3

P: n/a
LPCStr's are actually unicode null terminating strings. If you look in the
..Net Framework/Platform SDK's they mention changing them to StringBuilder
rather than strings.

If you want me to convert it (structure) for you, I can, but really its not
very difficult

"Hugo Amselschlag" wrote:
Hi,

thanks for your reply. But translating LPCTSTR to Strings throws an
exception. I tried translating the rest like you said and keeping LPCSTR
as intPTR, but i am still getting strange values within my structure.

I'll check the given URL for more informations. Maybe I can find
something helpful.

Mattias Sjögren wrote:
Have you searched http://www.pinvoke.net ?

Basicly the handle and pointer members become IntPtrs, the ints, LONGs
and DWORDs translate to Integers and the LPCTSTRs become Strings.

Mattias

Nov 21 '05 #4

P: n/a
Crouchie,

"Crouchie1998" <Cr**********@discussions.microsoft.com> schrieb:
LPCStr's are actually unicode null terminating strings.


I have to disagree...

'LPCSTR' strings are pointers to constant, null-terminated strings of 8-bit
Windows (ANSI) characters.

'LPCTSTR' strings are 'LPSTR' strings on non-Unicode systems, and 'LPCWSTR'
strings on Unicode systems. 'LPCWSTR' strings are pointers to a constant
null-terminated string of 16-bit Unicode characters.

More information:

Platform SDK: Windows API -- Windows Data Types
<URL:http://msdn.microsoft.com/library/en-us/winprog/winprog/windows_data_types.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5

P: n/a
Addendum:

'StringBuilder' cannot be used in structures (MSDN: "Strings are valid
members of structures; however, 'StringBuilder' buffers are invalid in
structures."). Instead, use a string + 'MarshalAsAttribute' + corresponding
'UnmanagedType'. In our particular case, '<MarshalAs(UnmanagedType.LPTStr)>
lpszName As String', for example.

More information:

..NET Framework Developer's Guide -- Default Marshaling for Strings
<URL:http://msdn.microsoft.com/library/en-us/cpguide/html/cpcondefaultmarshalingforstrings.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6

P: n/a
In article <#i**************@tk2msftngp13.phx.gbl>, Hugo Amselschlag wrote:
Hi there,

I've implemented a local system hook to suppress certain windows beeing
displayed by the axWebbrowser control. Now I need some more information
before I can decide, whether to suppress a window or not.

My callback function get a long pointer (lParam) to a structure which
contains further information. This structure is described at MSN as follows:

typedef struct tagCREATESTRUCT {
LPVOID lpCreateParams;
HINSTANCE hInstance;
HMENU hMenu;
HWND hwndParent;
int cy;
int cx;
int y;
int x;
LONG style;
LPCTSTR lpszName;
LPCTSTR lpszClass;
DWORD dwExStyle;
} CREATESTRUCT, *LPCREATESTRUCT;


Structure CREATESTRUCT
Public lpCreateParams as IntPtr
Public hInstance As IntPtr
Public hMenu As IntPtr
Public hwndParent As IntPtr
Public cy As Integer
Public cx As Integer
Public y As Integer
Public x As Integer
Public style As Integer
Public lpszName As IntPtr
Public lpszClass As IntPtr
Public dwExStyle As Integer
End Structure

I assume your getting a IntPtr that referes to one of these
structures... You should be able to do something like:

Dim cs As CREATESTRUCT = _
CType (Marshal.PtrToStructure (theIntPtr, GetType (CREATESTRUCT)), _
CREATESTRUCT)

If you need to access the lpszName or lpszClass members, you'll need to
look at the the Marshal.PtrToStringXXX methods. You would probably be
able to use Marshal.PtrToStringAuto here though...

Dim className As String = Marshal.PtrToStringAuto (cs.lpszClass)

Anyway, that's what I would do if I understand your question correctly
;)

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 14 Days, 17 Hours, 26 Minutes, 22 Seconds
Nov 21 '05 #7

P: n/a
Why is it written in the Platform SDK & the .NET Framework SDK that LPCStr
etc are null terminated unicode strings?

Yes, I know about the data types

The original structure states LPCStr & not LPTStr. So, how can you convert
it to T CHAR?
<MarshalAs(UnmanagedType.LPTStr)>
I disagree with what you have used above.

Yesterday, there was a user who asked about converting a function which used
LPCWStr & LPCStr. If you look them up in the data types you'll notice that
they are either declared as System.String or System.StringBuilder
(System.Text.StringBuilder) & handled accordingly.
Nov 21 '05 #8

P: n/a
"Crouchie1998" <Cr**********@discussions.microsoft.com> schrieb:
Why is it written in the Platform SDK & the .NET Framework SDK that LPCStr
etc are null terminated unicode strings?
I didn't find any occurance of 'LPCSTR' in the .NET Framework documentation,
and the Platform SDK documentation (same page as mentioned in my previous
post) says on 'LPCSTR' "Pointer to a constant null-terminated string of
8-bit Windows (ANSI) characters". ANSI, not Unicode!
The original structure states LPCStr & not LPTStr. So, how can you convert
it to T CHAR?
Doesn't it use 'LPCTSTR' (the syntax in the documentation of "'CREATESTRUCT'
Structure" does)?

| LPCTSTR lpszName;
| LPCTSTR lpszClass;
<MarshalAs(UnmanagedType.LPTStr)>
I disagree with what you have used above.


I think that 'LPTStr' is OK. It's not even necessary to specify the
'MarshalAs' attribute if 'CharSet' is specified for the structure containing
the string members:

\\\
<StructLayout(LayoutKind.Auto, CharSet:=CharSet.Auto)> _
Public Structure...
...
End Structure
///

In this case, strings are passed to a function ('CreateWindowEx') and are
not manipulated by this function.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #9

P: n/a
hey Tom,

I think you understood what I am trying to do. I am getting an IntPtr
which points to a structure of the window which is about to be created.
This structure contains further information about the window like title,
coordinates etc. My first try converting the given structure to vb.net
looked exactly like yours. But when I read out the values, I always have
0 for x and 96 for the y-value. I'm also getting negative values for
lpszClass and 0 for lpszName. So I think, there is a problem with the
structure.

@Crouchie1998:
If it's easy for you to convert it for me, I'd really appreciate it. I
am somehow stuck at this point.

Thanks.
Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.