Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / November 2003

Tip: Looking for answers? Try searching our database.

PostMessage to VB.NET messageloop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SM - 20 Nov 2003 08:52 GMT
Hello group,
i'm converting a VB6 application for .NET Framework.
The application depends on a DLL written in standard C.

The C DLL code creates an invisible window with CreateWindow()
with the WndProc function located into the VB.NET code.

Until the code was VB6/C code, they was OK.

Now the VB WndProc receives the startup message correctly
(WM_NCCREATE - WM_CREATE - WM_SIZE - .....)
but when i send (from the C code) custom messages (or
whatever kind of message) with PostMessage(....), using
as handle the same one returned from CreateWindow(..),
the WndProc doesn't receive them ....
I see this messages with Spy++ and all the parameters are OK.

What's the problem ?
Can i still use PostMessage() from C code to send a message
to .NET WndProc function ? I need to leave untouched the DLL ..

Thanks
--
Stefano M
email: *@*
(remove *)
Dmitriy Lapshin [C# / .NET MVP] - 20 Nov 2003 09:55 GMT
Hi Stefano,

I probably can't get you. Let's try to broke it all down into smaller
pieces.

1. A C DLL that creates an invisible window and contains a message loop for
that window,
2. A VB .NET WinForms application that can receive WM_xxx messages from the
invisible window.
3. The VB .NET app's main window handle is somehow passed to the DLL.

The problem is that you can't get the right window handle for the VB .NET's
application main window?
Well, Form.Handle should be the property returing the right handle value
(you might need to call its ToInt32() method depending on how you have
declared the DLL's functions in VB .NET).

Signature

Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

> Hello group,
> i'm converting a VB6 application for .NET Framework.
[quoted text clipped - 22 lines]
> email: *@*
> (remove *)
Ste M - 20 Nov 2003 10:21 GMT
Hi Dmitriy,
i try to explain better, i know it is a little complex..  :-)

1. VB .NET application makes a call to C DLL function. One of the function
parameters is a pointer to a VB.NET function.
2. In this C DLL function, the code creates an invisible window with
CreateWindow() using for the message loop the function pointer passed.
3. C DLL saves the window handle in a DLL global variable for future uses
4. C DLL function sends a PostMessage( saved_handle, WM_custom, xx, yy) but this
message is never received from VB.NET function (the message is visible with
Spy++)
5. Even if C DLL sends a message (custom or standard) just after the
CreateWindow(..), VB.NET function never receive it.
6. VB.NET message loop receives startup standard messages as WM_CREATE, WM_SIZE,
WM_MOVE,
....

Thanks
--
Stefano M
email: *@*
(remove *)
Dmitriy Lapshin [C# / .NET MVP] - 20 Nov 2003 11:46 GMT
Stefano,

> 2. In this C DLL function, the code creates an invisible window with
> CreateWindow() using for the message loop the function pointer passed.
> 3. C DLL saves the window handle in a DLL global variable for future uses
> 4. C DLL function sends a PostMessage( saved_handle, WM_custom, xx, yy) but this
> message is never received from VB.NET function (the message is visible with
> Spy++)

It is not clear why should the VB .NET callback receive the message in the
first place? Is this VB .NET function actually a window procedure? If yes, I
would advice to pay maximum attention on HOW do you pass the function
pointer to the C DLL. Ensure you specify the right calling convention
(WINAPI that is), and that all parameters will be marshalled properly.

Then, it's not clear what is meant under "the message is never received". If
you would post some standard message, would it be received by the VB .NET
procedure? Does it gets called at all?

I am just trying to focus on the root of the problem, so please be patient
to tons of my questions :-)

Signature

Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

> Hi Dmitriy,
> i try to explain better, i know it is a little complex..  :-)
[quoted text clipped - 18 lines]
> email: *@*
> (remove *)
SM - 20 Nov 2003 12:27 GMT
Dmitriy,
thanks again for your attention,

> It is not clear why should the VB .NET callback receive the message in the
> first place? Is this VB .NET function actually a window procedure? If yes, I
> would advice to pay maximum attention on HOW do you pass the function
> pointer to the C DLL. Ensure you specify the right calling convention
> (WINAPI that is), and that all parameters will be marshalled properly.

Yes, it is a window procedure for the window created into the C DLL.
OK, i'll re-check all the parameters .... but i think if the address was wrong,
i should have a crash !!

> Then, it's not clear what is meant under "the message is never received". If
> you would post some standard message, would it be received by the VB .NET
> procedure? Does it gets called at all?

The VB.NET window procedure receives all the standard messages fired
(internally) from the CreateWindow() call.
When i explicitly want to post a message (custom or not) to the invisible window
from my DLL code,
i think the message will be sent to its window procedure but the .NET window
procedure is not fired !

> I am just trying to focus on the root of the problem, so please be patient
> to tons of my questions :-)

Oh, don't worry !!! I understand that is not a clear architecture but
i'm migrating from an existent situation (VB6+C DLL), this
situation was created from previous programmer and I'm trying
to leave untouched the just-tested DLL. Maybe in the future I'll
have time to re-write also the DLL.....

Thanks a lot !
--
Stefano M
email: *@*
(remove *)
Dmitriy Lapshin [C# / .NET MVP] - 20 Nov 2003 12:58 GMT
> The VB.NET window procedure receives all the standard messages fired
> (internally) from the CreateWindow() call.
> When i explicitly want to post a message (custom or not) to the invisible window
> from my DLL code,
> i think the message will be sent to its window procedure but the .NET window
> procedure is not fired !

A-ha, now it's much more clear. So my suggestions are:

1. Check the custom message is registered (I believe there's an API function
to do that). Not sure the receiving party should also register the custom
message though.

2. The PostMessage function has some difficulties with marshalling custom
messages. Not sure what they meant in MSDN but here's the quote:

=== Cut ===
The system only does marshalling for system messages (those in the range 0
to WM_USER). To send other messages (those above WM_USER) to another
process, you must do custom marshalling.
=== Cut ===

I'd also suggest asking the more detailed question in the
dotnet.framework.interop newsgroup as it seems to be not related to the VB
.NET lanugage itself.

Signature

Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

> Dmitriy,
> thanks again for your attention,
[quoted text clipped - 34 lines]
> email: *@*
> (remove *)
Stephen Martin - 20 Nov 2003 13:13 GMT
How are you declaring the delegate that is being passed in to the dll for
the Window procedure? If you are not keeping a reference to the delegate it
will be garbage collected.

> Hello group,
> i'm converting a VB6 application for .NET Framework.
[quoted text clipped - 22 lines]
> email: *@*
> (remove *)

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.