Need help converting structure to VB.Net

  • Thread starter Thread starter Hugo Amselschlag
  • Start date Start date
H

Hugo Amselschlag

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.
 
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
 
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.
 
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
 
Crouchie,

Crouchie1998 said:
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>
 
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>
 
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
;)
 
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.
 
Crouchie1998 said:
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.
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Window class 8
Balloon-Tooltips (CreateWindow fails) 5

Back
Top