CreateWindowEx() fails while creating "Shell_TrayWnd"

R

Ray Phillips

I have been scouring the web for information on how to create something
similar to the system tray. Before anyone tries to show me how to make a
tray icon; that's NOT what I'm trying to do. Instead, I'm trying to get a
list of system tray applications.

By looking through the source code from GeoShell, I was able to figure out
that the process involves using CreateWindowEx() to create a window with the
class "Shell_TrayWnd" and then watch the WndProc messages for CopyData
messages. All sounds fairly simple. Or so I thought.

I created a windows form application in VB.NET 2005 and made the following
API Declarations:


'The CreateWindowEx function for making the window with a specified class name

Private Declare Auto Function CreateWindowEx Lib "user32" _
(ByVal dwExStyle As Integer, _
ByVal lpClassName As String, _
ByVal lpWindowName As String, _
ByVal dwStyle As Integer, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hWndParent As IntPtr, _
ByVal hMenu As IntPtr, _
ByVal hInstance As IntPtr, _
ByVal lpParam As IntPtr) As IntPtr

'GetLastError for finding the error if the CreateWindowEx fails

Private Declare Function GetLastError Lib "kernel32" () As Integer

I then put the following code in the Form Load event:

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

Dim hinst As IntPtr
Dim myTray as IntPtr

'Get hInstance

hinst = Marshal.GetHINSTANCE(GetType(Form1).Module)

'Attempt to create a window with the class "Shell_TrayWnd"

myTray = CreateWindowEx(0, "Shell_TrayWnd", _
0, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, hinst, IntPtr.Zero)

if myTray = IntPtr.Zero then
Dim err As Integer = GetLastError()
msgbox(err)
end if
End Sub

Any ideas what I'm doing wrong? I'm so close I can smell it! Is there a
managed code version of CreateWindowEx()? I need some way to create a window
with a specific window class.

BTW: I've written a replacement shell and this is one of the last steps
before it's finished. If you want to check it out, visit www.lcarsx32.com.

Thanks,
Ray
 
M

mayayana

See here for a discussion of why .Net may not be
a good idea for writing system functionality like a
replacement shell.

http://blogs.msdn.com/junfeng/archive/2005/11/18/494572.aspx
http://anotherlab.rajapet.net/2005/11/why-you-wont-find-c-example-code-for.h
tml

I don't actually know enough about it to know whether the
issues discussed relate to what you're doing, but it might
be worth checking out before you go any further. Just
as it wouldn't be ideal to write a new shell in Java, neither
is .Net designed for that sort of thing.
 
A

Armin Zingler

Ray said:
Any ideas what I'm doing wrong?

Any hint what the problem is? ;-) You didn't mention it. Try using
Marshal.GetLastWin32Error instead of GetLastError.

You may also use the System.Runtime.InteropServices.DllImport attribute
instead of the Declare statement for declaring the CreateWindowEx function.
There you can set the attribute's SetLastError field to True which is
required if you want the GetLastWin32Error function really return the last
error. AFAIK (I may be wrong) this is not done implicitly if you use Declare
instaed.

But wait,,, enabling Option Strict is always recommended. The 3rd argument
is a String. 0 is implicitly converted to "0" and a pointer to the String is
passed. I guess that's not what you want. Pass Nothing instead.


Armin
 
E

expvb

Ray Phillips said:
I have been scouring the web for information on how to create something
similar to the system tray. Before anyone tries to show me how to make a
tray icon; that's NOT what I'm trying to do. Instead, I'm trying to get a
list of system tray applications.

By looking through the source code from GeoShell, I was able to figure out
that the process involves using CreateWindowEx() to create a window with
the
class "Shell_TrayWnd" and then watch the WndProc messages for CopyData
messages. All sounds fairly simple. Or so I thought.

I created a windows form application in VB.NET 2005 and made the following
API Declarations:

Are you trying to make your own task bar replacement? If so, search for
samples that use SHAppBarMessage().

You probably don't need to use CreateWindowEx. See NativeWindow Class in the
help.
 

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

Top